Skip to main content

Matrix Functions

Functions whose output is a matrix

correlation

PearsonsCorrelation computes correlations defined by the formula:

cor(X, Y) = sum[(xi - E(X))(yi - E(Y))] / [(n - 1)s(X)s(Y)]

where E(X) and E(Y) are means of X and Y and s(X), s(Y) are standard deviations.

Syntax

correlation(list)

The list parameter is a list of variables which can be one of:

  • Timeseries
  • Reference to a Timeseries
  • String id of a Timeseries

Example

Con = TimeSeries("2021-10-01", "BUSINESS", [8.12,1.19,2.82,4.1,-6.31,6.87,1.16,-0.63,1.25,0.93])
Man = TimeSeries("2021-10-01", "BUSINESS", [8.98,4.23,0.71,2.47,-8.29,7.34,-0.39,-2.18,3.86,-0.84])
Tech = TimeSeries("2021-10-01", "BUSINESS", [8.97,5.32,3.31,6,-7.75,7.51,3.38,-2.33,1.01,3.06])
Health = TimeSeries("2021-10-01", "BUSINESS", [5.25,3.23,0.48,-3.13,-3.37,6.84,-2.19,-0.61,-0.94,4.73])

lts = [Con, Man, Tech, Health]

print correlation(lts)

Output

        Con         Man         Tech        Health
Con 1 0.926711 0.940898 0.627738
Man 0.926711 1 0.903867 0.660247
Tech 0.940898 0.903867 1 0.624856
Health 0.627738 0.660247 0.624856 1


covariance

Unbiased covariances are given by the formula cov(X, Y) = sum [(xi - E(X))(yi - E(Y))] / (n - 1)

where E(X) is the mean of X and E(Y) is the mean of the Y values.

Non-bias-corrected estimates use n in place of n - 1. Whether or not covariances are bias-corrected is determined by the optional parameter, biasCorrected which defaults to true.

Syntax

covariance(list)
covariance(list, biasCorrected)

The list parameter is a list of variables which can be one of:

  • Timeseries
  • Reference to a Timeseries
  • String id of a Timeseries

The biasCorrected parameter is a boolean.

Example

Con = TimeSeries("2021-10-01", "BUSINESS", [8.12,1.19,2.82,4.1,-6.31,6.87,1.16,-0.63,1.25,0.93])
Man = TimeSeries("2021-10-01", "BUSINESS", [8.98,4.23,0.71,2.47,-8.29,7.34,-0.39,-2.18,3.86,-0.84])
Tech = TimeSeries("2021-10-01", "BUSINESS", [8.97,5.32,3.31,6,-7.75,7.51,3.38,-2.33,1.01,3.06])
Health = TimeSeries("2021-10-01", "BUSINESS", [5.25,3.23,0.48,-3.13,-3.37,6.84,-2.19,-0.61,-0.94,4.73])

lts = [Con, Man, Tech, Health]

mc = covariance(lts)
print mc
mc = covariance(lts, false)
print mc

Output

        Con         Man         Tech        Health
Con 16.141200 18.532833 18.617622 9.361344
Man 18.532833 24.777610 22.158876 12.199110
Tech 18.617622 22.158876 24.256440 11.423153
Health 9.361344 12.199110 11.423153 13.777943

Con Man Tech Health
Con 14.527080 16.679550 16.755860 8.425210
Man 16.679550 22.299849 19.942988 10.979199
Tech 16.755860 19.942988 21.830796 10.280838
Health 8.425210 10.979199 10.280838 12.400149