-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathCovariance.fs
More file actions
137 lines (122 loc) · 6.6 KB
/
Covariance.fs
File metadata and controls
137 lines (122 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module CovarianceTests
open Expecto
open FSharp.Stats
[<Tests>]
let sequenceTests =
let x = [5.;12.;18.;-23.;45.]
let y = [2.;8.;18.;-20.;28.]
let xd = [5m;12m;18m;-23m;45m]
let yd = [2m;8m;18m;-20m;28m]
testList "Seq" [
testCase "cov of floats" <| fun () ->
let cov = Seq.cov x y
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulation of floats" <| fun () ->
let covPop = Seq.covPopulation x y
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "cov of decimals" <| fun () ->
let cov = Seq.cov xd yd
Expect.equal cov 434.90m "Should be equal (decimal)"
testCase "covPopulation of decimals" <| fun () ->
let covPop = Seq.covPopulation xd yd
Expect.equal covPop 347.92m "Should be equal (decimal)"
testCase "covOfPairs of floats" <| fun () ->
let cov = (x, y) ||> Seq.zip |> Seq.covOfPairs
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulationOfPairs of floats" <| fun () ->
let covPop = (x, y) ||> Seq.zip |> Seq.covPopulationOfPairs
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "covOfPairs of decimals" <| fun () ->
let cov = (xd, yd) ||> Seq.zip |> Seq.covOfPairs
Expect.equal cov 434.90m "Should be equal (decimal)"
testCase "covPopulationOfPairs of decimals" <| fun () ->
let covPop = (xd, yd) ||> Seq.zip |> Seq.covPopulationOfPairs
Expect.equal covPop 347.92m "Should be equal (decimal)"
testCase "covBy of floats" <| fun () ->
let cov = (x, y) ||> Seq.zip |> Seq.map(fun (x, y) -> {| x = x; y = y |}) |> Seq.covBy(fun x -> x.x, x.y)
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulationBy of floats" <| fun () ->
let covPop = (x, y) ||> Seq.zip |> Seq.map(fun (x, y) -> {| x = x; y = y |}) |> Seq.covPopulationBy(fun x -> x.x, x.y)
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "covBy of decimals" <| fun () ->
let cov = (xd, yd) ||> Seq.zip |> Seq.map(fun (x, y) -> {| x = x; y = y |}) |> Seq.covBy(fun x -> x.x, x.y)
Expect.equal cov 434.90m "Should be equal (decimal)"
testCase "covPopulationBy of decimals" <| fun () ->
let covPop = (xd, yd) ||> Seq.zip |> Seq.map(fun (x, y) -> {| x = x; y = y |}) |> Seq.covPopulationBy(fun x -> x.x, x.y)
Expect.equal covPop 347.92m "Should be equal (decimal)"
]
[<Tests>]
let listTests =
let x = [5.;12.;18.;-23.;45.]
let y = [2.;8.;18.;-20.;28.]
testList "List" [
testCase "cov" <| fun () ->
let cov = List.cov x y
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulation" <| fun () ->
let covPop = List.covPopulation x y
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "covOfPairs of floats" <| fun () ->
let cov = (x, y) ||> List.zip |> List.covOfPairs
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulationOfPairs of floats" <| fun () ->
let covPop = (x, y) ||> List.zip |> List.covPopulationOfPairs
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "covBy of floats" <| fun () ->
let cov = (x, y) ||> List.zip |> List.map(fun (x, y) -> {| x = x; y = y |}) |> List.covBy(fun x -> x.x, x.y)
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulationBy of floats" <| fun () ->
let covPop = (x, y) ||> List.zip |> List.map(fun (x, y) -> {| x = x; y = y |}) |> List.covPopulationBy(fun x -> x.x, x.y)
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
]
[<Tests>]
let arrayTests =
let x = [| 5.;12.;18.;-23.;45. |]
let y = [| 2.;8.;18.;-20.;28. |]
testList "Array" [
testCase "cov" <| fun () ->
let cov = Array.cov x y
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulation" <| fun () ->
let covPop = Array.covPopulation x y
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "covOfPairs of floats" <| fun () ->
let cov = (x, y) ||> Array.zip |> Array.covOfPairs
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulationOfPairs of floats" <| fun () ->
let covPop = (x, y) ||> Array.zip |> Array.covPopulationOfPairs
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
testCase "covBy of floats" <| fun () ->
let cov = (x, y) ||> Array.zip |> Array.map(fun (x, y) -> {| x = x; y = y |}) |> Array.covBy(fun x -> x.x, x.y)
Expect.floatClose Accuracy.high cov 434.90 "Should be equal (double precision)"
testCase "covPopulationBy of floats" <| fun () ->
let covPop = (x, y) ||> Array.zip |> Array.map(fun (x, y) -> {| x = x; y = y |}) |> Array.covPopulationBy(fun x -> x.x, x.y)
Expect.floatClose Accuracy.high covPop 347.92 "Should be equal (double precision)"
]
//tested in comparison with
//https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
//[<Tests>]
//let matrixTests =
// let m =
// [|
// [|4.0;4.2;3.9;4.3;4.1|]
// [|2.0;2.1;2.0;2.1;2.2|]
// [|0.60;0.59;0.58;0.62;0.63|]
// |]
// |> matrix
// testList "Matrix" [
// testCase "rowSampleCovarianceMatrixOf" <| fun () ->
// let actual = Matrix.rowSampleCovarianceMatrixOf m
// let expected =
// [|
// [|0.025; 0.0075; 0.00175|]
// [|0.0075; 0.0070; 0.00135|]
// [|0.00175;0.00135;0.00043|]
// |]
// |> matrix
// let difference =
// actual - expected
// |> Matrix.mapiCols (fun i x -> Seq.max x)
// |> Seq.max
// Expect.floatClose Accuracy.high difference 0. "Should be equal (double precision)"
// ]