2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,4 +1,4 @@
|
|||
Statistics is all about large groups of numbers.
|
||||
[[Statistics|Statistics]] is all about large groups of numbers.
|
||||
When talking about a set of sampled data, most frequently used is their [[wp:Mean|mean value]] and [[wp:Standard_deviation|standard deviation (stddev)]].
|
||||
If you have set of data <math>x_i</math> where <math>i = 1, 2, \ldots, n\,\!</math>, the mean is <math>\bar{x}\equiv {1\over n}\sum_i x_i</math>, while the stddev is <math>\sigma\equiv\sqrt{{1\over n}\sum_i \left(x_i - \bar x \right)^2}</math>.
|
||||
|
||||
|
|
@ -23,3 +23,11 @@ Or, more verbosely:
|
|||
:<math>
|
||||
\frac{1}{N}\sum_{i=1}^N(x_i-\overline{x})^2 = \frac{1}{N} \left(\sum_{i=1}^N x_i^2\right) - \overline{x}^2.
|
||||
</math>
|
||||
|
||||
{{task heading|See also}}
|
||||
|
||||
* [[Statistics/Normal_distribution|Statistics/Normal distribution]]
|
||||
|
||||
{{Related tasks/Statistical measures}}
|
||||
|
||||
<br><hr>
|
||||
|
|
|
|||
48
Task/Statistics-Basic/C++/statistics-basic.cpp
Normal file
48
Task/Statistics-Basic/C++/statistics-basic.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <iostream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
void printStars ( int number ) {
|
||||
if ( number > 0 ) {
|
||||
for ( int i = 0 ; i < number + 1 ; i++ )
|
||||
std::cout << '*' ;
|
||||
}
|
||||
std::cout << '\n' ;
|
||||
}
|
||||
|
||||
int main( int argc , char *argv[] ) {
|
||||
const int numberOfRandoms = std::atoi( argv[1] ) ;
|
||||
std::random_device rd ;
|
||||
std::mt19937 gen( rd( ) ) ;
|
||||
std::uniform_real_distribution<> distri( 0.0 , 1.0 ) ;
|
||||
std::vector<double> randoms ;
|
||||
for ( int i = 0 ; i < numberOfRandoms + 1 ; i++ )
|
||||
randoms.push_back ( distri( gen ) ) ;
|
||||
std::sort ( randoms.begin( ) , randoms.end( ) ) ;
|
||||
double start = 0.0 ;
|
||||
for ( int i = 0 ; i < 9 ; i++ ) {
|
||||
double to = start + 0.1 ;
|
||||
int howmany = std::count_if ( randoms.begin( ) , randoms.end( ),
|
||||
[&start , &to] ( double c ) { return c >= start
|
||||
&& c < to ; } ) ;
|
||||
if ( start == 0.0 ) //double 0.0 output as 0
|
||||
std::cout << "0.0" << " - " << to << ": " ;
|
||||
else
|
||||
std::cout << start << " - " << to << ": " ;
|
||||
if ( howmany > 50 ) //scales big interval numbers to printable length
|
||||
howmany = howmany / ( howmany / 50 ) ;
|
||||
printStars ( howmany ) ;
|
||||
start += 0.1 ;
|
||||
}
|
||||
double mean = std::accumulate( randoms.begin( ) , randoms.end( ) , 0.0 ) / randoms.size( ) ;
|
||||
double sum = 0.0 ;
|
||||
for ( double num : randoms )
|
||||
sum += std::pow( num - mean , 2 ) ;
|
||||
double stddev = std::pow( sum / randoms.size( ) , 0.5 ) ;
|
||||
std::cout << "The mean is " << mean << " !" << std::endl ;
|
||||
std::cout << "Standard deviation is " << stddev << " !" << std::endl ;
|
||||
return 0 ;
|
||||
}
|
||||
27
Task/Statistics-Basic/Elixir/statistics-basic.elixir
Normal file
27
Task/Statistics-Basic/Elixir/statistics-basic.elixir
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
defmodule Statistics do
|
||||
def basic(n) do
|
||||
{sum, sum2, hist} = generate(n)
|
||||
mean = sum / n
|
||||
stddev = :math.sqrt(sum2 / n - mean*mean)
|
||||
|
||||
IO.puts "size: #{n}"
|
||||
IO.puts "mean: #{mean}"
|
||||
IO.puts "stddev: #{stddev}"
|
||||
Enum.each(0..9, fn i ->
|
||||
:io.fwrite "~.1f:~s~n", [0.1*i, String.duplicate("=", trunc(500 * hist[i] / n))]
|
||||
end)
|
||||
IO.puts ""
|
||||
end
|
||||
|
||||
defp generate(n) do
|
||||
hist = for i <- 0..9, into: %{}, do: {i,0}
|
||||
Enum.reduce(1..n, {0, 0, hist}, fn _,{sum, sum2, h} ->
|
||||
r = :rand.uniform
|
||||
{sum+r, sum2+r*r, Map.update!(h, trunc(10*r), &(&1+1))}
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each([100,1000,10000], fn n ->
|
||||
Statistics.basic(n)
|
||||
end)
|
||||
48
Task/Statistics-Basic/Haskell/statistics-basic.hs
Normal file
48
Task/Statistics-Basic/Haskell/statistics-basic.hs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
{-# LANGUAGE BangPatterns #-}
|
||||
|
||||
import Data.Foldable
|
||||
import System.Random
|
||||
import System.Environment (getArgs)
|
||||
|
||||
intervals :: [(Double,Double)]
|
||||
intervals = map conv [0..9]
|
||||
where xs = [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
|
||||
conv s = let { [h,l] = take 2 $ drop s xs } in (h,l)
|
||||
|
||||
count :: [Double] -> [Int]
|
||||
count rands = map (\iv -> foldl' (loop iv) 0 rands) intervals
|
||||
where loop :: (Double,Double) -> Int -> Double -> Int
|
||||
loop (lo,hi) n x | lo <= x && x < hi = n+1
|
||||
| otherwise = n
|
||||
-- ^ fuses length and filter within (lo,hi)
|
||||
|
||||
data Pair a b = Pair !a !b
|
||||
|
||||
-- accumulate sum and length in one fold
|
||||
sumLen :: [Double] -> Pair Double Double
|
||||
sumLen = fion2 . foldl' (\(Pair s l) x -> Pair (s+x) (l+1)) (Pair 0.0 0)
|
||||
where fion2 :: Pair Double Int -> Pair Double Double
|
||||
fion2 (Pair s l) = Pair s (fromIntegral l)
|
||||
|
||||
-- safe division on pairs
|
||||
divl :: Pair Double Double -> Double
|
||||
divl (Pair _ 0.0) = 0.0
|
||||
divl (Pair s l) = s / l
|
||||
|
||||
-- sumLen and divl are separate for stddev below
|
||||
mean :: [Double] -> Double
|
||||
mean = divl . sumLen
|
||||
|
||||
stddev :: [Double] -> Double
|
||||
stddev xs = sqrt $ foldl' (\s x -> s+(x-m)^2) 0 xs / l
|
||||
where p@(Pair s l) = sumLen xs
|
||||
m = divl p
|
||||
|
||||
main = do nr <- read.head <$> getArgs
|
||||
rands <- take nr . randomRs (0.0,1.0) <$> newStdGen
|
||||
putStrLn $ "The mean is " ++ show (mean rands) ++ " !"
|
||||
putStrLn $ "The standard deviation is " ++ show (stddev rands) ++ " !"
|
||||
zipWithM_ (\iv fq -> putStrLn $ ivstr iv ++ ": " ++ fqstr fq) intervals (count rands)
|
||||
where
|
||||
fqstr i = replicate (if i > 50 then div i (div i 50) else i) '*'
|
||||
ivstr (lo,hi) = show lo ++ " - " ++ show hi
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
require'statfns'
|
||||
(mean,stddev) ?1000#0
|
||||
require 'stats'
|
||||
(mean,stddev) 1000 ?@$ 0
|
||||
0.484669 0.287482
|
||||
(mean,stddev) ?10000#0
|
||||
(mean,stddev) 10000 ?@$ 0
|
||||
0.503642 0.290777
|
||||
(mean,stddev) ?100000#0
|
||||
(mean,stddev) 100000 ?@$ 0
|
||||
0.499677 0.288726
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
|
||||
require'plot'
|
||||
plot ((%*1+i.)100) ([;histogram) ?10000#0
|
||||
plot ((% * 1 + i.)100) ([;histogram) 10000 ?@$ 0
|
||||
|
|
|
|||
|
|
@ -1,23 +1,24 @@
|
|||
histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
|
||||
|
||||
meanstddevP=:3 :0
|
||||
meanstddevP=: 3 :0
|
||||
NB. compute mean and std dev of y random numbers
|
||||
NB. picked from even distribution between 0 and 1
|
||||
NB. and display a normalized ascii histogram for this sample
|
||||
NB. note: should use population mean, not sample mean, for stddev
|
||||
NB. note: uses population mean (0.5), not sample mean, for stddev
|
||||
NB. given the equation specified for this task.
|
||||
h=.s=.t=. 0
|
||||
buckets=. (%~1+i.)10
|
||||
for_n.i.<.y%1e6 do.
|
||||
data=. ?1e6#0
|
||||
h=.h+ buckets histogram data
|
||||
s=.s+ +/ data
|
||||
t=.t+ +/(data-0.5)^2
|
||||
chunk=. 1e6
|
||||
bins=. (%~ 1 + i.) 10
|
||||
for. i. <.y%chunk do.
|
||||
data=. chunk ?@$ 0
|
||||
h=. h+ bins histogram data
|
||||
s=. s+ +/ data
|
||||
t=. t+ +/ *: data-0.5
|
||||
end.
|
||||
data=. ?(1e6|y)#0
|
||||
h=.h+ buckets histogram data
|
||||
s=.s+ +/ data
|
||||
t=.t++/(data-0.5)^2
|
||||
smoutput (<.300*h%y)#"0'#'
|
||||
(s%y),%:t%y
|
||||
data=. (chunk|y) ?@$ 0
|
||||
h=. h+ bins histogram data
|
||||
s=. s+ +/ data
|
||||
t=. t+ +/ *: data - 0.5
|
||||
smoutput (<.300*h%y) #"0 '#'
|
||||
(s%y) , %:t%y
|
||||
)
|
||||
|
|
|
|||
52
Task/Statistics-Basic/Java/statistics-basic.java
Normal file
52
Task/Statistics-Basic/Java/statistics-basic.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import static java.lang.Math.pow;
|
||||
import static java.util.Arrays.stream;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
public class Test {
|
||||
static double[] meanStdDev(double[] numbers) {
|
||||
if (numbers.length == 0)
|
||||
return new double[]{0.0, 0.0};
|
||||
|
||||
double sx = 0.0, sxx = 0.0;
|
||||
long n = 0;
|
||||
for (double x : numbers) {
|
||||
sx += x;
|
||||
sxx += pow(x, 2);
|
||||
n++;
|
||||
}
|
||||
return new double[]{sx / n, pow((n * sxx - pow(sx, 2)), 0.5) / n};
|
||||
}
|
||||
|
||||
static String replicate(int n, String s) {
|
||||
return range(0, n + 1).mapToObj(i -> s).collect(joining());
|
||||
}
|
||||
|
||||
static void showHistogram01(double[] numbers) {
|
||||
final int maxWidth = 50;
|
||||
long[] bins = new long[10];
|
||||
|
||||
for (double x : numbers)
|
||||
bins[(int) (x * bins.length)]++;
|
||||
|
||||
double maxFreq = stream(bins).max().getAsLong();
|
||||
|
||||
for (int i = 0; i < bins.length; i++)
|
||||
System.out.printf(" %3.1f: %s%n", i / (double) bins.length,
|
||||
replicate((int) (bins[i] / maxFreq * maxWidth), "*"));
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] a) {
|
||||
Locale.setDefault(Locale.US);
|
||||
for (int p = 1; p < 7; p++) {
|
||||
double[] n = range(0, (int) pow(10, p))
|
||||
.mapToDouble(i -> Math.random()).toArray();
|
||||
|
||||
System.out.println((int)pow(10, p) + " numbers:");
|
||||
double[] res = meanStdDev(n);
|
||||
System.out.printf(" Mean: %8.6f, SD: %8.6f%n", res[0], res[1]);
|
||||
showHistogram01(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
math.randomseed(os.time())
|
||||
math.random() -- First number after seeding not random - throw one away
|
||||
|
||||
function randList (n) -- Build table of size n
|
||||
local numbers = {}
|
||||
|
|
|
|||
5
Task/Statistics-Basic/Maple/statistics-basic-1.maple
Normal file
5
Task/Statistics-Basic/Maple/statistics-basic-1.maple
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
with(Statistics):
|
||||
X_100 := Sample( Uniform(0,1), 100 );
|
||||
Mean( X_100 );
|
||||
StandardDeviation( X_100 );
|
||||
Histogram( X_100 );
|
||||
9
Task/Statistics-Basic/Maple/statistics-basic-2.maple
Normal file
9
Task/Statistics-Basic/Maple/statistics-basic-2.maple
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
sample := proc( n )
|
||||
local data;
|
||||
data := Sample( Uniform(0,1), n );
|
||||
printf( "Mean: %.4f\nStandard Deviation: %.4f",
|
||||
Statistics:-Mean( data ),
|
||||
Statistics:-StandardDeviation( data ) );
|
||||
return Statistics:-Histogram( data );
|
||||
end proc:
|
||||
sample( 1000 );
|
||||
|
|
@ -1,35 +1,34 @@
|
|||
/*REXX pgm gens some random numbers, shows bin histogram, finds mean & stdDev.*/
|
||||
numeric digits 20 /*use twenty decimal digits precision, */
|
||||
showDigs=digits()%2 /* ··· but only show ten decimal digits*/
|
||||
parse arg size seed . /*allow specification: size, and seed.*/
|
||||
if size=='' | size==',' then size=100 /*Not specified? Then use the default.*/
|
||||
if datatype(seed,'W') then call random ,,seed /*allow a seed for RAND BIF.*/
|
||||
#.=0 /*count of the numbers in each bin. */
|
||||
do j=1 for size /*generate some random numbers. */
|
||||
@.j=random(0,99999)/100000 /*express it as a fraction. */
|
||||
_=substr(@.j'00',3,1) /*determine which bin the number is in,*/
|
||||
#._=#._+1 /* ··· and bump its count. */
|
||||
/*REXX program generates some random numbers, shows bin histogram, finds mean & stdDev. */
|
||||
numeric digits 20 /*use twenty decimal digits precision, */
|
||||
showDigs=digits()%2 /* ··· but only show ten decimal digits*/
|
||||
parse arg size seed . /*allow specification: size, and seed.*/
|
||||
if size=='' | size=="," then size=100 /*Not specified? Then use the default.*/
|
||||
if datatype(seed,'W') then call random ,,seed /*allow a seed for the RANDOM BIF. */
|
||||
#.=0 /*count of the numbers in each bin. */
|
||||
do j=1 for size /*generate some random numbers. */
|
||||
@.j=random(0, 99999) / 100000 /*express random number as a fraction. */
|
||||
_=substr(@.j'00', 3, 1) /*determine which bin the number is in,*/
|
||||
#._=#._+1 /* ··· and bump its count. */
|
||||
end /*j*/
|
||||
|
||||
do k=0 for 10 /*show a histogram of the bins. */
|
||||
lr='0.'k ; if k==0 then lr='0 ' /*adjust for the low range.*/
|
||||
hr='0.'||(k+1); if k==9 then hr='1 ' /* " " " high range.*/
|
||||
range=lr"──►"hr' ' /*construct the range. */
|
||||
barPC=right(strip(left(format(100*#.k/size,,2),5)),5) /*comp %.*/
|
||||
say range barPC copies('─',format(barPC*1,,0)) /*histo. */
|
||||
do k=0 for 10 /*show a histogram of the bins. */
|
||||
lr='0.'k ; if k==0 then lr="0 " /*adjust for the low range. */
|
||||
hr='0.'||(k+1); if k==9 then hr="1 " /* " " " high range. */
|
||||
range=lr"──►"hr' ' /*construct the range. */
|
||||
barPC=right(strip(left(format(100*#.k/size, , 2), 5)) ,5) /*compute the %. */
|
||||
say range barPC copies('─', format(barPC*1, , 0)) /*display histogram*/
|
||||
end /*k*/
|
||||
say
|
||||
say 'sample size = ' size; say
|
||||
avg=mean(size) ; say ' mean = ' format(avg,,showDigs)
|
||||
std=stdDev(size); say ' stdDev = ' format(std,,showDigs)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
mean: parse arg N; $=0; do m=1 for N; $=$+@.m; end; return $/n
|
||||
stdDev: parse arg N; $=0; do s=1 for N; $=$+(@.s-avg)**2; end; return sqrt($/n)
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
|
||||
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
numeric digits d; return (g/1)i /*make complex if X < 0.*/
|
||||
say 'sample size = ' size; say
|
||||
avg= mean(size) ; say ' mean = ' format(avg, , showDigs)
|
||||
std=stdDev(size) ; say ' stdDev = ' format(std, , showDigs)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mean: parse arg N; $=0; do m=1 for N; $=$+@.m; end; return $/n
|
||||
stdDev: parse arg N; $=0; do s=1 for N; $=$+(@.s-avg)**2; end; return sqrt($/n)
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
|
||||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_ % 2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
return g/1
|
||||
|
|
|
|||
42
Task/Statistics-Basic/Run-BASIC/statistics-basic.run
Normal file
42
Task/Statistics-Basic/Run-BASIC/statistics-basic.run
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
call sample 100
|
||||
call sample 1000
|
||||
call sample 10000
|
||||
|
||||
end
|
||||
|
||||
sub sample n
|
||||
dim samp(n)
|
||||
for i =1 to n
|
||||
samp(i) =rnd(1)
|
||||
next i
|
||||
|
||||
' calculate mean, standard deviation
|
||||
sum = 0
|
||||
sumSq = 0
|
||||
for i = 1 to n
|
||||
sum = sum + samp(i)
|
||||
sumSq = sumSq + samp(i)^2
|
||||
next i
|
||||
print n; " Samples used."
|
||||
|
||||
mean = sum / n
|
||||
print "Mean = "; mean
|
||||
|
||||
print "Std Dev = "; (sumSq /n -mean^2)^0.5
|
||||
|
||||
'------- Show histogram
|
||||
bins = 10
|
||||
dim bins(bins)
|
||||
for i = 1 to n
|
||||
z = int(bins * samp(i))
|
||||
bins(z) = bins(z) +1
|
||||
next i
|
||||
for b = 0 to bins -1
|
||||
print b;" ";
|
||||
for j = 1 to int(bins *bins(b)) /n *70
|
||||
print "*";
|
||||
next j
|
||||
print
|
||||
next b
|
||||
print
|
||||
end sub
|
||||
70
Task/Statistics-Basic/Rust/statistics-basic.rust
Normal file
70
Task/Statistics-Basic/Rust/statistics-basic.rust
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#![feature(iter_arith)]
|
||||
extern crate rand;
|
||||
|
||||
use rand::distributions::{IndependentSample, Range};
|
||||
|
||||
pub fn mean(data: &[f32]) -> Option<f32> {
|
||||
if data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let sum: f32 = data.iter().sum();
|
||||
Some(sum / data.len() as f32)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn variance(data: &[f32]) -> Option<f32> {
|
||||
if data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let mean = mean(data).unwrap();
|
||||
let mut sum = 0f32;
|
||||
for &x in data {
|
||||
sum += (x - mean).powi(2);
|
||||
}
|
||||
Some(sum / data.len() as f32)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn standard_deviation(data: &[f32]) -> Option<f32> {
|
||||
if data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let variance = variance(data).unwrap();
|
||||
Some(variance.sqrt())
|
||||
}
|
||||
}
|
||||
|
||||
fn print_histogram(width: u32, data: &[f32]) {
|
||||
let mut histogram = [0; 10];
|
||||
let len = histogram.len() as f32;
|
||||
for &x in data {
|
||||
histogram[(x * len) as usize] += 1;
|
||||
}
|
||||
let max_frequency = *histogram.iter().max().unwrap() as f32;
|
||||
for (i, &frequency) in histogram.iter().enumerate() {
|
||||
let bar_width = frequency as f32 * width as f32 / max_frequency;
|
||||
print!("{:3.1}: ", i as f32 / len);
|
||||
for _ in 0..bar_width as usize {
|
||||
print!("*");
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let range = Range::new(0f32, 1f32);
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
for &number_of_samples in [1000, 10_000, 1_000_000].iter() {
|
||||
let mut data = vec![];
|
||||
for _ in 0..number_of_samples {
|
||||
let x = range.ind_sample(&mut rng);
|
||||
data.push(x);
|
||||
}
|
||||
println!(" Statistics for sample size {}", number_of_samples);
|
||||
println!("Mean: {:?}", mean(&data));
|
||||
println!("Variance: {:?}", variance(&data));
|
||||
println!("Standard deviation: {:?}", standard_deviation(&data));
|
||||
print_histogram(40, &data);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue