Skip to content

Commit

Permalink
fixes for clang on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Jan 3, 2025
1 parent 8f63c39 commit f30f950
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 11 additions & 5 deletions vlib/math/stats/stats.v
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ pub fn covariance_mean[T](data1 []T, data2 []T, mean1 T, mean2 T) T {
for i in 0 .. n {
delta1 := data1[i] - mean1
delta2 := data2[i] - mean2
covariance += T((delta1 * delta2 - covariance) / (i + T(1)))
covariance += T((delta1 * delta2 - covariance) / (T(i) + T(1)))
}
return covariance
}
Expand All @@ -459,8 +459,11 @@ pub fn lag1_autocorrelation_mean[T](data []T, mean T) T {
for i := 1; i < data.len; i++ {
delta0 := data[i - 1] - mean
delta1 := data[i] - mean
q += T((delta0 * delta1 - q) / (i + T(1)))
v += T((delta1 * delta1 - v) / (T(i) + T(1)))
d01 := delta0 * delta1
d11 := delta1 * delta1
ti1 := T(i) + T(1)
q += T((d01 - q) / ti1)
v += T((d11 - v) / ti1)
}
return T(q / v)
}
Expand All @@ -486,7 +489,9 @@ pub fn kurtosis_mean_stddev[T](data []T, mean T, sd T) T {
*/
for i, v in data {
x := (v - mean) / sd
avg += T((x * x * x * x - avg) / (i + T(1)))
x4 := x * x * x * x
ti1 := (T(i) + T(1))
avg += T((x4 - avg) / ti1)
}
return avg - T(3)
}
Expand All @@ -511,7 +516,8 @@ pub fn skew_mean_stddev[T](data []T, mean T, sd T) T {
*/
for i, v in data {
x := (v - mean) / sd
skew += T((x * x * x - skew) / (i + T(1)))
x3 := x * x * x
skew += T((x3 - skew) / (T(i) + T(1)))
}
return skew
}
Expand Down
17 changes: 11 additions & 6 deletions vlib/math/stats/stats_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,15 @@ fn test_covariance() {
fn test_lag1_autocorrelation() {
mut data := [10.0, 4.45, 5.9, 2.7]
mut o := stats.lag1_autocorrelation(data)
mut e := 0.0
assert math.alike(o, -0.5542285481446095)
data = [-3.0, 67.31, 4.4, 1.89]
o = stats.lag1_autocorrelation(data)
assert math.alike(o, -0.5102510654033415)
data = [12.0, 7.88, 76.122, 54.83]
o = stats.lag1_autocorrelation(data)
assert math.alike(o, 0.10484450460892072)
e = 0.10484450460892072
assert math.veryclose(o, e), diff(o, e)

// test for int, i64, f32 array
assert stats.lag1_autocorrelation[int]([1, 2, 3, 1]) == 0
Expand All @@ -444,7 +446,7 @@ fn test_lag1_autocorrelation() {
}

fn diff(actual f64, expected f64) string {
return '\nactual:\n${actual}\nexpected:\n${expected}'
return '\n actual:${actual:40.35f}\nexpected:${expected:40.35f}\n diff:${actual - expected:40.35f}'
}

fn test_kurtosis() {
Expand All @@ -464,27 +466,30 @@ fn test_kurtosis() {
assert stats.kurtosis[int]([1, 2, 3, 1]) == 1
assert stats.kurtosis[i64]([i64(1), 2, 3, 1]) == 1
o = stats.kurtosis[f32]([f32(1.0), 3, 5, 7, 3])
assert math.alike(o, -1.044378399848938)
e = -1.044378399848938
assert math.alike(o, e), diff(o, e)
}

fn test_skew() {
mut data := [10.0, 4.45, 5.9, 2.7]
mut o := stats.skew(data)
mut e := 0.5754021106320453
assert math.veryclose(o, e), diff(o, e)
assert math.close(o, e), diff(o, e)
data = [-3.0, 67.31, 4.4, 1.89]
o = stats.skew(data)
e = 1.1248733711136492
assert math.veryclose(o, e), diff(o, e)
data = [12.0, 7.88, 76.122, 54.83]
o = stats.skew(data)
assert math.alike(o, 0.19007911706827735)
e = 0.19007911706827735
assert math.veryclose(o, e), diff(o, e)

// test for int, i64, f32 array
assert stats.skew[int]([1, 2, 3, 1]) == 2
assert stats.skew[i64]([i64(1), 2, 3, 1]) == 2
o = stats.skew[f32]([f32(1.0), 3, 5, 7, 3])
assert math.alike(o, 0.27154541015625)
e = 0.27154541015625
assert math.close(o, e), diff(o, e)
}

fn test_quantile() {
Expand Down

0 comments on commit f30f950

Please sign in to comment.