June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,37 @@
USING: assocs formatting grouping io kernel literals math
math.functions math.order math.statistics prettyprint random
sequences sequences.deep sequences.repeating ;
IN: rosetta-code.statistics-basic
CONSTANT: granularity
$[ 11 iota [ 10 /f ] map 2 clump ]
: mean/std ( seq -- a b )
[ mean ] [ population-std ] bi ;
: .mean/std ( seq -- )
mean/std [ "Mean: " write . ] [ "STD: " write . ] bi* ;
: count-between ( seq a b -- n )
[ between? ] 2curry count ;
: histo ( seq -- seq )
granularity [ first2 count-between ] with map ;
: bar ( n -- str )
[ dup 50 < ] [ 10 / ] until 2 * >integer "*" swap repeat ;
: (.histo) ( seq -- seq' )
[ bar ] map granularity swap zip flatten 3 group ;
: .histo ( seq -- )
(.histo) [ "%.1f - %.1f %s\n" vprintf ] each ;
: stats ( n -- )
dup "Statistics %d:\n" printf
random-units [ histo .histo ] [ .mean/std nl ] bi ;
: main ( -- )
{ 100 1,000 10,000 } [ stats ] each ;
MAIN: main

View file

@ -0,0 +1,18 @@
function hist(numbers)
maxwidth = 50
h = fill(0, 10)
for n in numbers
h[ceil(Int, 10n)] += 1
end
mx = maximum(h)
for (n, i) in enumerate(h)
@printf("%3.1f: %s\n", n / 10, "+" ^ floor(Int, i / mx * maxwidth))
end
end
for i in 1:6
n = rand(10 ^ i)
println("\n##\n## $(10 ^ i) numbers")
@printf("μ: %8.6f; σ: %8.6f\n", mean(n), std(n))
hist(n)
end

View file

@ -0,0 +1,9 @@
.l("nstat.kg")
bar::{x{x;.d("*")}:*0;.p("")}
hist10::{[s];#'=s@<s::_x*10}
plot::{[s];.p("");.p("n = ",$x);
(!10){.d(x%10);.d(" ");bar(y)}'_(100%x)*(hist10(s::{x;.rn()}'!x));
.p("mean = ",$mu(s));.p("sd = ",$sd(s))}
plot(100)
plot(1000)
plot(10000)

View file

@ -6,5 +6,5 @@ for 100, 1_000, 10_000 -> $N {
$mean**2 R- $N R/ [+] @data »**» 2;
printf "%.1f %s\n", .key, '=' x (500 * .value.elems / $N)
for sort @data.classify: (10 * *).Int / 10;
&say;
say '';
}

View file

@ -0,0 +1,28 @@
(seed (time))
(scl 8)
(de statistics (Cnt . Prg)
(prinl Cnt " numbers")
(let (Sum 0 Sqr 0 Hist (need 10 NIL 0))
(do Cnt
(let N (run Prg 1) # Get next number
(inc 'Sum N)
(inc 'Sqr (*/ N N 1.0))
(inc (nth Hist (inc (/ N 0.1)))) ) )
(let M (*/ Sum Cnt)
(prinl "Mean: " (round M))
(prinl "StdDev: "
(round
(sqrt
(- (*/ Sqr Cnt) (*/ M M 1.0))
1.0 ) ) ) )
(for (I . H) Hist
(prin (format I 1) " ")
(do (*/ H 400 Cnt) (prin '=))
(prinl) ) ) )
(for I (2 4 6)
(statistics (** 10 I)
(rand 0 (dec 1.0)) )
(prinl) )

View file

@ -0,0 +1,28 @@
#Abhishek Ghosh, 10th January 2018
#Generate the sets
a = runif(10,min=0,max=1)
b = runif(100,min=0,max=1)
c = runif(1000,min=0,max=1)
d = runif(10000,min=0,max=1)
#Print out the set of 10 values
cat("a = ",a)
#Print out the Mean and Standard Deviations of each of the sets
cat("Mean of a : ",mean(a))
cat("Standard Deviation of a : ", sd(a))
cat("Mean of b : ",mean(b))
cat("Standard Deviation of b : ", sd(b))
cat("Mean of c : ",mean(c))
cat("Standard Deviation of c : ", sd(c))
cat("Mean of d : ",mean(d))
cat("Standard Deviation of d : ", sd(d))
#Plotting the histogram of d
hist(d)
#Following lines error out due to insufficient memory
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)))

View file

@ -6,28 +6,27 @@ if size=='' | size=="," then size=100 /*Not specified? Then use the
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. */
@.j=random(, 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. */
#._=#._ + 1 /* ··· and bump its count. */
end /*j*/
do k=0 for 10; kp=k+1 /*show a histogram of the bins. */
lr='0.'k ; if k==0 then lr="0 " /*adjust for the low range. */
hr='0.'kp ; if k==9 then hr="1 " /* " " " high range. */
barPC=right(strip(left(format(100*#.k/size, , 2), 5)) ,5) /*compute the %. */
say lr"──►"hr' ' barPC copies('', format(barPC*1, , 0)) /*display histogram*/
end /*k*/
do k=0 for 10; kp=k + 1 /*show a histogram of the bins. */
lr='0.'k ; if k==0 then lr= "0 " /*adjust for the low range. */
hr='0.'kp ; if k==9 then hr= "1 " /* " " " high range. */
barPC=right( strip( left( format( 100*#.k / size, , 2), 5)), 5) /*compute the %. */
say lr"──►"hr' ' barPC copies("", barPC * 2 % 1 ) /*show 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)
mean: arg N; $=0; do m=1 for N; $=$ + @.m; end; return $/N
stdDev: arg N; $=0; do s=1 for N; $=$ + (@.s-avg)**2; end; return sqrt($/N) /1
/*──────────────────────────────────────────────────────────────────────────────────────*/
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
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

View file

@ -0,0 +1,41 @@
# Project : Statistics/Basic
# Date : 2017/11/14
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
decimals(9)
sample(100)
sample(1000)
sample(10000)
func sample(n)
samp = list(n)
for i =1 to n
samp[i] =random(9)/10
next
sum = 0
sumSq = 0
for i = 1 to n
sum = sum + samp[i]
sumSq = sumSq +pow(samp[i],2)
next
see n + " Samples used." + nl
mean = sum / n
see "Mean = " + mean + nl
see "Std Dev = " + pow((sumSq /n -pow(mean,2)),0.5) + nl
bins2 = 10
bins = list(bins2)
for i = 1 to n
z = floor(bins2 * samp[i])
if z != 0
bins[z] = bins[z] +1
ok
next
for b = 1 to bins2
see b + " " + nl
for j = 1 to floor(bins2 *bins[b]) /n *70
see "*"
next
see nl
next
see nl