Skip to content

Commit

Permalink
add correlation test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmal committed Apr 26, 2024
1 parent c0c7778 commit 2eb55cb
Showing 1 changed file with 240 additions and 0 deletions.
240 changes: 240 additions & 0 deletions tests/FSharp.Stats.Tests/Correlation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,246 @@ open System
open FSharp.Stats.Correlation
open Expecto

module TestData =
let doubles =
[
{|
X = [| 0.769975279369337; -0.26975129370715756; -0.22164107602804684; -0.37964372892225584; 1.7976931348623157E+308; 0.6956489946628831; 0.8498674478461568; 0.007870060694074144 |]
Y = [| 8.05529523804792; -9.648443925108909; -1.215500483344818; 5E-324; -4.337558555754166; infinity; -7.497611995486394; -9.039643739188005 |]
Spearman = 0.09523809523809525
KendallA = 0.07142857142857142
KendallB = 0.07142857142857142
KendallC = 0.07142857142857142
Pearson = nan
|}
{|
X = [| -1.3946407056008117; -1.7976931348623157E+308; 0.02665139354486956; 0.16752887114290516; 0.6510630080261284 |]
Y = [| -5.934146660251358; -7.514325777080982; -2.869708043284536; -0.6743782342678939; -2.2164107602804686 |]
Spearman = 0.9
KendallA = 0.8
KendallB = 0.8
KendallC = 0.8
Pearson = nan //R returns 0.0
|}
{|
X = [| -infinity; 3.2160411307302565 |]
Y = [| -3.8511452553484538; -5.393177399524884 |]
Spearman = -1.0
KendallA = -1.0
KendallB = -1.0
KendallC = -1.0
Pearson = nan
|}
{|
X = [| 5E-324; 0.4310933883901359; 1.1782225200518512; 4.490557012680512; -infinity; -0.05931977813647893 |]
Y = [| -1.7431196366262147; -3.3100232065058477; -infinity; 6.432082261460513; 8.025230948524591; 5E-324 |]
Spearman = -0.42857142857142855
KendallA = -0.4666666666666667
KendallB = -0.4666666666666667
KendallC = -0.4666666666666667
Pearson = nan
|}
{|
X = [| -0.6237678376055525; -0.02398140791055825; -0.33238783674585126; 5E-324; -0.9617738169271464; -0.6402018172171572; -0.7944049915885085 |]
Y = [| 7.487700704756412; 2.882382571594094; 0.6608761209968983; -1.7976931348623157E+308; 3.7699648024572516; -5.349991331399306; -6.943140018463384 |]
Spearman = -0.28571428571428564
KendallA = -0.14285714285714285
KendallB = -0.14285714285714285
KendallC = -0.14285714285714285
Pearson = nan // R returns -0.5693503001745431
|}
{|
X = [| infinity; -0.4380145079632394; 0.2525563106400899; -0.7097994161043718; -infinity; 0.6891193732603421; -1.7976931348623157E+308; 3.3026058744137248 |]
Y = [| -4.619190203598879; -6.830939838589383; 4.262013366906972; -1.719153567018289; -5.8337600091398345; 3.631337095047412; 1.7976931348623157E+308; 1.7976931348623157E+308 |]
Spearman = 0.17964393928698885
KendallA = 0.10714285714285714
KendallB = 0.10910894511799618
KendallC = 0.109375
Pearson = nan
|}

{|
X = [| -1.0; 1.0; -3.0; 0.0; 0.0; 2.0; -2.0 |]
Y = [| -3.0; -3.0; 0.0; 2.0; -2.0; -2.0; 1.0 |]
Spearman = -0.35781322366606727
KendallA = -0.19047619047619047
KendallB = -0.20519567041703082
KendallC = -0.20408163265306123
Pearson = -0.43649077143553344
|}
{|
X = [| 1.0; 3.0; 3.0; -1.0 |]
Y = [| 3.0; -1.0; -1.0; 1.0 |]
Spearman = -0.7777777777777779
KendallA = -0.5
KendallB = -0.5999999999999999
KendallC = -0.5625
Pearson = -0.6363636363636365
|}
{|
X = [| 0.0; 2.0; -2.0; 1.0; 1.0; 3.0; -1.0; 2.0 |]
Y = [| -2.0; -2.0; 1.0; 3.0; 3.0; -1.0; 2.0; -3.0 |]
Spearman = -0.3719512195121951
KendallA = -0.17857142857142858
KendallB = -0.19230769230769235
KendallC = -0.1875
Pearson = -0.41619003555011974
|}
{|
X = [| 2.0; -3.0; -3.0; 0.0; 3.0 |]
Y = [| -3.0; 0.0; 0.0; 2.0; -2.0 |]
Spearman = -0.5789473684210528
KendallA = -0.3
KendallB = -0.3333333333333334
KendallC = -0.32
Pearson = -0.5823356699841468
|}
{|
X = [| 1.0; 3.0; -1.0; 2.0; 2.0; -2.0; 0.0; 3.0; 3.0 |]
Y = [| -1.0; -1.0; 2.0; -3.0; -3.0; 0.0; 3.0; -2.0; -2.0 |]
Spearman = -0.6293337301361106
KendallA = -0.3611111111111111
KendallB = -0.40004734568283135
KendallC = -0.3851851851851852
Pearson = -0.6851039625605218
|}
{|
X = [| 3.0; -2.0; -2.0; 1.0; -3.0; -3.0 |]
Y = [| -2.0; 1.0; 1.0; 3.0; -1.0; 2.0 |]
Spearman = -0.14927035850663303
KendallA = -0.06666666666666667
KendallB = -0.07412493166611012
KendallC = -0.07407407407407407
Pearson = -0.2631174057921088
|}
{|
X = [| 2.0; -3.0; 0.0 |]
Y = [| -3.0; 0.0; 3.0 |]
Spearman = -0.5
KendallA = -0.3333333333333333
KendallB = -0.33333333333333337
KendallC = -0.3333333333333333
Pearson = -0.39735970711951313
|}
{|
X = [| -3.0; -1.0; -1.0; 2.0; -2.0; -2.0; 0.0 |]
Y = [| -1.0; 2.0; 2.0; -3.0; 0.0; 3.0; 3.0 |]
Spearman = 0.00925925925925926
KendallA = 0.09523809523809523
KendallB = 0.10526315789473686
KendallC = 0.10204081632653061
Pearson = -0.3150360061726043
|}
{|
X = [| 2.0; 2.0; -1.0 |]
Y = [| -2.0; 1.0; -3.0 |]
Spearman = 0.8660254037844387
KendallA = 0.6666666666666666
KendallB = 0.8164965809277261
KendallC = 0.8888888888888888
Pearson = 0.6933752452815365
|}
{|
X = [| -3.0; 1.0; 0.0; -2.0 |]
Y = [| -2.0; 0.0; 2.0; 2.0 |]
Spearman = 0.31622776601683794
KendallA = 0.16666666666666666
KendallB = 0.18257418583505536
KendallC = 0.1875
Pearson = 0.3813850356982369
|}
]
let ints =
[
{|
X = [| 1; 3; -1; 2; 2; -2; 0; 3; 3|]
Y = [| -1; -1; 2; -3; -3; 0; 3; -2; -2|]
Spearman = -0.6293337301361106
KendallA = -0.3611111111111111
KendallB = -0.40004734568283135
KendallC = -0.3851851851851852
Pearson = -0.6851039625605218
|}
{|
X = [| 3; -2; -2; 1; -3; -3|]
Y = [| -2; 1; 1; 3; -1; 2|]
Spearman = -0.14927035850663303
KendallA = -0.06666666666666667
KendallB = -0.07412493166611012
KendallC = -0.07407407407407407
Pearson = -0.2631174057921088
|}
{|
X = [| 2; -3; 0|]
Y = [| -3; 0; 3|]
Spearman = -0.5
KendallA = -0.3333333333333333
KendallB = -0.33333333333333337
KendallC = -0.3333333333333333
Pearson = -0.39735970711951313
|}
{|
X = [| -3; -1; -1; 2; -2; -2; 0|]
Y = [| -1; 2; 2; -3; 0; 3; 3|]
Spearman = 0.00925925925925926
KendallA = 0.09523809523809523
KendallB = 0.10526315789473686
KendallC = 0.10204081632653061
Pearson = -0.3150360061726043
|}
{|
X = [| 2; 2; -1|]
Y = [| -2; 1; -3|]
Spearman = 0.8660254037844387
KendallA = 0.6666666666666666
KendallB = 0.8164965809277261
KendallC = 0.8888888888888888
Pearson = 0.6933752452815365
|}
{|
X = [| -3; 1; 0; -2|]
Y = [| -2; 0; 2; 2|]
Spearman = 0.31622776601683794
KendallA = 0.16666666666666666
KendallB = 0.18257418583505536
KendallC = 0.1875
Pearson = 0.3813850356982369
|}
]

