From 68d724a008b9b0f27dfa21a99c4d62b52caf630b Mon Sep 17 00:00:00 2001 From: omaus Date: Wed, 26 Jan 2022 23:22:38 +0100 Subject: [PATCH 1/2] Add Kolmogorov-Smirnov test :construction: --- src/FSharp.Stats/FSharp.Stats.fsproj | 1 + src/FSharp.Stats/Testing/KolmogorovSmirnov.fs | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/FSharp.Stats/Testing/KolmogorovSmirnov.fs diff --git a/src/FSharp.Stats/FSharp.Stats.fsproj b/src/FSharp.Stats/FSharp.Stats.fsproj index fd456944..d6271959 100644 --- a/src/FSharp.Stats/FSharp.Stats.fsproj +++ b/src/FSharp.Stats/FSharp.Stats.fsproj @@ -115,6 +115,7 @@ + diff --git a/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs b/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs new file mode 100644 index 00000000..2316bbaa --- /dev/null +++ b/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs @@ -0,0 +1,44 @@ +namespace FSharp.Stats.Testing + +open FSharp.Stats.Distributions + +module KolmogorovSmirnov = + + /// Tests the input values with the Kolmogorov-Smirnov Goodness of Fit-test against a normal distribution and returns a p-Value. + /// The p-value indicates how likely it is to observe such a distribution under the assumption that the null hypothesis is true (= sample distribution normally distributed). + /// In short: The lower the p-value, the more likely it is that the sample does NOT originate from a normal distribution. + let goF values = + let lVals = Array.length values |> float + let rankedVals = Array.sort values + let rankedNormVals = rankedVals |> Array.mapi (fun i _ -> float i / lVals) + let meanVals = FSharp.Stats.Seq.mean values + let stdVals = FSharp.Stats.Seq.stDev values + let nD = FSharp.Stats.Distributions.Continuous.normal meanVals stdVals + let cum = rankedVals |> Array.map (fun v -> nD.CDF v) + let deltaCumVals = (cum, rankedNormVals) ||> Array.map2 (fun c v -> System.Math.Abs (c - v)) + let kS = Array.max deltaCumVals + kS + //Continuous.kolmogorov + // let z = kS * (System.Math.Sqrt()) + // let kolmoDist x = (System.Math.Sqrt (2. * System.Math.PI) / x) * + +module Distributions = + + // TO DO + let cdfKolmogorov x = + let fraction = (System.Math.Sqrt(2. * System.Math.PI)) / x + printfn "fraction: %f" fraction + let eFractionDenominator = 8. * x ** 2. + printfn "eFractionDenominator: %f" eFractionDenominator + let rec getSum acc k n = + printfn "k: %f" k + let eFractionNumerator = -(2. * k - 1.) ** 2. * System.Math.PI ** 2. + printfn "eFractionNumerator: %f" eFractionNumerator + let newAcc = acc + System.Math.E ** (eFractionNumerator / eFractionDenominator) + printfn "newAcc: %f" newAcc + if k < n then getSum newAcc (k + 1.) n + else newAcc + let sum = getSum 0. 1. 1. + fraction * sum + + //cdfKolmogorov 1.3581 \ No newline at end of file From cfc6958b121eb50db609f6ebb5dac0543f014483 Mon Sep 17 00:00:00 2001 From: omaus Date: Fri, 28 Jan 2022 11:44:20 +0100 Subject: [PATCH 2/2] Delete unused/not-working distribution :fire: --- src/FSharp.Stats/Testing/KolmogorovSmirnov.fs | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs b/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs index 2316bbaa..34369635 100644 --- a/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs +++ b/src/FSharp.Stats/Testing/KolmogorovSmirnov.fs @@ -18,27 +18,4 @@ module KolmogorovSmirnov = let deltaCumVals = (cum, rankedNormVals) ||> Array.map2 (fun c v -> System.Math.Abs (c - v)) let kS = Array.max deltaCumVals kS - //Continuous.kolmogorov - // let z = kS * (System.Math.Sqrt()) - // let kolmoDist x = (System.Math.Sqrt (2. * System.Math.PI) / x) * - -module Distributions = - - // TO DO - let cdfKolmogorov x = - let fraction = (System.Math.Sqrt(2. * System.Math.PI)) / x - printfn "fraction: %f" fraction - let eFractionDenominator = 8. * x ** 2. - printfn "eFractionDenominator: %f" eFractionDenominator - let rec getSum acc k n = - printfn "k: %f" k - let eFractionNumerator = -(2. * k - 1.) ** 2. * System.Math.PI ** 2. - printfn "eFractionNumerator: %f" eFractionNumerator - let newAcc = acc + System.Math.E ** (eFractionNumerator / eFractionDenominator) - printfn "newAcc: %f" newAcc - if k < n then getSum newAcc (k + 1.) n - else newAcc - let sum = getSum 0. 1. 1. - fraction * sum - - //cdfKolmogorov 1.3581 \ No newline at end of file + Continuous.kolmogorov \ No newline at end of file