Statistics/Basic: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 38: Line 38:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F sd_mean(numbers)
<syntaxhighlight lang="11l">F sd_mean(numbers)
V mean = sum(numbers) / numbers.len
V mean = sum(numbers) / numbers.len
V sd = (sum(numbers.map(n -> (n - @mean) ^ 2)) / numbers.len) ^ 0.5
V sd = (sum(numbers.map(n -> (n - @mean) ^ 2)) / numbers.len) ^ 0.5
Line 59: Line 59:
V (sd, mean) = sd_mean(n)
V (sd, mean) = sd_mean(n)
print(‘ sd: #.6, mean: #.6’.format(sd, mean))
print(‘ sd: #.6, mean: #.6’.format(sd, mean))
histogram(n)</lang>
histogram(n)</syntaxhighlight>


{{out}}
{{out}}
Line 102: Line 102:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"


DEFINE SIZE="10000"
DEFINE SIZE="10000"
Line 228: Line 228:
PutE() PutE()
PutE() PutE()
Test(10000)
Test(10000)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Statistics_basic.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Statistics_basic.png Screenshot from Atari 8-bit computer]
Line 261: Line 261:
===A plain solution for moderate sample sizes===
===A plain solution for moderate sample sizes===


<lang Ada>with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
Ada.Numerics.Generic_Elementary_Functions;
Ada.Numerics.Generic_Elementary_Functions;


Line 323: Line 323:
TIO.New_Line;
TIO.New_Line;
Put_Mean_Et_Al(Sample_Size => N, Val_Sum => Val_Sum, Square_Sum => Squ_Sum);
Put_Mean_Et_Al(Sample_Size => N, Val_Sum => Val_Sum, Square_Sum => Squ_Sum);
end Basic_Stat;</lang>
end Basic_Stat;</syntaxhighlight>


{{out}} from a few sample runs:
{{out}} from a few sample runs:
Line 399: Line 399:
This is the modified program
This is the modified program


<lang Ada>with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
Ada.Numerics.Generic_Elementary_Functions;
Ada.Numerics.Generic_Elementary_Functions;


Line 475: Line 475:
Put_Mean_Et_Al(Sample_Size => N,
Put_Mean_Et_Al(Sample_Size => N,
Val_Sum => Float(Val_Sum), Square_Sum => Float(Squ_Sum));
Val_Sum => Float(Val_Sum), Square_Sum => Float(Squ_Sum));
end Long_Basic_Stat;</lang>
end Long_Basic_Stat;</syntaxhighlight>


{{out}} for sample size 10^12 took one night on my PC:
{{out}} for sample size 10^12 took one night on my PC:
Line 561: Line 561:
=={{header|C}}==
=={{header|C}}==
Sample code.
Sample code.
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <math.h>
#include <math.h>
Line 662: Line 662:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{libheader|Math.Net}}
{{libheader|Math.Net}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using MathNet.Numerics.Statistics;
using MathNet.Numerics.Statistics;


Line 697: Line 697:
Run(10000);
Run(10000);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Sample size: 100
<pre>Sample size: 100
Line 742: Line 742:


=={{header|C++}}==
=={{header|C++}}==
<lang Cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <random>
#include <random>
#include <vector>
#include <vector>
Line 789: Line 789:
std::cout << "Standard deviation is " << stddev << " !" << std::endl ;
std::cout << "Standard deviation is " << stddev << " !" << std::endl ;
return 0 ;
return 0 ;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>./statistics 100
<pre>./statistics 100
Line 805: Line 805:


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<lang coffeescript>
<syntaxhighlight lang="coffeescript">
generate_statistics = (n) ->
generate_statistics = (n) ->
hist = {}
hist = {}
Line 843: Line 843:
display_statistics n, mean, stddev, hist
display_statistics n, mean, stddev, hist


</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 904: Line 904:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.algorithm, std.array, std.typecons,
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.array, std.typecons,
std.range, std.exception;
std.range, std.exception;


Line 948: Line 948:
}
}
}
}
}</lang>
}</syntaxhighlight>
Compile with "-version=statistics_basic_main" to run the main function.
Compile with "-version=statistics_basic_main" to run the main function.
{{out}}
{{out}}
Line 1,030: Line 1,030:


=={{header|Dart}}==
=={{header|Dart}}==
<lang d>/* Import math library to get:
<syntaxhighlight lang="d">/* Import math library to get:
* 1) Square root function : Math.sqrt(x)
* 1) Square root function : Math.sqrt(x)
* 2) Power function : Math.pow(base, exponent)
* 2) Power function : Math.pow(base, exponent)
Line 1,128: Line 1,128:
print('');
print('');
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,205: Line 1,205:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang elixir>defmodule Statistics do
<syntaxhighlight lang="elixir">defmodule Statistics do
def basic(n) do
def basic(n) do
{sum, sum2, hist} = generate(n)
{sum, sum2, hist} = generate(n)
Line 1,231: Line 1,231:
Enum.each([100,1000,10000], fn n ->
Enum.each([100,1000,10000], fn n ->
Statistics.basic(n)
Statistics.basic(n)
end)</lang>
end)</syntaxhighlight>


{{out}}
{{out}}
Line 1,279: Line 1,279:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: assocs formatting grouping io kernel literals math
<syntaxhighlight lang="factor">USING: assocs formatting grouping io kernel literals math
math.functions math.order math.statistics prettyprint random
math.functions math.order math.statistics prettyprint random
sequences sequences.deep sequences.repeating ;
sequences sequences.deep sequences.repeating ;
Line 1,315: Line 1,315:
{ 100 1,000 10,000 } [ stats ] each ;
{ 100 1,000 10,000 } [ stats ] each ;
MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,364: Line 1,364:
{{works with|Fortran|95 and later}}
{{works with|Fortran|95 and later}}
This version will handle numbers as large as 1 trillion or more if you are prepared to wait long enough
This version will handle numbers as large as 1 trillion or more if you are prepared to wait long enough
<lang fortran>program basic_stats
<syntaxhighlight lang="fortran">program basic_stats
implicit none
implicit none
Line 1,399: Line 1,399:
end do
end do
end program</lang>
end program</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,460: Line 1,460:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Randomize
Randomize
Line 1,528: Line 1,528:
Print
Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 1,594: Line 1,594:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,634: Line 1,634:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,684: Line 1,684:


The following runs comfortably on a simulated data size of 10 million. To scale to a trillion, and to use real data, you would want to use a technique like [[Distributed_programming#Go]] to distribute work across multiple computers, and on each computer, use a technique like [[Parallel_calculations#Go]] to distribute work across multiple cores within each computer. You would tune parameters like the constant <tt>threshold</tt> in the code below to optimize cache performance.
The following runs comfortably on a simulated data size of 10 million. To scale to a trillion, and to use real data, you would want to use a technique like [[Distributed_programming#Go]] to distribute work across multiple computers, and on each computer, use a technique like [[Parallel_calculations#Go]] to distribute work across multiple cores within each computer. You would tune parameters like the constant <tt>threshold</tt> in the code below to optimize cache performance.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,747: Line 1,747:
}
}
return
return
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,766: Line 1,766:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>import Data.Foldable (foldl') --'
<syntaxhighlight lang="haskell">import Data.Foldable (foldl') --'
import System.Random (randomRs, newStdGen)
import System.Random (randomRs, newStdGen)
import Control.Monad (zipWithM_)
import Control.Monad (zipWithM_)
Line 1,834: Line 1,834:


-- To avoid Wiki formatting issue
-- To avoid Wiki formatting issue
foldl'' = foldl'</lang>
foldl'' = foldl'</syntaxhighlight>
{{out}}
{{out}}
<pre>./Statistics 100
<pre>./Statistics 100
Line 1,866: Line 1,866:
=={{header|Hy}}==
=={{header|Hy}}==


<lang lisp>(import
<syntaxhighlight lang="lisp">(import
[numpy.random [random]]
[numpy.random [random]]
[numpy [mean std]]
[numpy [mean std]]
Line 1,876: Line 1,876:


(plt.hist (random 1000))
(plt.hist (random 1000))
(plt.show)</lang>
(plt.show)</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,883: Line 1,883:
In this example,
In this example,


<lang Icon>procedure main(A)
<syntaxhighlight lang="icon">procedure main(A)


W := 50 # avg width for histogram bar
W := 50 # avg width for histogram bar
Line 1,908: Line 1,908:
write(right(real(i)/*H,5)," : ",repl("*",integer(*H*50./N*H[i])))
write(right(real(i)/*H,5)," : ",repl("*",integer(*H*50./N*H[i])))
}
}
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,957: Line 1,957:


J has library routines to compute mean and standard deviation:
J has library routines to compute mean and standard deviation:
<lang j> require 'stats'
<syntaxhighlight lang="j"> require 'stats'
(mean,stddev) 1000 ?@$ 0
(mean,stddev) 1000 ?@$ 0
0.484669 0.287482
0.484669 0.287482
Line 1,963: Line 1,963:
0.503642 0.290777
0.503642 0.290777
(mean,stddev) 100000 ?@$ 0
(mean,stddev) 100000 ?@$ 0
0.499677 0.288726</lang>
0.499677 0.288726</syntaxhighlight>


And, for a histogram:
And, for a histogram:


<lang j>histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
<syntaxhighlight lang="j">histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
require'plot'
require'plot'
plot ((% * 1 + i.)100) ([;histogram) 10000 ?@$ 0</lang>
plot ((% * 1 + i.)100) ([;histogram) 10000 ?@$ 0</syntaxhighlight>


but these are not quite what is being asked for here.
but these are not quite what is being asked for here.
Line 1,975: Line 1,975:
Instead:
Instead:


<lang j>histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
<syntaxhighlight lang="j">histogram=: <: @ (#/.~) @ (i.@#@[ , I.)


meanstddevP=: 3 :0
meanstddevP=: 3 :0
Line 1,998: Line 1,998:
smoutput (<.300*h%y) #"0 '#'
smoutput (<.300*h%y) #"0 '#'
(s%y) , %:t%y
(s%y) , %:t%y
)</lang>
)</syntaxhighlight>


Example use:
Example use:


<lang j> meanstddevP 1000
<syntaxhighlight lang="j"> meanstddevP 1000
#############################
#############################
####################################
####################################
Line 2,037: Line 2,037:
##############################
##############################
#############################
#############################
0.500872 0.288241</lang>
0.500872 0.288241</syntaxhighlight>


(That said, note that these numbers are random, so reported standard deviation will vary with the random sample being tested.)
(That said, note that these numbers are random, so reported standard deviation will vary with the random sample being tested.)
Line 2,046: Line 2,046:
Translation of [[Statistics/Basic#Python|Python]] via [[Statistics/Basic#D|D]]
Translation of [[Statistics/Basic#Python|Python]] via [[Statistics/Basic#D|D]]
{{works with|Java|8}}
{{works with|Java|8}}
<lang java>import static java.lang.Math.pow;
<syntaxhighlight lang="java">import static java.lang.Math.pow;
import static java.util.Arrays.stream;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.joining;
Line 2,097: Line 2,097:
}
}
}
}
}</lang>
}</syntaxhighlight>
<pre>10 numbers:
<pre>10 numbers:
Mean: 0.564409, SD: 0.249601
Mean: 0.564409, SD: 0.249601
Line 2,190: Line 2,190:


For the sake of illustration, we will use /dev/urandom encapsulated in a shell function:
For the sake of illustration, we will use /dev/urandom encapsulated in a shell function:
<lang sh># Usage: prng N width
<syntaxhighlight lang="sh"># Usage: prng N width
function prng {
function prng {
cat /dev/urandom | tr -cd '0-9' | fold -w "$2" | head -n "$1"
cat /dev/urandom | tr -cd '0-9' | fold -w "$2" | head -n "$1"
}</lang>
}</syntaxhighlight>


'''basicStats.jq'''
'''basicStats.jq'''
<lang jq> # $histogram should be a JSON object, with buckets as keys and frequencies as values;
<syntaxhighlight lang="jq"> # $histogram should be a JSON object, with buckets as keys and frequencies as values;
# $keys should be an array of all the potential bucket names (possibly integers)
# $keys should be an array of all the potential bucket names (possibly integers)
# in the order to be used for display:
# in the order to be used for display:
Line 2,227: Line 2,227:
stddev: \(.stddev)
stddev: \(.stddev)
Histogram dividing [0,1] into 10 equal intervals:",
Histogram dividing [0,1] into 10 equal intervals:",
pp(.histogram; [range(0;10)] )</lang>
pp(.histogram; [range(0;10)] )</syntaxhighlight>
'''Driver Script''' (e.g. bash)
'''Driver Script''' (e.g. bash)
<lang>for n in 100 1000 1000000 100000000; do
<syntaxhighlight lang="text">for n in 100 1000 1000000 100000000; do
echo "Basic statistics for $n PRNs in [0,1]"
echo "Basic statistics for $n PRNs in [0,1]"
prng $n 10 | jq -nrR -f basicStats.jq
prng $n 10 | jq -nrR -f basicStats.jq
echo
echo
done</lang>
done</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,304: Line 2,304:


=={{header|Jsish}}==
=={{header|Jsish}}==
<lang javascript>#!/usr/bin/env jsish
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish
"use strict";
"use strict";


Line 2,413: Line 2,413:
0
0
=!EXPECTEND!=
=!EXPECTEND!=
*/</lang>
*/</syntaxhighlight>