let inline makeTestList listName caseName corr prop cases =
let getX x = ( ^a : (member X : ^t[]) x)
let getY x = ( ^a : (member Y : ^t[]) x)
cases
|> List.mapi
(fun i x ->
let i = i + 1
[
testCase $"{caseName} Case {i}" <| fun () ->
let corr = corr (getX x) (getY x)
if Double.IsNaN (prop x) then
Expect.isTrue (Double.IsNaN corr) "Should be equal (double precision)"
else
Expect.floatClose Accuracy.high corr (prop x) "Should be equal (double precision)"
]
)
|> List.concat
|> testList $"Correlation.Seq.{listName}"

[<Tests>]
let kendallTauBDoubles = TestData.doubles |> makeTestList "kendall" "Double" Seq.kendall (fun x -> x.KendallB)
[<Tests>]
let kendallTauBInts = TestData.ints |> makeTestList "kendall" "Int" Seq.kendall (fun x -> x.KendallB)
[<Tests>]
let pearsonDoubles = TestData.doubles |> makeTestList "pearson" "Double" Seq.pearson (fun x -> x.Pearson)
[<Tests>]
let pearsonInts = TestData.ints |> makeTestList "pearson" "Int" Seq.pearson (fun x -> x.Pearson)
[<Tests>]
let spearmanDoubles = TestData.doubles |> makeTestList "spearman" "Double" Seq.spearman (fun x -> x.Spearman)
[<Tests>]
let spearmanInts = TestData.ints |> makeTestList "spearman" "Int" Seq.spearman (fun x -> x.Spearman)


[<Tests>]
let kendallCorrelationTests =
// tested with R Kendall(x,y) function
Expand Down

0 comments on commit 2eb55cb

Please sign in to comment.