Skip to content

Commit

Permalink
Propagate missing values in Stats.cov from Stats.stdDev. (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
Choc13 authored Nov 18, 2022
1 parent ffa0af9 commit 4436511
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Deedle.Math/Stats.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type Stats =
static member covMatrix (df:Frame<'R, 'C>) =
// Treat nan as zero, the same as MATLAB
let corr = Stats.corrMatrix(df) |> Matrix.map(fun x -> if Double.IsNaN x then 0. else x)
let stdev = df |> Stats.stdDev |> Series.values |> Array.ofSeq
let stdev = df |> Stats.stdDev |> Series.valuesAll |> Seq.map (Option.defaultValue nan) |> Array.ofSeq
let stdevDiag = DenseMatrix.ofDiagArray stdev
stdevDiag * corr * stdevDiag

Expand Down
19 changes: 19 additions & 0 deletions tests/Deedle.Math.Tests/Stats.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ let ``cov2Corr and corr2Cov work`` () =
let actual = Stats.corr2Cov(std, corr).GetColumnAt<float>(0).GetAt(0)
let expected = cov.GetColumnAt<float>(0).GetAt(0)
actual |> should beWithin (expected +/- 1e-6)

[<Test>]
let ``cov propogates missing values when stddev produces missing values`` () =
let returns =
Frame.ofColumns [ "A"
=> series [ DateTime(2022, 11, 1) => 0.
DateTime(2022, 11, 2) => 0. ]
"B" => series [ DateTime(2022, 11, 1) => 0. ] ]

let cov = returns |> Stats.cov

let expected =
Frame.ofRowKeys [ "A"; "B" ]
|> Frame.addCol "A" (series [ "A" => 0. ])
|> Frame.addCol "B" (series [])

cov
|> Frame.toMatrix
|> shouldEqual (expected |> Frame.toMatrix)

0 comments on commit 4436511

Please sign in to comment.