This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "91e9f0b60d46f187e133fc4ef3ddab21efe6a0c99f34402454088b277db1c318",
|
||||
"stdin": "3 8 1 1 1 2 1 2 1 2 5 0 5 0 7 0 9 0 2 1 0 1 1 4 250000001 2 1000000007 0 1000000013 0 62500001 4 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "4.571428571428571\n0.5\n30.0",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
def variance(x: List[Float64]) -> Float64:
|
||||
var m: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
m = m + x[i]
|
||||
m = m / Float64(len(x))
|
||||
var total: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
total = total + (x[i] - m) * (x[i] - m)
|
||||
return total / Float64(len(x) - 1)
|
||||
@@ -0,0 +1,9 @@
|
||||
def variance(x):
|
||||
m = 0.0
|
||||
for v in x:
|
||||
m += v
|
||||
m /= len(x)
|
||||
total = 0.0
|
||||
for v in x:
|
||||
total += (v - m) * (v - m)
|
||||
return total / (len(x) - 1)
|
||||
@@ -0,0 +1,5 @@
|
||||
import statistics
|
||||
|
||||
|
||||
def variance(x):
|
||||
return statistics.variance(x)
|
||||
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "Variance",
|
||||
"summary": "The sample variance of a list of numbers (dividing by N - 1), computed in two passes.",
|
||||
"tags": [
|
||||
"statistics",
|
||||
"cancellation"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Chan, Golub and LeVeque, Algorithms for computing the sample variance: analysis and recommendations, The American Statistician 37(3), 1983",
|
||||
"url": "https://doi.org/10.1080/00031305.1983.10483115"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "Variance([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])"
|
||||
},
|
||||
{
|
||||
"call": "Variance([1.0, 2.0])"
|
||||
},
|
||||
{
|
||||
"call": "Variance([1.0e9 + 4.0, 1.0e9 + 7.0, 1.0e9 + 13.0, 1.0e9 + 16.0])"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same two passes."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "statistics",
|
||||
"file": "ref/python/statistics.py",
|
||||
"note": "The standard library's statistics.variance, which works with exact fractions inside: a different method, held to agree to 12 digits.",
|
||||
"rel": 1e-12
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same two passes in Mojo."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Variance: the sample variance (dividing by N - 1), in two passes: the mean
|
||||
// first, then the squared distances from it. The one-pass formula
|
||||
// (SUM X^2 - N * MEAN^2) / (N - 1) subtracts two nearly equal large numbers
|
||||
// and loses every digit when the values sit far from 0: on the third case it
|
||||
// gives -170.66666666666666 in FLOAT arithmetic, where the two passes give 30.0.
|
||||
USE stats.Mean
|
||||
FUNCTION Variance(X : LIST OF FLOAT) RETURNS FLOAT
|
||||
REQUIRE LENGTH(X) >= 2
|
||||
DECLARE M : FLOAT <- Mean(X)
|
||||
DECLARE Total : FLOAT <- 0.0
|
||||
FOR I <- 0 TO LENGTH(X)
|
||||
Total <- Total + (X[I] - M) * (X[I] - M)
|
||||
NEXT I
|
||||
RETURN Total / (LENGTH(X) - 1)
|
||||
ENDFUNCTION
|
||||
Reference in new issue
Block a user