Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
25
Task/Statistics-Basic/00DESCRIPTION
Normal file
25
Task/Statistics-Basic/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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>.
|
||||
|
||||
When examining a large quantity of data, one often uses a [[wp:Histogram|histogram]], which shows the counts of data samples falling into a prechosen set of intervals (or bins).
|
||||
When plotted, often as bar graphs, it visually indicates how often each data value occurs.
|
||||
|
||||
'''Task''' Using your language's random number routine, generate real numbers in the range of [0, 1]. It doesn't matter if you chose to use open or closed range.
|
||||
Create 100 of such numbers (i.e. sample size 100) and calculate their mean and stddev.
|
||||
Do so for sample size of 1,000 and 10,000, maybe even higher if you feel like.
|
||||
Show a histogram of any of these sets.
|
||||
Do you notice some patterns about the standard deviation?
|
||||
|
||||
'''Extra''' Sometimes so much data need to be processed that it's impossible to keep all of them at once. Can you calculate the mean, stddev and histogram of a trillion numbers? (You don't really need to do a trillion numbers, just show how it can be done.)
|
||||
|
||||
;Hint:
|
||||
For a finite population with equal probabilities at all points, one can derive:
|
||||
|
||||
:<math>\overline{(x - \overline{x})^2} = \overline{x^2} - \overline{x}^2</math>
|
||||
|
||||
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>
|
||||
2
Task/Statistics-Basic/00META.yaml
Normal file
2
Task/Statistics-Basic/00META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Mathematics
|
||||
63
Task/Statistics-Basic/Ada/statistics-basic-1.ada
Normal file
63
Task/Statistics-Basic/Ada/statistics-basic-1.ada
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
|
||||
Ada.Numerics.Generic_Elementary_Functions;
|
||||
|
||||
procedure Basic_Stat is
|
||||
|
||||
package FRG renames Ada.Numerics.Float_Random;
|
||||
package TIO renames Ada.Text_IO;
|
||||
|
||||
type Counter is range 0 .. 2**31-1;
|
||||
type Result_Array is array(Natural range <>) of Counter;
|
||||
|
||||
package FIO is new TIO.Float_IO(Float);
|
||||
|
||||
procedure Put_Histogram(R: Result_Array; Scale, Full: Counter) is
|
||||
begin
|
||||
for I in R'Range loop
|
||||
FIO.Put(Float'Max(0.0, Float(I)/10.0 - 0.05),
|
||||
Fore => 1, Aft => 2, Exp => 0); TIO.Put("..");
|
||||
FIO.Put(Float'Min(1.0, Float(I)/10.0 + 0.05),
|
||||
Fore => 1, Aft => 2, Exp => 0); TIO.Put(": ");
|
||||
for J in 1 .. (R(I)* Scale)/Full loop
|
||||
Ada.Text_IO.Put("X");
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
end Put_Histogram;
|
||||
|
||||
procedure Put_Mean_Et_Al(Sample_Size: Counter;
|
||||
Val_Sum, Square_Sum: Float) is
|
||||
Mean: constant Float := Val_Sum / Float(Sample_Size);
|
||||
package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
|
||||
begin
|
||||
TIO.Put("Mean: ");
|
||||
FIO.Put(Mean, Fore => 1, Aft => 5, Exp => 0);
|
||||
TIO.Put(", Standard Deviation: ");
|
||||
FIO.Put(Math.Sqrt(abs(Square_Sum / Float(Sample_Size)
|
||||
- (Mean * Mean))), Fore => 1, Aft => 5, Exp => 0);
|
||||
TIO.New_Line;
|
||||
end Put_Mean_Et_Al;
|
||||
|
||||
N: Counter := Counter'Value(Ada.Command_Line.Argument(1));
|
||||
Gen: FRG.Generator;
|
||||
Results: Result_Array(0 .. 10) := (others => 0);
|
||||
X: Float;
|
||||
Val_Sum, Squ_Sum: Float := 0.0;
|
||||
|
||||
begin
|
||||
FRG.Reset(Gen);
|
||||
for I in 1 .. N loop
|
||||
X := FRG.Random(Gen);
|
||||
Val_Sum := Val_Sum + X;
|
||||
Squ_Sum := Squ_Sum + X*X;
|
||||
declare
|
||||
Index: Integer := Integer(X*10.0);
|
||||
begin
|
||||
Results(Index) := Results(Index) + 1;
|
||||
end;
|
||||
end loop;
|
||||
TIO.Put_Line("After sampling" & Counter'Image(N) & " random numnbers: ");
|
||||
Put_Histogram(Results, Scale => 600, Full => N);
|
||||
TIO.New_Line;
|
||||
Put_Mean_Et_Al(Sample_Size => N, Val_Sum => Val_Sum, Square_Sum => Squ_Sum);
|
||||
end Basic_Stat;
|
||||
77
Task/Statistics-Basic/Ada/statistics-basic-2.ada
Normal file
77
Task/Statistics-Basic/Ada/statistics-basic-2.ada
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
|
||||
Ada.Numerics.Generic_Elementary_Functions;
|
||||
|
||||
procedure Long_Basic_Stat is
|
||||
|
||||
package FRG renames Ada.Numerics.Float_Random;
|
||||
package TIO renames Ada.Text_IO;
|
||||
|
||||
type Counter is range 0 .. 2**63-1;
|
||||
type Result_Array is array(Natural range <>) of Counter;
|
||||
type High_Precision is digits 15;
|
||||
|
||||
package FIO is new TIO.Float_IO(Float);
|
||||
|
||||
procedure Put_Histogram(R: Result_Array; Scale, Full: Counter) is
|
||||
begin
|
||||
for I in R'Range loop
|
||||
FIO.Put(Float'Max(0.0, Float(I)/10.0 - 0.05),
|
||||
Fore => 1, Aft => 2, Exp => 0); TIO.Put("..");
|
||||
FIO.Put(Float'Min(1.0, Float(I)/10.0 + 0.05),
|
||||
Fore => 1, Aft => 2, Exp => 0); TIO.Put(": ");
|
||||
for J in 1 .. (R(I)* Scale)/Full loop
|
||||
Ada.Text_IO.Put("X");
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
end Put_Histogram;
|
||||
|
||||
procedure Put_Mean_Et_Al(Sample_Size: Counter;
|
||||
Val_Sum, Square_Sum: Float) is
|
||||
Mean: constant Float := Val_Sum / Float(Sample_Size);
|
||||
package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
|
||||
begin
|
||||
TIO.Put("Mean: ");
|
||||
FIO.Put(Mean, Fore => 1, Aft => 5, Exp => 0);
|
||||
TIO.Put(", Standard Deviation: ");
|
||||
FIO.Put(Math.Sqrt(abs(Square_Sum / Float(Sample_Size)
|
||||
- (Mean * Mean))), Fore => 1, Aft => 5, Exp => 0);
|
||||
TIO.New_Line;
|
||||
end Put_Mean_Et_Al;
|
||||
|
||||
N: Counter := Counter'Value(Ada.Command_Line.Argument(1));
|
||||
Gen: FRG.Generator;
|
||||
Results: Result_Array(0 .. 10) := (others => 0);
|
||||
X: Float;
|
||||
Val_Sum, Squ_Sum: High_Precision := 0.0;
|
||||
|
||||
begin
|
||||
FRG.Reset(Gen);
|
||||
for Outer in 1 .. 1000 loop
|
||||
for I in 1 .. N/1000 loop
|
||||
X := FRG.Random(Gen);
|
||||
Val_Sum := Val_Sum + High_Precision(X);
|
||||
Squ_Sum := Squ_Sum + High_Precision(X)*High_Precision(X);
|
||||
declare
|
||||
Index: Integer := Integer(X*10.0);
|
||||
begin
|
||||
Results(Index) := Results(Index) + 1;
|
||||
end;
|
||||
end loop;
|
||||
if Outer mod 50 = 0 then
|
||||
TIO.New_Line(1);
|
||||
TIO.Put_Line(Integer'Image(Outer/10) &"% done; current results:");
|
||||
Put_Mean_Et_Al(Sample_Size => (Counter(Outer)*N)/1000,
|
||||
Val_Sum => Float(Val_Sum),
|
||||
Square_Sum => Float(Squ_Sum));
|
||||
else
|
||||
Ada.Text_IO.Put(".");
|
||||
end if;
|
||||
end loop;
|
||||
TIO.New_Line(4);
|
||||
TIO.Put_Line("After sampling" & Counter'Image(N) & " random numnbers: ");
|
||||
Put_Histogram(Results, Scale => 600, Full => N);
|
||||
TIO.New_Line;
|
||||
Put_Mean_Et_Al(Sample_Size => N,
|
||||
Val_Sum => Float(Val_Sum), Square_Sum => Float(Squ_Sum));
|
||||
end Long_Basic_Stat;
|
||||
102
Task/Statistics-Basic/C/statistics-basic.c
Normal file
102
Task/Statistics-Basic/C/statistics-basic.c
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define n_bins 10
|
||||
|
||||
double rand01() { return rand() / (RAND_MAX + 1.0); }
|
||||
|
||||
double avg(int count, double *stddev, int *hist)
|
||||
{
|
||||
double x[count];
|
||||
double m = 0, s = 0;
|
||||
|
||||
for (int i = 0; i < n_bins; i++) hist[i] = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
m += (x[i] = rand01());
|
||||
hist[(int)(x[i] * n_bins)] ++;
|
||||
}
|
||||
|
||||
m /= count;
|
||||
for (int i = 0; i < count; i++)
|
||||
s += x[i] * x[i];
|
||||
*stddev = sqrt(s / count - m * m);
|
||||
return m;
|
||||
}
|
||||
|
||||
void hist_plot(int *hist)
|
||||
{
|
||||
int max = 0, step = 1;
|
||||
double inc = 1.0 / n_bins;
|
||||
|
||||
for (int i = 0; i < n_bins; i++)
|
||||
if (hist[i] > max) max = hist[i];
|
||||
|
||||
/* scale if numbers are too big */
|
||||
if (max >= 60) step = (max + 59) / 60;
|
||||
|
||||
for (int i = 0; i < n_bins; i++) {
|
||||
printf("[%5.2g,%5.2g]%5d ", i * inc, (i + 1) * inc, hist[i]);
|
||||
for (int j = 0; j < hist[i]; j += step)
|
||||
printf("#");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* record for moving average and stddev. Values kept are sums and sum data^2
|
||||
* to avoid excessive precision loss due to divisions, but some loss is inevitable
|
||||
*/
|
||||
typedef struct {
|
||||
uint64_t size;
|
||||
double sum, x2;
|
||||
uint64_t hist[n_bins];
|
||||
} moving_rec;
|
||||
|
||||
void moving_avg(moving_rec *rec, double *data, int count)
|
||||
{
|
||||
double sum = 0, x2 = 0;
|
||||
/* not adding data directly to the sum in case both recorded sum and
|
||||
* count of this batch are large; slightly less likely to lose precision*/
|
||||
for (int i = 0; i < count; i++) {
|
||||
sum += data[i];
|
||||
x2 += data[i] * data[i];
|
||||
rec->hist[(int)(data[i] * n_bins)]++;
|
||||
}
|
||||
|
||||
rec->sum += sum;
|
||||
rec->x2 += x2;
|
||||
rec->size += count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double m, stddev;
|
||||
int hist[n_bins], samples = 10;
|
||||
|
||||
while (samples <= 10000) {
|
||||
m = avg(samples, &stddev, hist);
|
||||
printf("size %5d: %g %g\n", samples, m, stddev);
|
||||
samples *= 10;
|
||||
}
|
||||
|
||||
printf("\nHistograph:\n");
|
||||
hist_plot(hist);
|
||||
|
||||
printf("\nMoving average:\n N Mean Sigma\n");
|
||||
moving_rec rec = { 0, 0, 0, {0} };
|
||||
double data[100];
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
for (int j = 0; j < 100; j++) data[j] = rand01();
|
||||
|
||||
moving_avg(&rec, data, 100);
|
||||
|
||||
if ((i % 1000) == 999) {
|
||||
printf("%4lluk %f %f\n",
|
||||
rec.size/1000,
|
||||
rec.sum / rec.size,
|
||||
sqrt(rec.x2 * rec.size - rec.sum * rec.sum)/rec.size
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Task/Statistics-Basic/CoffeeScript/statistics-basic.coffee
Normal file
36
Task/Statistics-Basic/CoffeeScript/statistics-basic.coffee
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
generate_statistics = (n) ->
|
||||
hist = {}
|
||||
|
||||
update_hist = (r) ->
|
||||
hist[Math.floor 10*r] ||= 0
|
||||
hist[Math.floor 10*r] += 1
|
||||
|
||||
sum = 0
|
||||
sum_squares = 0.0
|
||||
|
||||
for i in [1..n]
|
||||
r = Math.random()
|
||||
sum += r
|
||||
sum_squares += r*r
|
||||
update_hist r
|
||||
mean = sum / n
|
||||
stddev = Math.sqrt((sum_squares / n) - mean*mean)
|
||||
|
||||
[n, mean, stddev, hist]
|
||||
|
||||
display_statistics = (n, mean, stddev, hist) ->
|
||||
console.log "-- Stats for sample size #{n}"
|
||||
console.log "mean: #{mean}"
|
||||
console.log "sdev: #{stddev}"
|
||||
for x, cnt of hist
|
||||
bars = repeat "=", Math.floor(cnt*300/n)
|
||||
console.log "#{x/10}: #{bars} #{cnt}"
|
||||
|
||||
repeat = (c, n) ->
|
||||
s = ''
|
||||
s += c for i in [1..n]
|
||||
s
|
||||
|
||||
for n in [100, 1000, 10000, 1000000]
|
||||
[n, mean, stddev, hist] = generate_statistics n
|
||||
display_statistics n, mean, stddev, hist
|
||||
45
Task/Statistics-Basic/D/statistics-basic.d
Normal file
45
Task/Statistics-Basic/D/statistics-basic.d
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import std.stdio, std.algorithm, std.array, std.typecons,
|
||||
std.range, std.exception;
|
||||
|
||||
auto meanStdDev(R)(R numbers) /*nothrow*/ @safe /*@nogc*/ {
|
||||
if (numbers.empty)
|
||||
return tuple(0.0L, 0.0L);
|
||||
|
||||
real sx = 0.0, sxx = 0.0;
|
||||
ulong n;
|
||||
foreach (x; numbers) {
|
||||
sx += x;
|
||||
sxx += x ^^ 2;
|
||||
n++;
|
||||
}
|
||||
return tuple(sx / n, (n * sxx - sx ^^ 2) ^^ 0.5L / n);
|
||||
}
|
||||
|
||||
void showHistogram01(R)(R numbers) /*@safe*/ {
|
||||
enum maxWidth = 50; // N. characters.
|
||||
ulong[10] bins;
|
||||
foreach (immutable x; numbers) {
|
||||
immutable index = cast(size_t)(x * bins.length);
|
||||
enforce(index >= 0 && index < bins.length);
|
||||
bins[index]++;
|
||||
}
|
||||
immutable real maxFreq = bins.reduce!max;
|
||||
|
||||
foreach (immutable n, immutable i; bins)
|
||||
writefln(" %3.1f: %s", n / real(bins.length),
|
||||
replicate("*", cast(int)(i / maxFreq * maxWidth)));
|
||||
writeln;
|
||||
}
|
||||
|
||||
version (statistics_basic_main) {
|
||||
void main() @safe {
|
||||
import std.random;
|
||||
|
||||
foreach (immutable p; 1 .. 7) {
|
||||
auto n = iota(10L ^^ p).map!(_ => uniform(0.0L, 1.0L));
|
||||
writeln(10L ^^ p, " numbers:");
|
||||
writefln(" Mean: %8.6f, SD: %8.6f", n.meanStdDev.tupleof);
|
||||
n.showHistogram01;
|
||||
}
|
||||
}
|
||||
}
|
||||
99
Task/Statistics-Basic/Dart/statistics-basic.dart
Normal file
99
Task/Statistics-Basic/Dart/statistics-basic.dart
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/* Import math library to get:
|
||||
* 1) Square root function : Math.sqrt(x)
|
||||
* 2) Power function : Math.pow(base, exponent)
|
||||
* 3) Random number generator : Math.Random()
|
||||
*/
|
||||
import 'dart:math' as Math show sqrt, pow, Random;
|
||||
|
||||
// Returns average/mean of a list of numbers
|
||||
num mean(List<num> l) => l.reduce((num value,num element)=>value+element)/l.length;
|
||||
|
||||
// Returns standard deviation of a list of numbers
|
||||
num stdev(List<num> l) => Math.sqrt((1/l.length)*l.map((num x)=>x*x).reduce((num value,num element) => value+element) - Math.pow(mean(l),2));
|
||||
|
||||
/* CODE TO PRINT THE HISTOGRAM STARTS HERE
|
||||
*
|
||||
* Histogram has ten fields, one for every tenth between 0 and 1
|
||||
* To do this, we save the histogram as a global variable
|
||||
* that will hold the number of occurences of each tenth in the sample
|
||||
*/
|
||||
List<num> histogram = new List.filled(10,0);
|
||||
|
||||
/*
|
||||
* METHOD TO CREATE A RANDOM SAMPLE OF n NUMBERS (Returns a list)
|
||||
*
|
||||
* While creating each value, this method also increments the
|
||||
* appropriate index of the histogram
|
||||
*/
|
||||
List<num> randomsample(num n){
|
||||
List<num> l = new List<num>(n);
|
||||
histogram = new List.filled(10,0);
|
||||
num random = new Math.Random();
|
||||
for (int i = 0; i < n; i++){
|
||||
l[i] = random.nextDouble();
|
||||
histogram[conv(l[i])] += 1;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
/*
|
||||
* METHOD TO RETURN A STRING OF n ASTERIXES (yay ASCII art)
|
||||
*/
|
||||
String stars(num n){
|
||||
String s = '';
|
||||
for (int i = 0; i < n; i++){
|
||||
s = s + '*';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
* METHOD TO DRAW THE HISTOGRAM
|
||||
* 1) Get to total for all the values in the histogram
|
||||
* 2) For every field in the histogram:
|
||||
* a) Compute the frequency for every field in the histogram
|
||||
* b) Print the frequency as asterixes
|
||||
*/
|
||||
void drawhistogram(){
|
||||
int total = histogram.reduce((num element,num value)=>element+value);
|
||||
double freq;
|
||||
for (int i = 0; i < 10; i++){
|
||||
freq = histogram[i]/total;
|
||||
print('${i/10} - ${(i+1)/10} : ' + stars(conv(30*freq)));
|
||||
}
|
||||
}
|
||||
|
||||
/* HELPER METHOD:
|
||||
* converts values between 0-1 to integers between 0-9 inclusive
|
||||
* useful to figure out which random value generated
|
||||
* corresponds to which field in the histogram
|
||||
*/
|
||||
int conv(num i) => (10*i).floor();
|
||||
|
||||
|
||||
/* MAIN FUNCTION
|
||||
*
|
||||
* Create 5 histograms and print the mean and standard deviation for each:
|
||||
* 1) Sample Size = 100
|
||||
* 2) Sample Size = 1000
|
||||
* 3) Sample Size = 10000
|
||||
* 4) Sample Size = 100000
|
||||
* 5) Sample Size = 1000000
|
||||
*
|
||||
*/
|
||||
void main(){
|
||||
List<num> l;
|
||||
num m;
|
||||
num s;
|
||||
List<int> sampleSizes = [100,1000,10000,100000,1000000];
|
||||
for (int samplesize in sampleSizes){
|
||||
print('--------------- Sample size $samplesize ----------------');
|
||||
l = randomsample(samplesize);
|
||||
m = mean(l);
|
||||
s = stdev(l);
|
||||
drawhistogram();
|
||||
print('');
|
||||
print('mean: ${m.toStringAsPrecision(8)} standard deviation: ${s.toStringAsPrecision(8)}');
|
||||
print('');
|
||||
}
|
||||
}
|
||||
36
Task/Statistics-Basic/Fortran/statistics-basic.f
Normal file
36
Task/Statistics-Basic/Fortran/statistics-basic.f
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
program basic_stats
|
||||
implicit none
|
||||
|
||||
integer, parameter :: i64 = selected_int_kind(18)
|
||||
integer, parameter :: r64 = selected_real_kind(15)
|
||||
integer(i64), parameter :: samples = 1000000000_i64
|
||||
|
||||
real(r64) :: r
|
||||
real(r64) :: mean, stddev
|
||||
real(r64) :: sumn = 0, sumnsq = 0
|
||||
integer(i64) :: n = 0
|
||||
integer(i64) :: bin(10) = 0
|
||||
integer :: i, ind
|
||||
|
||||
call random_seed
|
||||
|
||||
n = 0
|
||||
do while(n <= samples)
|
||||
call random_number(r)
|
||||
ind = r * 10 + 1
|
||||
bin(ind) = bin(ind) + 1_i64
|
||||
sumn = sumn + r
|
||||
sumnsq = sumnsq + r*r
|
||||
n = n + 1_i64
|
||||
end do
|
||||
|
||||
mean = sumn / n
|
||||
stddev = sqrt(sumnsq/n - mean*mean)
|
||||
write(*, "(a, i0)") "sample size = ", samples
|
||||
write(*, "(a, f17.15)") "Mean : ", mean,
|
||||
write(*, "(a, f17.15)") "Stddev : ", stddev
|
||||
do i = 1, 10
|
||||
write(*, "(f3.1, a, a)") real(i)/10.0, ": ", repeat("=", int(bin(i)*500/samples))
|
||||
end do
|
||||
|
||||
end program
|
||||
41
Task/Statistics-Basic/Go/statistics-basic-1.go
Normal file
41
Task/Statistics-Basic/Go/statistics-basic-1.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sample(100)
|
||||
sample(1000)
|
||||
sample(10000)
|
||||
}
|
||||
|
||||
func sample(n int) {
|
||||
// generate data
|
||||
d := make([]float64, n)
|
||||
for i := range d {
|
||||
d[i] = rand.Float64()
|
||||
}
|
||||
// show mean, standard deviation
|
||||
var sum, ssq float64
|
||||
for _, s := range d {
|
||||
sum += s
|
||||
ssq += s * s
|
||||
}
|
||||
fmt.Println(n, "numbers")
|
||||
m := sum / float64(n)
|
||||
fmt.Println("Mean: ", m)
|
||||
fmt.Println("Stddev:", math.Sqrt(ssq/float64(n)-m*m))
|
||||
// show histogram
|
||||
h := make([]int, 10)
|
||||
for _, s := range d {
|
||||
h[int(s*10)]++
|
||||
}
|
||||
for _, c := range h {
|
||||
fmt.Println(strings.Repeat("*", c*205/int(n)))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
64
Task/Statistics-Basic/Go/statistics-basic-2.go
Normal file
64
Task/Statistics-Basic/Go/statistics-basic-2.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
bigSample(1e7)
|
||||
}
|
||||
|
||||
func bigSample(n int64) {
|
||||
sum, ssq, h := reduce(0, n)
|
||||
// compute final statistics and output as above
|
||||
fmt.Println(n, "numbers")
|
||||
m := sum / float64(n)
|
||||
fmt.Println("Mean: ", m)
|
||||
fmt.Println("Stddev:", math.Sqrt(ssq/float64(n)-m*m))
|
||||
for _, c := range h {
|
||||
fmt.Println(strings.Repeat("*", c*205/int(n)))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
const threshold = 1e6
|
||||
|
||||
func reduce(start, end int64) (sum, ssq float64, h []int) {
|
||||
n := end - start
|
||||
if n < threshold {
|
||||
d := getSegment(start, end)
|
||||
return computeSegment(d)
|
||||
}
|
||||
// map to two sub problems
|
||||
half := (start + end) / 2
|
||||
sum1, ssq1, h1 := reduce(start, half)
|
||||
sum2, ssq2, h2 := reduce(half, end)
|
||||
// combine results
|
||||
for i, c := range h2 {
|
||||
h1[i] += c
|
||||
}
|
||||
return sum1 + sum2, ssq1 + ssq2, h1
|
||||
}
|
||||
|
||||
func getSegment(start, end int64) []float64 {
|
||||
d := make([]float64, end-start)
|
||||
for i := range d {
|
||||
d[i] = rand.Float64()
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func computeSegment(d []float64) (sum, ssq float64, h []int) {
|
||||
for _, s := range d {
|
||||
sum += s
|
||||
ssq += s * s
|
||||
}
|
||||
h = make([]int, 10)
|
||||
for _, s := range d {
|
||||
h[int(s*10)]++
|
||||
}
|
||||
return
|
||||
}
|
||||
26
Task/Statistics-Basic/Icon/statistics-basic.icon
Normal file
26
Task/Statistics-Basic/Icon/statistics-basic.icon
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
procedure main(A)
|
||||
|
||||
W := 50 # avg width for histogram bar
|
||||
B := 10 # histogram bins
|
||||
if *A = 0 then put(A,100) # 100 if none specified
|
||||
|
||||
while N := get(A) do { # once per argument
|
||||
write("\nN=",N)
|
||||
|
||||
N := 0 < integer(N) | next # skip if invalid
|
||||
|
||||
stddev() # reset
|
||||
m := 0.
|
||||
H := list(B,0) # Histogram of
|
||||
every i := 1 to N do { # calc running ...
|
||||
s := stddev(r := ?0) # ... std dev
|
||||
m +:= r/N # ... mean
|
||||
H[integer(*H*r)+1] +:= 1 # ... histogram
|
||||
}
|
||||
|
||||
write("mean=",m)
|
||||
write("stddev=",s)
|
||||
every i := 1 to *H do # show histogram
|
||||
write(right(real(i)/*H,5)," : ",repl("*",integer(*H*50./N*H[i])))
|
||||
}
|
||||
end
|
||||
7
Task/Statistics-Basic/J/statistics-basic-1.j
Normal file
7
Task/Statistics-Basic/J/statistics-basic-1.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
require'statfns'
|
||||
(mean,stddev) ?1000#0
|
||||
0.484669 0.287482
|
||||
(mean,stddev) ?10000#0
|
||||
0.503642 0.290777
|
||||
(mean,stddev) ?100000#0
|
||||
0.499677 0.288726
|
||||
3
Task/Statistics-Basic/J/statistics-basic-2.j
Normal file
3
Task/Statistics-Basic/J/statistics-basic-2.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
|
||||
require'plot'
|
||||
plot ((%*1+i.)100) ([;histogram) ?10000#0
|
||||
23
Task/Statistics-Basic/J/statistics-basic-3.j
Normal file
23
Task/Statistics-Basic/J/statistics-basic-3.j
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
|
||||
|
||||
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. 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
|
||||
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
|
||||
)
|
||||
36
Task/Statistics-Basic/J/statistics-basic-4.j
Normal file
36
Task/Statistics-Basic/J/statistics-basic-4.j
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
meanstddevP 1000
|
||||
#############################
|
||||
####################################
|
||||
###########################
|
||||
##############################
|
||||
###################################
|
||||
########################
|
||||
###########################
|
||||
############################
|
||||
################################
|
||||
##########################
|
||||
0.488441 0.289744
|
||||
meanstddevP 10000
|
||||
##############################
|
||||
##############################
|
||||
#############################
|
||||
#############################
|
||||
###############################
|
||||
##############################
|
||||
############################
|
||||
##############################
|
||||
#############################
|
||||
#############################
|
||||
0.49697 0.289433
|
||||
meanstddevP 100000
|
||||
#############################
|
||||
##############################
|
||||
#############################
|
||||
#############################
|
||||
#############################
|
||||
##############################
|
||||
##############################
|
||||
##############################
|
||||
##############################
|
||||
#############################
|
||||
0.500872 0.288241
|
||||
41
Task/Statistics-Basic/Liberty-BASIC/statistics-basic.liberty
Normal file
41
Task/Statistics-Basic/Liberty-BASIC/statistics-basic.liberty
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
call sample 100
|
||||
call sample 1000
|
||||
call sample 10000
|
||||
|
||||
end
|
||||
|
||||
sub sample n
|
||||
dim dat( n)
|
||||
for i =1 to n
|
||||
dat( i) =rnd( 1)
|
||||
next i
|
||||
|
||||
'// show mean, standard deviation
|
||||
sum =0
|
||||
sSq =0
|
||||
for i =1 to n
|
||||
sum =sum +dat( i)
|
||||
sSq =sSq +dat( i)^2
|
||||
next i
|
||||
print n; " data terms used."
|
||||
|
||||
mean =sum / n
|
||||
print "Mean ="; mean
|
||||
|
||||
print "Stddev ="; ( sSq /n -mean^2)^0.5
|
||||
|
||||
'// show histogram
|
||||
nBins =10
|
||||
dim bins( nBins)
|
||||
for i =1 to n
|
||||
z =int( nBins *dat( i))
|
||||
bins( z) =bins( z) +1
|
||||
next i
|
||||
for b =0 to nBins -1
|
||||
for j =1 to int( nBins *bins( b)) /n *70)
|
||||
print "#";
|
||||
next j
|
||||
print
|
||||
next b
|
||||
print
|
||||
end sub
|
||||
20
Task/Statistics-Basic/MATLAB/statistics-basic.m
Normal file
20
Task/Statistics-Basic/MATLAB/statistics-basic.m
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
% Initialize
|
||||
N = 0; S=0; S2 = 0;
|
||||
binlist = 0:.1:1;
|
||||
h = zeros(1,length(binlist)); % initialize histogram
|
||||
|
||||
% read data and perform computation
|
||||
while (1)
|
||||
% read next sample x
|
||||
if (no_data_available) break; end;
|
||||
N = N + 1;
|
||||
S = S + x;
|
||||
S2= S2+ x*x;
|
||||
ix= sum(x < binlist);
|
||||
h(ix) = h(ix)+1;
|
||||
end
|
||||
|
||||
% generate output
|
||||
m = S/N; % mean
|
||||
sd = sqrt(S2/N-mean*mean); % standard deviation
|
||||
bar(binlist,h)
|
||||
4
Task/Statistics-Basic/Mathematica/statistics-basic.math
Normal file
4
Task/Statistics-Basic/Mathematica/statistics-basic.math
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Sample[n_]:= (Print[#//Length," numbers, Mean : ",#//Mean,", StandardDeviation : ",#//StandardDeviation ];
|
||||
BarChart[BinCounts[#,{0,1,.1}], Axes->False, BarOrigin->Left])&[(RandomReal[1,#])&[ n ]]
|
||||
|
||||
Sample/@{100,1 000,10 000,1 000 000}
|
||||
21
Task/Statistics-Basic/PARI-GP/statistics-basic-1.pari
Normal file
21
Task/Statistics-Basic/PARI-GP/statistics-basic-1.pari
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
mean(v)={
|
||||
sum(i=1,#v,v[i])/#v
|
||||
};
|
||||
stdev(v,mu="")={
|
||||
if(mu=="",mu=mean(v));
|
||||
sqrt(sum(i=1,#v,(v[i]-mu)^2))/#v
|
||||
};
|
||||
histogram(v,bins=16,low=0,high=1)={
|
||||
my(u=vector(bins),width=(high-low)/bins);
|
||||
for(i=1,#v,u[(v[i]-low)\width+1]++);
|
||||
u
|
||||
};
|
||||
show(n)={
|
||||
my(v=vector(n,i,random(1.)),mu=mean(v),s=stdev(v,mu),h=histogram(v),sz=ceil(n/50/16));
|
||||
for(i=1,16,for(j=1,h[i]\sz,print1("#"));print());
|
||||
print("Mean: "mu);
|
||||
print("Stdev: "s);
|
||||
};
|
||||
show(100);
|
||||
show(1000);
|
||||
show(10000);
|
||||
4
Task/Statistics-Basic/PARI-GP/statistics-basic-2.pari
Normal file
4
Task/Statistics-Basic/PARI-GP/statistics-basic-2.pari
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
rreal()={
|
||||
my(pr=32*ceil(default(realprecision)*log(10)/log(4294967296))); \\ Current precision
|
||||
random(2^pr)*1.>>pr
|
||||
};
|
||||
39
Task/Statistics-Basic/PL-I/statistics-basic.pli
Normal file
39
Task/Statistics-Basic/PL-I/statistics-basic.pli
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
stat: procedure options (main); /* 21 May 2014 */
|
||||
|
||||
stats: procedure (values, mean, standard_deviation);
|
||||
declare (values(*), mean, standard_deviation) float;
|
||||
declare n fixed binary (31) initial ( (hbound(values,1)) );
|
||||
|
||||
mean = sum(values)/n;
|
||||
|
||||
standard_deviation = sqrt( sum(values - mean)**2 / n);
|
||||
|
||||
end stats;
|
||||
|
||||
declare values (*) float controlled;
|
||||
declare (mean, stddev) float;
|
||||
declare bin(0:9) fixed;
|
||||
declare (i, n) fixed binary (31);
|
||||
|
||||
do n = 100, 1000, 10000, 100000;
|
||||
allocate values(n);
|
||||
values = random();
|
||||
call stats (values, mean, stddev);
|
||||
|
||||
if n = 100 then
|
||||
do;
|
||||
bin = 0;
|
||||
do i = 1 to 100;
|
||||
bin(10*values(i)) += 1;
|
||||
end;
|
||||
put skip list ('Histogram for 100 values:');
|
||||
do i = 0 to 9; /* display histogram */
|
||||
put skip list (repeat('.', bin(i)) );
|
||||
end;
|
||||
end;
|
||||
|
||||
put skip list (n || ' values: mean=' || mean, 'stddev=' || stddev);
|
||||
free values;
|
||||
end;
|
||||
|
||||
end stat;
|
||||
10
Task/Statistics-Basic/Perl-6/statistics-basic.pl6
Normal file
10
Task/Statistics-Basic/Perl-6/statistics-basic.pl6
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
for 100, 1_000, 10_000 -> $N {
|
||||
say "size: $N";
|
||||
my @data = rand xx $N;
|
||||
printf "mean: %f\n", my $mean = $N R/ [+] @data;
|
||||
printf "stddev: %f\n", sqrt
|
||||
$mean**2 R- $N R/ [+] @data »**» 2;
|
||||
printf "%.1f %s\n", .key, '=' x (500 * .value.elems / $N)
|
||||
for sort *.key, @data.classify: (10 * *).Int / 10;
|
||||
say;
|
||||
}
|
||||
24
Task/Statistics-Basic/Perl/statistics-basic.pl
Normal file
24
Task/Statistics-Basic/Perl/statistics-basic.pl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
my @histogram = (0) x 10;
|
||||
my $sum = 0;
|
||||
my $sum_squares = 0;
|
||||
my $n = $ARGV[0];
|
||||
|
||||
for (1..$n) {
|
||||
my $current = rand();
|
||||
$sum+= $current;
|
||||
$sum_squares+= $current ** 2;
|
||||
$histogram[$current * @histogram]+= 1;
|
||||
}
|
||||
|
||||
my $mean = $sum / $n;
|
||||
|
||||
print "$n numbers\n",
|
||||
"Mean: $mean\n",
|
||||
"Stddev: ", sqrt(($sum_squares / $n) - ($mean ** 2)), "\n";
|
||||
|
||||
for my $i (0..$#histogram) {
|
||||
printf "%.1f - %.1f : ", $i/@histogram, (1 + $i)/@histogram;
|
||||
|
||||
print "*" x (30 * $histogram[$i] * @histogram/$n); # 30 stars expected per row
|
||||
print "\n";
|
||||
}
|
||||
21
Task/Statistics-Basic/PicoLisp/statistics-basic-1.l
Normal file
21
Task/Statistics-Basic/PicoLisp/statistics-basic-1.l
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(scl 6)
|
||||
|
||||
(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) ) ) )
|
||||
11
Task/Statistics-Basic/PicoLisp/statistics-basic-2.l
Normal file
11
Task/Statistics-Basic/PicoLisp/statistics-basic-2.l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(statistics 100
|
||||
(rand 0 (dec 1.0)) )
|
||||
(prinl)
|
||||
|
||||
(statistics 10000
|
||||
(rand 0 (dec 1.0)) )
|
||||
(prinl)
|
||||
|
||||
(statistics 1000000
|
||||
(rand 0 (dec 1.0)) )
|
||||
(prinl)
|
||||
56
Task/Statistics-Basic/PureBasic/statistics-basic.purebasic
Normal file
56
Task/Statistics-Basic/PureBasic/statistics-basic.purebasic
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Procedure.f randomf()
|
||||
#RNG_max_resolution = 2147483647
|
||||
ProcedureReturn Random(#RNG_max_resolution) / #RNG_max_resolution
|
||||
EndProcedure
|
||||
|
||||
Procedure sample(n)
|
||||
Protected i, nBins, binNumber, tickMarks, maxBinValue
|
||||
Protected.f sum, sumSq, mean
|
||||
|
||||
Dim dat.f(n)
|
||||
For i = 1 To n
|
||||
dat(i) = randomf()
|
||||
Next
|
||||
|
||||
;show mean, standard deviation
|
||||
For i = 1 To n
|
||||
sum + dat(i)
|
||||
sumSq + dat(i) * dat(i)
|
||||
Next i
|
||||
|
||||
PrintN(Str(n) + " data terms used.")
|
||||
mean = sum / n
|
||||
PrintN("Mean =" + StrF(mean))
|
||||
PrintN("Stddev =" + StrF((sumSq / n) - Sqr(mean * mean)))
|
||||
|
||||
;show histogram
|
||||
nBins = 10
|
||||
Dim bins(nBins)
|
||||
For i = 1 To n
|
||||
binNumber = Int(nBins * dat(i))
|
||||
bins(binNumber) + 1
|
||||
Next
|
||||
|
||||
maxBinValue = 1
|
||||
For i = 0 To nBins
|
||||
If bins(i) > maxBinValue
|
||||
maxBinValue = bins(i)
|
||||
EndIf
|
||||
Next
|
||||
|
||||
#normalizedMaxValue = 70
|
||||
For binNumber = 0 To nBins
|
||||
tickMarks = Int(bins(binNumber) * #normalizedMaxValue / maxBinValue)
|
||||
PrintN(ReplaceString(Space(tickMarks), " ", "#"))
|
||||
Next
|
||||
PrintN("")
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
sample(100)
|
||||
sample(1000)
|
||||
sample(10000)
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
39
Task/Statistics-Basic/Python/statistics-basic.py
Normal file
39
Task/Statistics-Basic/Python/statistics-basic.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
def sd1(numbers):
|
||||
if numbers:
|
||||
mean = sum(numbers) / len(numbers)
|
||||
sd = (sum((n - mean)**2 for n in numbers) / len(numbers))**0.5
|
||||
return sd, mean
|
||||
else:
|
||||
return 0, 0
|
||||
|
||||
def sd2(numbers):
|
||||
if numbers:
|
||||
sx = sxx = n = 0
|
||||
for x in numbers:
|
||||
sx += x
|
||||
sxx += x*x
|
||||
n += 1
|
||||
sd = (n * sxx - sx*sx)**0.5 / n
|
||||
return sd, sx / n
|
||||
else:
|
||||
return 0, 0
|
||||
|
||||
def histogram(numbers):
|
||||
h = [0] * 10
|
||||
maxwidth = 50 # characters
|
||||
for n in numbers:
|
||||
h[int(n*10)] += 1
|
||||
mx = max(h)
|
||||
print()
|
||||
for n, i in enumerate(h):
|
||||
print('%3.1f: %s' % (n / 10, '+' * int(i / mx * maxwidth)))
|
||||
print()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import random
|
||||
for i in range(1, 6):
|
||||
n = [random.random() for j in range(10**i)]
|
||||
print("\n##\n## %i numbers\n##" % 10**i)
|
||||
print(' Naive method: sd: %8.6f, mean: %8.6f' % sd1(n))
|
||||
print(' Second method: sd: %8.6f, mean: %8.6f' % sd2(n))
|
||||
histogram(n)
|
||||
1
Task/Statistics-Basic/README
Normal file
1
Task/Statistics-Basic/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Statistics/Basic
|
||||
38
Task/Statistics-Basic/REXX/statistics-basic.rexx
Normal file
38
Task/Statistics-Basic/REXX/statistics-basic.rexx
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*REXX pgm gens some random #s, shows bin histogram, finds mean & stdDev*/
|
||||
numeric digits 20 /*use twenty digits precision, */
|
||||
showDigs=digits()%2 /* ··· but only show ten digits.*/
|
||||
parse arg size seed . /*allow specification: size, seed*/
|
||||
if size=='' | size==',' then size=100 /*if not specified, then use 100.*/
|
||||
if datatype(seed,'W') then call random ,,seed /*allow seed for RAND.*/
|
||||
#.=0 /*count of numbers in each bin. */
|
||||
do j=1 for size /*generate some random numbers. */
|
||||
@.j=random(0,99999)/100000 /*express as a fraction.*/
|
||||
_=substr(@.j'00',3,1) /*determine which bin it's 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 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.*/
|
||||
end /*k*/
|
||||
say
|
||||
say 'sample size = ' size; say
|
||||
avg=mean(size) ; say ' mean = ' format(avg,,showDigs)
|
||||
stddev=stddev(size); say ' stddev = ' format(stddev,,showDigs)
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────MEAN subroutine─────────────────────*/
|
||||
mean: parse arg N .; $=0; do m=1 for N; $=$+@.m; end /*m*/
|
||||
return $/n
|
||||
/*──────────────────────────────────STDDEV subroutine───────────────────*/
|
||||
stddev: parse arg N .; $=0; do s=1 for N; $=$+(@.s-avg)**2; end /*s*/
|
||||
return sqrt($/n)
|
||||
/*──────────────────────────────────SQRT subroutine─────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits()
|
||||
numeric digits 11; numeric form; m.=11; p=d+d%4+2
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2
|
||||
do j=0 while p>9; m.j=p; p=p%2+1; end
|
||||
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end
|
||||
numeric digits d; return g/1
|
||||
21
Task/Statistics-Basic/Racket/statistics-basic.rkt
Normal file
21
Task/Statistics-Basic/Racket/statistics-basic.rkt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#lang racket
|
||||
(require math (only-in srfi/27 random-real))
|
||||
|
||||
(define (histogram n xs Δx)
|
||||
(define (r x) (~r x #:precision 1 #:min-width 3))
|
||||
(define (len count) (exact-floor (/ (* count 200) n)))
|
||||
(for ([b (bin-samples (range 0 1 Δx) <= xs)])
|
||||
(displayln (~a (r (sample-bin-min b)) "-" (r (sample-bin-max b)) ": "
|
||||
(make-string (len (length (sample-bin-values b))) #\*)))))
|
||||
|
||||
(define (task n)
|
||||
(define xs (for/list ([_ n]) (random-real)))
|
||||
(displayln (~a "Number of samples: " n))
|
||||
(displayln (~a "Mean: " (mean xs)))
|
||||
(displayln (~a "Standard deviance: " (stddev xs)))
|
||||
(histogram n xs 0.1)
|
||||
(newline))
|
||||
|
||||
(task 100)
|
||||
(task 1000)
|
||||
(task 10000)
|
||||
20
Task/Statistics-Basic/Ruby/statistics-basic.rb
Normal file
20
Task/Statistics-Basic/Ruby/statistics-basic.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def generate_statistics(n)
|
||||
sum = sum2 = 0.0
|
||||
hist = Array.new(10, 0)
|
||||
n.times do
|
||||
r = rand
|
||||
sum += r
|
||||
sum2 += r**2
|
||||
hist[(10*r).to_i] += 1
|
||||
end
|
||||
mean = sum / n
|
||||
stddev = Math::sqrt((sum2 / n) - mean**2)
|
||||
|
||||
puts "size: #{n}"
|
||||
puts "mean: #{mean}"
|
||||
puts "stddev: #{stddev}"
|
||||
hist.each_with_index {|x,i| puts "%.1f:%s" % [0.1*i, "=" * (70*x/hist.max)]}
|
||||
puts
|
||||
end
|
||||
|
||||
[100, 1000, 10000].each {|n| generate_statistics n}
|
||||
21
Task/Statistics-Basic/Scala/statistics-basic.scala
Normal file
21
Task/Statistics-Basic/Scala/statistics-basic.scala
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
def mean(a:Array[Double])=a.sum / a.size
|
||||
def stddev(a:Array[Double])={
|
||||
val sum = a.fold(0.0)((a, b) => a + math.pow(b,2))
|
||||
math.sqrt((sum/a.size) - math.pow(mean(a),2))
|
||||
}
|
||||
def hist(a:Array[Double]) = {
|
||||
val grouped=(SortedMap[Double, Array[Double]]() ++ (a groupBy (x => math.rint(x*10)/10)))
|
||||
grouped.map(v => (v._1, v._2.size))
|
||||
}
|
||||
def printHist(a:Array[Double])=for((g,v) <- hist(a)){
|
||||
println(s"$g: ${"*"*(205*v/a.size)} $v")
|
||||
}
|
||||
|
||||
for(n <- Seq(100,1000,10000)){
|
||||
val a = Array.fill(n)(Random.nextDouble)
|
||||
println(s"$n numbers")
|
||||
println(s"Mean: ${mean(a)}")
|
||||
println(s"StdDev: ${stddev(a)}")
|
||||
printHist(a)
|
||||
println
|
||||
}
|
||||
27
Task/Statistics-Basic/Tcl/statistics-basic.tcl
Normal file
27
Task/Statistics-Basic/Tcl/statistics-basic.tcl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package require Tcl 8.5
|
||||
proc stats {size} {
|
||||
set sum 0.0
|
||||
set sum2 0.0
|
||||
for {set i 0} {$i < $size} {incr i} {
|
||||
set r [expr {rand()}]
|
||||
|
||||
incr histo([expr {int(floor($r*10))}])
|
||||
set sum [expr {$sum + $r}]
|
||||
set sum2 [expr {$sum2 + $r**2}]
|
||||
}
|
||||
set mean [expr {$sum / $size}]
|
||||
set stddev [expr {sqrt($sum2/$size - $mean**2)}]
|
||||
puts "$size numbers"
|
||||
puts "Mean: $mean"
|
||||
puts "StdDev: $stddev"
|
||||
foreach i {0 1 2 3 4 5 6 7 8 9} {
|
||||
# The 205 is a magic factor stolen from the Go solution
|
||||
puts [string repeat "*" [expr {$histo($i)*205/int($size)}]]
|
||||
}
|
||||
}
|
||||
|
||||
stats 100
|
||||
puts ""
|
||||
stats 1000
|
||||
puts ""
|
||||
stats 10000
|
||||
Loading…
Add table
Add a link
Reference in a new issue