# Correlation factor

## Description

Counting linear correlation factor

Source: PROLOG str. 200 Wydawnictwo PLJ Warszawa 1991 ISBN 83-85190-63-5

Program source code: correlation_factor.pl

## Listing

```suma([], Sum, Sum, Ilosc, Ilosc).
suma([H|T], Sum, Sum1, Ilosc, Ilosc1) :- Nilosc is Ilosc + 1,
Nsuma is Sum1 + H, suma(T, Sum, Nsuma, Ilosc, Nilosc).

sred(L, Sr) :- suma(L, S, 0, I, 0), Sr is S/I.

wari(L, W) :- sred(L, Sr), rozn2(L, H, Sr), suma(H, Suma, 0, Ilosc, 0),
W is Suma/Ilosc.
rozn2([], [], _).
rozn2([H|T], [R|T1], Sr) :- R is (H - Sr)*(H - Sr), rozn2(T, T1, Sr).

odch(L, O) :- wari(L, War), O is sqrt(War).

korel(L1, L2, R) :- sred(L1, Sr1),
sred(L2, Sr2),
rozn(L1, K1, Sr1),
rozn(L2, K2, Sr2),
mnoz(K1, K2, Sr2),
suma(K3, Cov, 0, Ilosc, 0),
odch(L1, O1),
odch(L2, O2),
R is Cov/(Ilosc*O1*O2).

rozn([], [], _).
rozn([H|T], [R|T1], Sr) :- R is H - Sr, rozn(T, T1, Sr).

mnoz([], [], []).
mnoz([H1|T1], [H2|T2], [H3|T3]) :- H3 is H1*H2, mnoz(T1, T2, T3).

test :- korel([1, 5, 3, 4, 6], [8, 12, 3, 5, 9], K).```