{{out}}
{{out}}
Line 2,420: Line 2,420:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Printf
<syntaxhighlight lang="julia">using Printf


function hist(numbers)
function hist(numbers)
Line 2,439: Line 2,439:
@printf("μ: %8.6f; σ: %8.6f\n", mean(n), std(n))
@printf("μ: %8.6f; σ: %8.6f\n", mean(n), std(n))
hist(n)
hist(n)
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 2,530: Line 2,530:
Using the "mu" (mean) and "sd" (standard deviation) functions from the
Using the "mu" (mean) and "sd" (standard deviation) functions from the
Klong statistics library:
Klong statistics library:
<syntaxhighlight lang="k">
<lang K>
.l("nstat.kg")
.l("nstat.kg")
bar::{x{x;.d("*")}:*0;.p("")}
bar::{x{x;.d("*")}:*0;.p("")}
Line 2,540: Line 2,540:
plot(1000)
plot(1000)
plot(10000)
plot(10000)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,588: Line 2,588:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


val rand = java.util.Random()
val rand = java.util.Random()
Line 2,636: Line 2,636:
val sampleSizes = intArrayOf(100, 1_000, 10_000, 100_000)
val sampleSizes = intArrayOf(100, 1_000, 10_000, 100_000)
for (sampleSize in sampleSizes) basicStats(sampleSize)
for (sampleSize in sampleSizes) basicStats(sampleSize)
}</lang>
}</syntaxhighlight>
Sample run:
Sample run:
{{out}}
{{out}}
Line 2,702: Line 2,702:


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>define stat1(a) => {
<syntaxhighlight lang="lasso">define stat1(a) => {
if(#a->size) => {
if(#a->size) => {
local(mean = (with n in #a sum #n) / #a->size)
local(mean = (with n in #a sum #n) / #a->size)
Line 2,752: Line 2,752:
histogram(#n)
histogram(#n)
'\r\r'
'\r\r'
^}</lang>
^}</syntaxhighlight>


{{out}}
{{out}}
Line 2,824: Line 2,824:
=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
Be aware that the PRNG in LB has a SLIGHT bias.
Be aware that the PRNG in LB has a SLIGHT bias.
<syntaxhighlight lang="lb">
<lang lb>
call sample 100
call sample 100
call sample 1000
call sample 1000
Line 2,866: Line 2,866:
print
print
end sub
end sub
</syntaxhighlight>
</lang>
100000 data terms used.
100000 data terms used.
Mean =0.49870232
Mean =0.49870232
Line 2,883: Line 2,883:
=={{header|Lua}}==
=={{header|Lua}}==
The standard deviation seems to converge to around 0.28. I expect there's a good reason for this, though it's entirely beyond me.
The standard deviation seems to converge to around 0.28. I expect there's a good reason for this, though it's entirely beyond me.
<lang lua>
<syntaxhighlight lang="lua">
math.randomseed(os.time())
math.randomseed(os.time())


Line 2,940: Line 2,940:
showStats(10 ^ power)
showStats(10 ^ power)
end
end
</syntaxhighlight>
</lang>


=={{header|Maple}}==
=={{header|Maple}}==
The following samples 100 uniformly distributed numbers between 0 and 1:
The following samples 100 uniformly distributed numbers between 0 and 1:
<lang maple>with(Statistics):
<syntaxhighlight lang="maple">with(Statistics):
X_100 := Sample( Uniform(0,1), 100 );
X_100 := Sample( Uniform(0,1), 100 );
Mean( X_100 );
Mean( X_100 );
StandardDeviation( X_100 );
StandardDeviation( X_100 );
Histogram( X_100 );</lang>
Histogram( X_100 );</syntaxhighlight>
It is also possible to make a procedure that outputs the mean, standard deviation, and a histogram for a given number of random uniformly distributed numbers:
It is also possible to make a procedure that outputs the mean, standard deviation, and a histogram for a given number of random uniformly distributed numbers:
<lang maple>sample := proc( n )
<syntaxhighlight lang="maple">sample := proc( n )
local data;
local data;
data := Sample( Uniform(0,1), n );
data := Sample( Uniform(0,1), n );
Line 2,958: Line 2,958:
return Statistics:-Histogram( data );
return Statistics:-Histogram( data );
end proc:
end proc:
sample( 1000 );</lang>
sample( 1000 );</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Sample[n_]:= (Print[#//Length," numbers, Mean : ",#//Mean,", StandardDeviation : ",#//StandardDeviation ];
<syntaxhighlight lang="mathematica">Sample[n_]:= (Print[#//Length," numbers, Mean : ",#//Mean,", StandardDeviation : ",#//StandardDeviation ];
BarChart[BinCounts[#,{0,1,.1}], Axes->False, BarOrigin->Left])&[(RandomReal[1,#])&[ n ]]
BarChart[BinCounts[#,{0,1,.1}], Axes->False, BarOrigin->Left])&[(RandomReal[1,#])&[ n ]]
Sample/@{100,1 000,10 000,1 000 000} </lang>
Sample/@{100,1 000,10 000,1 000 000} </syntaxhighlight>
{{out}}
{{out}}
<pre>100 numbers, Mean : 0.478899, StandardDeviation : 0.322265
<pre>100 numbers, Mean : 0.478899, StandardDeviation : 0.322265
Line 2,972: Line 2,972:


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
<lang Matlab> % Initialize
<syntaxhighlight lang="matlab"> % Initialize
N = 0; S=0; S2 = 0;
N = 0; S=0; S2 = 0;
binlist = 0:.1:1;
binlist = 0:.1:1;
Line 2,991: Line 2,991:
m = S/N; % mean
m = S/N; % mean
sd = sqrt(S2/N-mean*mean); % standard deviation
sd = sqrt(S2/N-mean*mean); % standard deviation
bar(binlist,h)</lang>
bar(binlist,h)</syntaxhighlight>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
A little Stats class is defined that can calculate mean and standard deviation for a stream of numbers (of arbitrary size, so yes, this would work for a trillion numbers just as well as for one).
A little Stats class is defined that can calculate mean and standard deviation for a stream of numbers (of arbitrary size, so yes, this would work for a trillion numbers just as well as for one).
<lang MiniScript>Stats = {}
<syntaxhighlight lang="miniscript">Stats = {}
Stats.count = 0
Stats.count = 0
Stats.sum = 0
Stats.sum = 0
Line 3,033: Line 3,033:
print "Mean: " + st.mean + " Standard Deviation: " + st.stddev
print "Mean: " + st.mean + " Standard Deviation: " + st.stddev
st.histogram
st.histogram
end for</lang>
end for</syntaxhighlight>
{{out}}
{{out}}
<pre>Samples: 100
<pre>Samples: 100
Line 3,074: Line 3,074:
=={{header|Nim}}==
=={{header|Nim}}==
The standard module “stats” provides procedures to compute the statistical moments. It is possible to compute them either on a list of values or incrementally using an object <code>RunningStat</code>. In the following program, we use the first method for the 100 numbers samples and we draw the histogram. For the 1_000, 10_000, 100_000 and 1_000_000 samples, we use the second method which avoids to store the values (but don’t draw the histogram).
The standard module “stats” provides procedures to compute the statistical moments. It is possible to compute them either on a list of values or incrementally using an object <code>RunningStat</code>. In the following program, we use the first method for the 100 numbers samples and we draw the histogram. For the 1_000, 10_000, 100_000 and 1_000_000 samples, we use the second method which avoids to store the values (but don’t draw the histogram).
<lang nim>import random, sequtils, stats, strutils, strformat
<syntaxhighlight lang="nim">import random, sequtils, stats, strutils, strformat


proc drawHistogram(ns: seq[float]) =
proc drawHistogram(ns: seq[float]) =
Line 3,105: Line 3,105:
rs.push(n)
rs.push(n)
echo &"μ = {rs.mean:.12f} σ = {rs.standardDeviation:.12f}"
echo &"μ = {rs.mean:.12f} σ = {rs.standardDeviation:.12f}"
echo()</lang>
echo()</syntaxhighlight>
{{out}}
{{out}}
<pre>For 100 numbers:
<pre>For 100 numbers:
Line 3,136: Line 3,136:
=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>: main(n)
<syntaxhighlight lang="oforth">: main(n)
| l m std i nb |
| l m std i nb |


Line 3,150: Line 3,150:
i 0.1 + <<wjp(3, JUSTIFY_RIGHT, 2) " - " <<
i 0.1 + <<wjp(3, JUSTIFY_RIGHT, 2) " - " <<
StringBuffer new "*" <<n(nb) << cr
StringBuffer new "*" <<n(nb) << cr
] ;</lang>
] ;</syntaxhighlight>


{{out}}
{{out}}
Line 3,224: Line 3,224:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.4.3 and above}}
{{works with|PARI/GP|2.4.3 and above}}
<lang parigp>mean(v)={
<syntaxhighlight lang="parigp">mean(v)={
vecsum(v)/#v
vecsum(v)/#v
};
};
Line 3,244: Line 3,244:
show(100);
show(100);
show(1000);
show(1000);
show(10000);</lang>
show(10000);</syntaxhighlight>


For versions before 2.4.3, define
For versions before 2.4.3, define
<lang parigp>rreal()={
<syntaxhighlight lang="parigp">rreal()={
my(pr=32*ceil(default(realprecision)*log(10)/log(4294967296))); \\ Current precision
my(pr=32*ceil(default(realprecision)*log(10)/log(4294967296))); \\ Current precision
random(2^pr)*1.>>pr
random(2^pr)*1.>>pr
};</lang>
};</syntaxhighlight>
and use <code>rreal()</code> in place of <code>random(1.)</code>.
and use <code>rreal()</code> in place of <code>random(1.)</code>.


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>my @histogram = (0) x 10;
<syntaxhighlight lang="perl">my @histogram = (0) x 10;
my $sum = 0;
my $sum = 0;
my $sum_squares = 0;
my $sum_squares = 0;
Line 3,277: Line 3,277:
print "*" x (30 * $histogram[$i] * @histogram/$n); # 30 stars expected per row
print "*" x (30 * $histogram[$i] * @histogram/$n); # 30 stars expected per row
print "\n";
print "\n";
}</lang>
}</syntaxhighlight>


Usage: <pre>perl rand_statistics.pl (number of values)</pre>
Usage: <pre>perl rand_statistics.pl (number of values)</pre>
Line 3,344: Line 3,344:
{{trans|CoffeeScript}}
{{trans|CoffeeScript}}
To do a trillion samples, I would change the existing generate loop into an inner 100_000_000 loop that still uses the fast native types, with everything outside that changed to bigatom, and of course add an outer loop which sums into them.
To do a trillion samples, I would change the existing generate loop into an inner 100_000_000 loop that still uses the fast native types, with everything outside that changed to bigatom, and of course add an outer loop which sums into them.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">generate_statistics</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">generate_statistics</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">hist</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">hist</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
Line 3,379: Line 3,379:
<span style="color: #000000;">display_statistics</span><span style="color: #0000FF;">(</span><span style="color: #000000;">generate_statistics</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))))</span>
<span style="color: #000000;">display_statistics</span><span style="color: #0000FF;">(</span><span style="color: #000000;">generate_statistics</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{Out}}
{{Out}}
<pre style="float:left; font-size: 10px">
<pre style="float:left; font-size: 10px">
Line 3,444: Line 3,444:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
The following has no limit on the number of samples. The 'statistics' function accepts an executable body 'Prg', which it calls repeatedly to get the samples.
The following has no limit on the number of samples. The 'statistics' function accepts an executable body 'Prg', which it calls repeatedly to get the samples.
<syntaxhighlight lang="picolisp">
<lang PicoLisp>
(seed (time))
(seed (time))


Line 3,473: Line 3,473:
(rand 0 (dec 1.0)) )
(rand 0 (dec 1.0)) )
(prinl) )
(prinl) )
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>100 numbers
<pre>100 numbers
Line 3,518: Line 3,518:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli> stat: procedure options (main); /* 21 May 2014 */
<syntaxhighlight lang="pli"> stat: procedure options (main); /* 21 May 2014 */


stats: procedure (values, mean, standard_deviation);
stats: procedure (values, mean, standard_deviation);
Line 3,556: Line 3,556:
end;
end;


end stat;</lang>
end stat;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,579: Line 3,579:
{{trans|Liberty BASIC}}
{{trans|Liberty BASIC}}
Changes were made from the Liberty BASIC version to normalize the histogram as well as implement a random float function.
Changes were made from the Liberty BASIC version to normalize the histogram as well as implement a random float function.
<lang purebasic>Procedure.f randomf()
<syntaxhighlight lang="purebasic">Procedure.f randomf()
#RNG_max_resolution = 2147483647
#RNG_max_resolution = 2147483647
ProcedureReturn Random(#RNG_max_resolution) / #RNG_max_resolution
ProcedureReturn Random(#RNG_max_resolution) / #RNG_max_resolution
Line 3,634: Line 3,634:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre>100 data terms used.
<pre>100 data terms used.
Line 3,682: Line 3,682:
=={{header|Python}}==
=={{header|Python}}==
The second function, sd2 only needs to go once through the numbers and so can more efficiently handle large streams of numbers.
The second function, sd2 only needs to go once through the numbers and so can more efficiently handle large streams of numbers.
<lang python>def sd1(numbers):
<syntaxhighlight lang="python">def sd1(numbers):
if numbers:
if numbers:
mean = sum(numbers) / len(numbers)
mean = sum(numbers) / len(numbers)
Line 3,720: Line 3,720:
print(' Naive method: sd: %8.6f, mean: %8.6f' % sd1(n))
print(' Naive method: sd: %8.6f, mean: %8.6f' % sd1(n))
print(' Second method: sd: %8.6f, mean: %8.6f' % sd2(n))
print(' Second method: sd: %8.6f, mean: %8.6f' % sd2(n))
histogram(n)</lang>
histogram(n)</syntaxhighlight>


{{out}}
{{out}}
Line 3,764: Line 3,764:
=={{header|R}}==
=={{header|R}}==
The challenge of processing a trillion numbers is generating them in the first place. As the errors below show, allocating 7.5 TB for such a vector is simply impractical. The workaround is to generate them, process individual data points and then discard them. The downside in this case is the time.
The challenge of processing a trillion numbers is generating them in the first place. As the errors below show, allocating 7.5 TB for such a vector is simply impractical. The workaround is to generate them, process individual data points and then discard them. The downside in this case is the time.
<syntaxhighlight lang="r">
<lang R>
#Generate the sets
#Generate the sets
a = runif(10,min=0,max=1)
a = runif(10,min=0,max=1)
Line 3,791: Line 3,791:
cat("Mean of a trillion random values in the range [0,1] : ",mean(runif(10^12,min=0,max=1)))
cat("Mean of a trillion random values in the range [0,1] : ",mean(runif(10^12,min=0,max=1)))
cat("Standard Deviation of a trillion random values in the range [0,1] : ", sd(runif(10^12,min=0,max=1)))
cat("Standard Deviation of a trillion random values in the range [0,1] : ", sd(runif(10^12,min=0,max=1)))
</syntaxhighlight>
</lang>
Output
Output
<pre>
<pre>
Line 3,812: Line 3,812:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require math (only-in srfi/27 random-real))
(require math (only-in srfi/27 random-real))
Line 3,834: Line 3,834:
(task 1000)
(task 1000)
(task 10000)
(task 10000)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,883: Line 3,883:
(formerly Perl 6)
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
{{Works with|rakudo|2018.03}}
<lang perl6>for 100, 1_000, 10_000 -> $N {
<syntaxhighlight lang="raku" line>for 100, 1_000, 10_000 -> $N {
say "size: $N";
say "size: $N";
my @data = rand xx $N;
my @data = rand xx $N;
Line 3,892: Line 3,892:
for sort @data.classify: (10 * *).Int / 10;
for sort @data.classify: (10 * *).Int / 10;
say '';
say '';
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>size: 100
<pre>size: 100
Line 3,938: Line 3,938:
=={{header|REXX}}==
=={{header|REXX}}==
Twenty decimal digits are used for the calculations, but only half that (ten digits) are displayed in the output.
Twenty decimal digits are used for the calculations, but only half that (ten digits) are displayed in the output.
<lang rexx>/*REXX program generates some random numbers, shows bin histogram, finds mean & stdDev. */
<syntaxhighlight lang="rexx">/*REXX program generates some random numbers, shows bin histogram, finds mean & stdDev. */
numeric digits 20 /*use twenty decimal digits precision, */
numeric digits 20 /*use twenty decimal digits precision, */
showDigs=digits()%2 /* ··· but only show ten decimal digits*/
showDigs=digits()%2 /* ··· but only show ten decimal digits*/
Line 3,969: Line 3,969:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_ % 2
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 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</lang>
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 100 </tt>}}
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 100 </tt>}}
<pre>
<pre>
Line 4,062: Line 4,062:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Statistics/Basic
# Project : Statistics/Basic


Line 4,101: Line 4,101:
next
next
see nl
see nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 4,172: Line 4,172:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def generate_statistics(n)
<syntaxhighlight lang="ruby">def generate_statistics(n)
sum = sum2 = 0.0
sum = sum2 = 0.0
hist = Array.new(10, 0)
hist = Array.new(10, 0)
Line 4,191: Line 4,191:
end
end


[100, 1000, 10000].each {|n| generate_statistics n}</lang>
[100, 1000, 10000].each {|n| generate_statistics n}</syntaxhighlight>


{{out}}
{{out}}
Line 4,239: Line 4,239:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>call sample 100
<syntaxhighlight lang="runbasic">call sample 100
call sample 1000
call sample 1000
call sample 10000
call sample 10000
Line 4,280: Line 4,280:
next b
next b
print
print
end sub</lang>
end sub</syntaxhighlight>
<pre style="height: 40ex; overflow: scroll">
<pre style="height: 40ex; overflow: scroll">
100 Samples used.
100 Samples used.
Line 4,327: Line 4,327:
=={{header|Rust}}==
=={{header|Rust}}==
{{libheader|rand}}
{{libheader|rand}}
<lang rust>#![feature(iter_arith)]
<syntaxhighlight lang="rust">#![feature(iter_arith)]
extern crate rand;
extern crate rand;


Line 4,396: Line 4,396:
print_histogram(40, &data);
print_histogram(40, &data);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> Statistics for sample size 1000
<pre> Statistics for sample size 1000
Line 4,442: Line 4,442:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>def mean(a:Array[Double])=a.sum / a.size
<syntaxhighlight lang="scala">def mean(a:Array[Double])=a.sum / a.size
def stddev(a:Array[Double])={
def stddev(a:Array[Double])={
val sum = a.fold(0.0)((a, b) => a + math.pow(b,2))
val sum = a.fold(0.0)((a, b) => a + math.pow(b,2))
Line 4,462: Line 4,462:
printHist(a)
printHist(a)
println
println
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>100 numbers
<pre>100 numbers
Line 4,511: Line 4,511:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>func generate_statistics(n) {
<syntaxhighlight lang="ruby">func generate_statistics(n) {
var(sum=0, sum2=0);
var(sum=0, sum2=0);
var hist = 10.of(0);
var hist = 10.of(0);
Line 4,536: Line 4,536:
}
}


[100, 1000, 10000].each {|n| generate_statistics(n) }</lang>
[100, 1000, 10000].each {|n| generate_statistics(n) }</syntaxhighlight>
{{out}}
{{out}}
<pre style="height: 40ex; overflow: scroll">
<pre style="height: 40ex; overflow: scroll">
Line 4,586: Line 4,586:
For a uniform distribution on [0,1], the mean is 1/2 and the variance is 1/12 (hence the standard deviation is 0.28867513). With a large sample, one can check the convergence to these values.
For a uniform distribution on [0,1], the mean is 1/2 and the variance is 1/12 (hence the standard deviation is 0.28867513). With a large sample, one can check the convergence to these values.


<lang stata>. clear all
<syntaxhighlight lang="stata">. clear all
. set obs 100000
. set obs 100000
number of observations (_N) was 0, now 100,000
number of observations (_N) was 0, now 100,000
Line 4,595: Line 4,595:
-------------+---------------------------------------------------------
-------------+---------------------------------------------------------
x | 100,000 .4991874 .2885253 1.18e-06 .9999939
x | 100,000 .4991874 .2885253 1.18e-06 .9999939
. hist x</lang>
. hist x</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang="tcl">package require Tcl 8.5
proc stats {size} {
proc stats {size} {
set sum 0.0
set sum 0.0
Line 4,624: Line 4,624:
stats 1000
stats 1000
puts ""
puts ""
stats 10000</lang>
stats 10000</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,672: Line 4,672:


=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>Option Base 1
<syntaxhighlight lang="vb">Option Base 1
Private Function mean(s() As Variant) As Double
Private Function mean(s() As Variant) As Double
mean = WorksheetFunction.Average(s)
mean = WorksheetFunction.Average(s)
Line 4,696: Line 4,696:
Debug.Print
Debug.Print
Next e
Next e
End Sub</lang>{{out}}
End Sub</syntaxhighlight>{{out}}
<pre>sample size 100 mean 0,472405961751938 standard deviation 0,260463885857138
<pre>sample size 100 mean 0,472405961751938 standard deviation 0,260463885857138
0,00-0,10 XXXXXX
0,00-0,10 XXXXXX
Line 4,735: Line 4,735:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>import rand
<syntaxhighlight lang="vlang">import rand
import math
import math


Line 4,769: Line 4,769:
}
}
println('')
println('')
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,817: Line 4,817:
</pre>
</pre>
Or use standard math.stats module
Or use standard math.stats module
<lang>import rand
<syntaxhighlight lang="text">import rand
import math.stats
import math.stats


Line 4,846: Line 4,846:
}
}
println('')
println('')
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Similar to above
Similar to above
Line 4,852: Line 4,852:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-math}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/math" for Nums
import "/math" for Nums


Line 4,875: Line 4,875:
sums[9] = 100 - Nums.sum(sums[0..8])
sums[9] = 100 - Nums.sum(sums[0..8])
System.print(" 0.9 - 1.0: %("*" * sums[9])\n")
System.print(" 0.9 - 1.0: %("*" * sums[9])\n")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,927: Line 4,927:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn mean(ns) { ns.sum(0.0)/ns.len() }
<syntaxhighlight lang="zkl">fcn mean(ns) { ns.sum(0.0)/ns.len() }
fcn stdDev(ns){
fcn stdDev(ns){
m:=mean(ns); (ns.reduce('wrap(p,n){ x:=(n-m); p+x*x },0.0)/ns.len()).sqrt()
m:=mean(ns); (ns.reduce('wrap(p,n){ x:=(n-m); p+x*x },0.0)/ns.len()).sqrt()
}</lang>
}</syntaxhighlight>
<lang zkl>reg ns;
<syntaxhighlight lang="zkl">reg ns;
foreach n in (T(100,1000,10000)){
foreach n in (T(100,1000,10000)){
ns=(0).pump(n,List,(0.0).random.fp(1.0));
ns=(0).pump(n,List,(0.0).random.fp(1.0));
Line 4,939: Line 4,939:
n:=ns.filter('wrap(x){ r<=x<(r+0.1) }).len();
n:=ns.filter('wrap(x){ r<=x<(r+0.1) }).len();
println("%.2f..%.2f:%4d%s".fmt(r,r+0.1,n,"*"*(n/20)));
println("%.2f..%.2f:%4d%s".fmt(r,r+0.1,n,"*"*(n/20)));
}</lang>
}</syntaxhighlight>
(0.0).random(1.0) generates a [uniform] random number between 0 (inclusive) and 1 (exclusive).
(0.0).random(1.0) generates a [uniform] random number between 0 (inclusive) and 1 (exclusive).
{{out}}
{{out}}
Line 4,958: Line 4,958:
</pre>
</pre>
For the extra credit, pretend we have a device that spews random numbers in the range [0..1) forever. We connect this device to a measuring device that calculates mean and std deviation, printing results on a regular basis.
For the extra credit, pretend we have a device that spews random numbers in the range [0..1) forever. We connect this device to a measuring device that calculates mean and std deviation, printing results on a regular basis.
<lang zkl>var pipe=Thread.Pipe(); // used to connect the two threads
<syntaxhighlight lang="zkl">var pipe=Thread.Pipe(); // used to connect the two threads
fcn{ while(1){ pipe.write((0.0).random(1.0)) } }.launch(); // generator
fcn{ while(1){ pipe.write((0.0).random(1.0)) } }.launch(); // generator
fcn{ // consumer/calculator
fcn{ // consumer/calculator
Line 4,970: Line 4,970:
}.launch();
}.launch();


Atomic.sleep(60*60); // wait because exiting the VM kills the threads</lang>
Atomic.sleep(60*60); // wait because exiting the VM kills the threads</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>