Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Statistics-Normal-distribution/00-META.yaml
Normal file
2
Task/Statistics-Normal-distribution/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Statistics/Normal_distribution
|
||||
12
Task/Statistics-Normal-distribution/00-TASK.txt
Normal file
12
Task/Statistics-Normal-distribution/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
The [[wp:Normal distribution|Normal]] (or Gaussian) distribution is a frequently used distribution in statistics. While most programming languages provide a uniformly distributed random number generator, one can [[wp:Normal distribution#Generating_values_from_normal_distribution|derive]] normally distributed random numbers from a uniform generator.
|
||||
|
||||
|
||||
;The task:
|
||||
# Take a uniform random number generator and create a large (you decide how large) set of numbers that follow a normal (Gaussian) distribution. Calculate the dataset's mean and standard deviation, and show a histogram of the data.
|
||||
# Mention any native language support for the generation of normally distributed random numbers.
|
||||
|
||||
|
||||
;Reference:
|
||||
* You may refer to code in [[Statistics/Basic]] if available.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#include <random>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
|
||||
int main( ) {
|
||||
std::random_device myseed ;
|
||||
std::mt19937 engine ( myseed( ) ) ;
|
||||
std::normal_distribution<> normDistri ( 2 , 3 ) ;
|
||||
std::map<int , int> normalFreq ;
|
||||
int sum = 0 ; //holds the sum of the randomly created numbers
|
||||
double mean = 0.0 ;
|
||||
double stddev = 0.0 ;
|
||||
for ( int i = 1 ; i < 10001 ; i++ )
|
||||
++normalFreq[ normDistri ( engine ) ] ;
|
||||
for ( auto MapIt : normalFreq ) {
|
||||
sum += MapIt.first * MapIt.second ;
|
||||
}
|
||||
mean = sum / 10000 ;
|
||||
stddev = sqrt( sum / 10000 ) ;
|
||||
std::cout << "The mean of the distribution is " << mean << " , the " ;
|
||||
std::cout << "standard deviation " << stddev << " !\n" ;
|
||||
std::cout << "And now the histogram:\n" ;
|
||||
for ( auto MapIt : normalFreq ) {
|
||||
std::cout << std::left << std::setw( 4 ) << MapIt.first <<
|
||||
std::string( MapIt.second / 100 , '*' ) << std::endl ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using MathNet.Numerics.Distributions;
|
||||
using MathNet.Numerics.Statistics;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void RunNormal(int sampleSize)
|
||||
{
|
||||
double[] X = new double[sampleSize];
|
||||
var norm = new Normal(new Random());
|
||||
norm.Samples(X);
|
||||
|
||||
const int numBuckets = 10;
|
||||
var histogram = new Histogram(X, numBuckets);
|
||||
Console.WriteLine("Sample size: {0:N0}", sampleSize);
|
||||
for (int i = 0; i < numBuckets; i++)
|
||||
{
|
||||
string bar = new String('#', (int)(histogram[i].Count * 360 / sampleSize));
|
||||
Console.WriteLine(" {0:0.00} : {1}", histogram[i].LowerBound, bar);
|
||||
}
|
||||
var statistics = new DescriptiveStatistics(X);
|
||||
Console.WriteLine(" Mean: " + statistics.Mean);
|
||||
Console.WriteLine("StdDev: " + statistics.StandardDeviation);
|
||||
Console.WriteLine();
|
||||
}
|
||||
static void Main(string[] args)
|
||||
{
|
||||
RunNormal(100);
|
||||
RunNormal(1000);
|
||||
RunNormal(10000);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* RosettaCode example: Statistics/Normal distribution in C
|
||||
*
|
||||
* The random number generator rand() of the standard C library is obsolete
|
||||
* and should not be used in more demanding applications. There are plenty
|
||||
* libraries with advanced features (eg. GSL) with functions to calculate
|
||||
* the mean, the standard deviation, generating random numbers etc.
|
||||
* However, these features are not the core of the standard C library.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
#define NMAX 10000000
|
||||
|
||||
|
||||
double mean(double* values, int n)
|
||||
{
|
||||
int i;
|
||||
double s = 0;
|
||||
|
||||
for ( i = 0; i < n; i++ )
|
||||
s += values[i];
|
||||
return s / n;
|
||||
}
|
||||
|
||||
|
||||
double stddev(double* values, int n)
|
||||
{
|
||||
int i;
|
||||
double average = mean(values,n);
|
||||
double s = 0;
|
||||
|
||||
for ( i = 0; i < n; i++ )
|
||||
s += (values[i] - average) * (values[i] - average);
|
||||
return sqrt(s / (n - 1));
|
||||
}
|
||||
|
||||
/*
|
||||
* Normal random numbers generator - Marsaglia algorithm.
|
||||
*/
|
||||
double* generate(int n)
|
||||
{
|
||||
int i;
|
||||
int m = n + n % 2;
|
||||
double* values = (double*)calloc(m,sizeof(double));
|
||||
double average, deviation;
|
||||
|
||||
if ( values )
|
||||
{
|
||||
for ( i = 0; i < m; i += 2 )
|
||||
{
|
||||
double x,y,rsq,f;
|
||||
do {
|
||||
x = 2.0 * rand() / (double)RAND_MAX - 1.0;
|
||||
y = 2.0 * rand() / (double)RAND_MAX - 1.0;
|
||||
rsq = x * x + y * y;
|
||||
}while( rsq >= 1. || rsq == 0. );
|
||||
f = sqrt( -2.0 * log(rsq) / rsq );
|
||||
values[i] = x * f;
|
||||
values[i+1] = y * f;
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
void printHistogram(double* values, int n)
|
||||
{
|
||||
const int width = 50;
|
||||
int max = 0;
|
||||
|
||||
const double low = -3.05;
|
||||
const double high = 3.05;
|
||||
const double delta = 0.1;
|
||||
|
||||
int i,j,k;
|
||||
int nbins = (int)((high - low) / delta);
|
||||
int* bins = (int*)calloc(nbins,sizeof(int));
|
||||
if ( bins != NULL )
|
||||
{
|
||||
for ( i = 0; i < n; i++ )
|
||||
{
|
||||
int j = (int)( (values[i] - low) / delta );
|
||||
if ( 0 <= j && j < nbins )
|
||||
bins[j]++;
|
||||
}
|
||||
|
||||
for ( j = 0; j < nbins; j++ )
|
||||
if ( max < bins[j] )
|
||||
max = bins[j];
|
||||
|
||||
for ( j = 0; j < nbins; j++ )
|
||||
{
|
||||
printf("(%5.2f, %5.2f) |", low + j * delta, low + (j + 1) * delta );
|
||||
k = (int)( (double)width * (double)bins[j] / (double)max );
|
||||
while(k-- > 0) putchar('*');
|
||||
printf(" %-.1f%%", bins[j] * 100.0 / (double)n);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
free(bins);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double* seq;
|
||||
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
if ( (seq = generate(NMAX)) != NULL )
|
||||
{
|
||||
printf("mean = %g, stddev = %g\n\n", mean(seq,NMAX), stddev(seq,NMAX));
|
||||
printHistogram(seq,NMAX);
|
||||
free(seq);
|
||||
|
||||
printf("\n%s\n", "press enter");
|
||||
getchar();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import std.stdio, std.random, std.math, std.range, std.algorithm,
|
||||
statistics_basic;
|
||||
|
||||
struct Normals {
|
||||
double mu, sigma;
|
||||
double[2] state;
|
||||
size_t index = state.length;
|
||||
enum empty = false;
|
||||
|
||||
void popFront() pure nothrow { index++; }
|
||||
|
||||
@property double front() {
|
||||
if (index >= state.length) {
|
||||
immutable r = sqrt(-2 * uniform!"]["(0., 1.0).log) * sigma;
|
||||
immutable x = 2 * PI * uniform01;
|
||||
state = [mu + r * x.sin, mu + r * x.cos];
|
||||
index = 0;
|
||||
}
|
||||
return state[index];
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
const data = Normals(0.0, 0.5).take(100_000).array;
|
||||
writefln("Mean: %8.6f, SD: %8.6f\n", data.meanStdDev[]);
|
||||
data.map!q{ max(0.0, min(0.9999, a / 3 + 0.5)) }.showHistogram01;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
defmodule Statistics do
|
||||
def normal_distribution(n, w\\5) do
|
||||
{sum, sum2, hist} = generate(n, w)
|
||||
mean = sum / n
|
||||
stddev = :math.sqrt(sum2 / n - mean*mean)
|
||||
|
||||
IO.puts "size: #{n}"
|
||||
IO.puts "mean: #{mean}"
|
||||
IO.puts "stddev: #{stddev}"
|
||||
{min, max} = Map.to_list(hist)
|
||||
|> Enum.filter_map(fn {_k,v} -> v >= n/120/w end, fn {k,_v} -> k end)
|
||||
|> Enum.min_max
|
||||
Enum.each(min..max, fn i ->
|
||||
bar = String.duplicate("=", trunc(120 * w * Map.get(hist, i, 0) / n))
|
||||
:io.fwrite "~4.1f: ~s~n", [i/w, bar]
|
||||
end)
|
||||
IO.puts ""
|
||||
end
|
||||
|
||||
defp generate(n, w) do
|
||||
Enum.reduce(1..n, {0, 0, %{}}, fn _,{sum, sum2, hist} ->
|
||||
z = :rand.normal
|
||||
{sum+z, sum2+z*z, Map.update(hist, round(w*z), 1, &(&1+1))}
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each([100,1000,10000], fn n ->
|
||||
Statistics.normal_distribution(n)
|
||||
end)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
USING: assocs formatting kernel math math.functions
|
||||
math.statistics random sequences sorting ;
|
||||
|
||||
2,000,000 [ 0 1 normal-random-float ] replicate ! make data set
|
||||
dup [ mean ] [ population-std ] bi ! calculate and show
|
||||
"Mean: %f\nStdev: %f\n\n" printf ! mean and stddev
|
||||
|
||||
[ 10 * floor 10 / ] map ! map data to buckets
|
||||
histogram >alist [ first ] sort-with ! create histogram sorted by bucket (key)
|
||||
dup values supremum ! find maximum count
|
||||
[
|
||||
[ /f 100 * >integer ] keepd ! how big should this histogram bar be?
|
||||
[ [ CHAR: * ] "" replicate-as ] dip ! make the bar
|
||||
"% 5.2f: %s %d\n" printf ! print a line of the histogram
|
||||
] curry assoc-each
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
program Normal_Distribution
|
||||
implicit none
|
||||
|
||||
integer, parameter :: i64 = selected_int_kind(18)
|
||||
integer, parameter :: r64 = selected_real_kind(15)
|
||||
integer(i64), parameter :: samples = 1000000_i64
|
||||
real(r64) :: mean, stddev
|
||||
real(r64) :: sumn = 0, sumnsq = 0
|
||||
integer(i64) :: n = 0
|
||||
integer(i64) :: bin(-50:50) = 0
|
||||
integer :: i, ind
|
||||
real(r64) :: ur1, ur2, nr1, nr2, s
|
||||
|
||||
n = 0
|
||||
do while(n <= samples)
|
||||
call random_number(ur1)
|
||||
call random_number(ur2)
|
||||
ur1 = ur1 * 2.0 - 1.0
|
||||
ur2 = ur2 * 2.0 - 1.0
|
||||
|
||||
s = ur1*ur1 + ur2*ur2
|
||||
if(s >= 1.0_r64) cycle
|
||||
|
||||
nr1 = ur1 * sqrt(-2.0*log(s)/s)
|
||||
ind = floor(5.0*nr1)
|
||||
bin(ind) = bin(ind) + 1_i64
|
||||
sumn = sumn + nr1
|
||||
sumnsq = sumnsq + nr1*nr1
|
||||
|
||||
nr2 = ur2 * sqrt(-2.0*log(s)/s)
|
||||
ind = floor(5.0*nr2)
|
||||
bin(ind) = bin(ind) + 1_i64
|
||||
sumn = sumn + nr2
|
||||
sumnsq = sumnsq + nr2*nr2
|
||||
n = n + 2_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 = -15, 15
|
||||
write(*, "(f4.1, a, a)") real(i)/5.0, ": ", repeat("=", int(bin(i)*500/samples))
|
||||
end do
|
||||
|
||||
end program
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Const pi As Double = 3.141592653589793
|
||||
Randomize
|
||||
|
||||
' Generates normally distributed random numbers with mean 0 and standard deviation 1
|
||||
Function randomNormal() As Double
|
||||
Return Cos(2.0 * pi * Rnd) * Sqr(-2.0 * Log(Rnd))
|
||||
End Function
|
||||
|
||||
Sub normalStats(sampleSize As Integer)
|
||||
If sampleSize < 1 Then Return
|
||||
Dim r(1 To sampleSize) As Double
|
||||
Dim h(-1 To 10) As Integer '' all zero by default
|
||||
Dim sum As Double = 0.0
|
||||
Dim hSum As Integer = 0
|
||||
|
||||
' Generate 'sampleSize' normally distributed random numbers with mean 0.5 and standard deviation 0.25
|
||||
' calculate their sum
|
||||
' and in which box they will fall when drawing the histogram
|
||||
For i As Integer = 1 To sampleSize
|
||||
r(i) = 0.5 + randomNormal / 4.0
|
||||
sum += r(i)
|
||||
If r(i) < 0.0 Then
|
||||
h(-1) += 1
|
||||
ElseIf r(i) >= 1.0 Then
|
||||
h(10) += 1
|
||||
Else
|
||||
h(Int(r(i) * 10)) += 1
|
||||
End If
|
||||
Next
|
||||
|
||||
For i As Integer = -1 To 10 : hSum += h(i) : Next
|
||||
' adjust one of the h() values if necessary to ensure hSum = sampleSize
|
||||
Dim adj As Integer = sampleSize - hSum
|
||||
If adj <> 0 Then
|
||||
For i As Integer = -1 To 10
|
||||
h(i) += adj
|
||||
If h(i) >= 0 Then Exit For
|
||||
h(i) -= adj
|
||||
Next
|
||||
End If
|
||||
|
||||
Dim mean As Double = sum / sampleSize
|
||||
|
||||
Dim sd As Double
|
||||
sum = 0.0
|
||||
' Now calculate their standard deviation
|
||||
For i As Integer = 1 To sampleSize
|
||||
sum += (r(i) - mean) ^ 2.0
|
||||
Next
|
||||
sd = Sqr(sum/sampleSize)
|
||||
|
||||
' Draw a histogram of the data with interval 0.1
|
||||
Dim numStars As Integer
|
||||
' If sample size > 300 then normalize histogram to 300
|
||||
Dim scale As Double = 1.0
|
||||
If sampleSize > 300 Then scale = 300.0 / sampleSize
|
||||
Print "Sample size "; sampleSize
|
||||
Print
|
||||
Print Using " Mean #.######"; mean;
|
||||
Print Using " SD #.######"; sd
|
||||
Print
|
||||
For i As Integer = -1 To 10
|
||||
If i = -1 Then
|
||||
Print Using "< 0.00 : ";
|
||||
ElseIf i = 10 Then
|
||||
Print Using ">=1.00 : ";
|
||||
Else
|
||||
Print Using " #.## : "; i/10.0;
|
||||
End If
|
||||
Print Using "##### " ; h(i);
|
||||
numStars = Int(h(i) * scale + 0.5)
|
||||
Print String(numStars, "*")
|
||||
Next
|
||||
End Sub
|
||||
|
||||
normalStats 100
|
||||
Print
|
||||
normalStats 1000
|
||||
Print
|
||||
normalStats 10000
|
||||
Print
|
||||
normalStats 100000
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Box-Muller
|
||||
func norm2() (s, c float64) {
|
||||
r := math.Sqrt(-2 * math.Log(rand.Float64()))
|
||||
s, c = math.Sincos(2 * math.Pi * rand.Float64())
|
||||
return s * r, c * r
|
||||
}
|
||||
|
||||
func main() {
|
||||
const (
|
||||
n = 10000
|
||||
bins = 12
|
||||
sig = 3
|
||||
scale = 100
|
||||
)
|
||||
var sum, sumSq float64
|
||||
h := make([]int, bins)
|
||||
for i, accum := 0, func(v float64) {
|
||||
sum += v
|
||||
sumSq += v * v
|
||||
b := int((v + sig) * bins / sig / 2)
|
||||
if b >= 0 && b < bins {
|
||||
h[b]++
|
||||
}
|
||||
}; i < n/2; i++ {
|
||||
v1, v2 := norm2()
|
||||
accum(v1)
|
||||
accum(v2)
|
||||
}
|
||||
m := sum / n
|
||||
fmt.Println("mean:", m)
|
||||
fmt.Println("stddev:", math.Sqrt(sumSq/float64(n)-m*m))
|
||||
for _, p := range h {
|
||||
fmt.Println(strings.Repeat("*", p/scale))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
import Data.Map (Map, empty, insert, findWithDefault, toList)
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Text.Printf (printf)
|
||||
import Data.Function (on)
|
||||
import Data.List (sort, maximumBy, minimumBy)
|
||||
import Control.Monad.Random (RandomGen, Rand, evalRandIO, getRandomR)
|
||||
import Control.Monad (replicateM)
|
||||
|
||||
-- Box-Muller
|
||||
getNorm :: RandomGen g => Rand g Double
|
||||
getNorm = do
|
||||
u0 <- getRandomR (0.0, 1.0)
|
||||
u1 <- getRandomR (0.0, 1.0)
|
||||
let r = sqrt $ (-2.0) * log u0
|
||||
theta = 2.0 * pi * u1
|
||||
return $ r * sin theta
|
||||
|
||||
putInBin :: Double -> Map Int Int -> Double -> Map Int Int
|
||||
putInBin binWidth t v =
|
||||
let bin = round (v / binWidth)
|
||||
count = findWithDefault 0 bin t
|
||||
in insert bin (count+1) t
|
||||
|
||||
runTest :: Int -> IO ()
|
||||
runTest n = do
|
||||
rs <- evalRandIO $ replicateM n getNorm
|
||||
let binWidth = 0.1
|
||||
|
||||
tally v (sv, sv2, t) = (sv+v, sv2 + v*v, putInBin binWidth t v)
|
||||
|
||||
(sum, sum2, tallies) = foldr tally (0.0, 0.0, empty) rs
|
||||
|
||||
tallyList = sort $ toList tallies
|
||||
|
||||
printStars tallies binWidth maxCount selection =
|
||||
let count = findWithDefault 0 selection tallies
|
||||
bin = binWidth * fromIntegral selection
|
||||
maxStars = 100
|
||||
starCount = if maxCount <= maxStars
|
||||
then count
|
||||
else maxStars * count `div` maxCount
|
||||
stars = replicate starCount '*'
|
||||
in printf "%5.2f: %s %d\n" bin stars count
|
||||
|
||||
mean = sum / fromIntegral n
|
||||
stddev = sqrt (sum2/fromIntegral n - mean*mean)
|
||||
|
||||
printf "\n"
|
||||
printf "sample count: %d\n" n
|
||||
printf "mean: %9.7f\n" mean
|
||||
printf "stddev: %9.7f\n" stddev
|
||||
|
||||
let maxCount = snd $ maximumBy (compare `on` snd) tallyList
|
||||
maxBin = fst $ maximumBy (compare `on` fst) tallyList
|
||||
minBin = fst $ minimumBy (compare `on` fst) tallyList
|
||||
|
||||
mapM_ (printStars tallies binWidth maxCount) [minBin..maxBin]
|
||||
|
||||
main = do
|
||||
runTest 1000
|
||||
runTest 2000000
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
runif01=: ?@$ 0: NB. random uniform number generator
|
||||
rnorm01=. (2 o. 2p1 * runif01) * [: %: _2 * ^.@runif01 NB. random normal number generator (Box-Muller)
|
||||
|
||||
mean=: +/ % # NB. mean
|
||||
stddev=: (<:@# %~ +/)&.:*:@(- mean) NB. standard deviation
|
||||
histogram=: <:@(#/.~)@(i.@#@[ , I.)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
DataSet=: rnorm01 1e5
|
||||
(mean , stddev) DataSet
|
||||
0.000781667 1.00154
|
||||
require 'plot'
|
||||
plot (5 %~ i: 25) ([;histogram) DataSet
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
import static java.lang.Math.*;
|
||||
import static java.util.Arrays.stream;
|
||||
import java.util.Locale;
|
||||
import java.util.function.DoubleSupplier;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import java.util.stream.DoubleStream;
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
public class Test implements DoubleSupplier {
|
||||
|
||||
private double mu, sigma;
|
||||
private double[] state = new double[2];
|
||||
private int index = state.length;
|
||||
|
||||
Test(double m, double s) {
|
||||
mu = m;
|
||||
sigma = s;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getAsDouble() {
|
||||
index++;
|
||||
if (index >= state.length) {
|
||||
double r = sqrt(-2 * log(random())) * sigma;
|
||||
double x = 2 * PI * random();
|
||||
state = new double[]{mu + r * sin(x), mu + r * cos(x)};
|
||||
index = 0;
|
||||
}
|
||||
return state[index];
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Locale.setDefault(Locale.US);
|
||||
double[] data = DoubleStream.generate(new Test(0.0, 0.5)).limit(100_000)
|
||||
.toArray();
|
||||
|
||||
double[] res = meanStdDev(data);
|
||||
System.out.printf("Mean: %8.6f, SD: %8.6f%n", res[0], res[1]);
|
||||
|
||||
showHistogram01(stream(data).map(a -> max(0.0, min(0.9999, a / 3 + 0.5)))
|
||||
.toArray());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
# Pretty print a number to facilitate alignment of the decimal point.
|
||||
# Input: a number without an exponent
|
||||
# Output: a string holding the reformatted number so that there are at least `left` characters
|
||||
# to the left of the decimal point, and exactly `right` characters to its right.
|
||||
# Spaces are used for padding on the left, and zeros for padding on the right.
|
||||
# No left-truncation occurs, so `left` can be specified as 0 to prevent left-padding.
|
||||
def pp(left; right):
|
||||
def lpad: tostring | (left - length) as $l | (" " * $l)[:$l] + .;
|
||||
def rpad:
|
||||
if (right > length) then . + ((right - length) * "0")
|
||||
else .[:right]
|
||||
end;
|
||||
tostring as $s
|
||||
| $s
|
||||
| index(".") as $ix
|
||||
| ((if $ix then $s[0:$ix] else $s end) | lpad) + "." +
|
||||
(if $ix then $s[$ix+1:] | .[:right] else "" end | rpad) ;
|
||||
|
||||
def sigma( stream ): reduce stream as $x (0; . + $x);
|
||||
|
||||
# Input: {n, sum, ss}
|
||||
# Output: augmented object with {mean, variance}
|
||||
def sample_mean_and_variance:
|
||||
.mean = (.sum/.n)
|
||||
| .variance = ((.ss / .n) - .mean*.mean);
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
# Task parameters
|
||||
def parameters: {
|
||||
N: 100000,
|
||||
NUM_BINS: 12,
|
||||
HIST_CHAR: "■",
|
||||
HIST_CHAR_ALT: "-",
|
||||
HIST_CHAR_SIZE: null, # null means compute dynamically
|
||||
binSize: 0.1,
|
||||
mu: 0.5,
|
||||
sigma: 0.25 }
|
||||
| .bins = [range(0; .NUM_BINS) | 0] ;
|
||||
|
||||
# input: an array of two iid rvs on [0,1]
|
||||
# output: [z0, z1] as per the Box-Muller method -- see
|
||||
# https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
|
||||
def normal(mu; sigma):
|
||||
def pi: (1|atan) * 4;
|
||||
. as [$u1, $u2]
|
||||
| pi as $pi
|
||||
| (sigma * ((-2 * ($u1|log))|sqrt)) as $mag
|
||||
| [ $mag * ((2 * $pi * $u2)|cos) + mu,
|
||||
$mag * ((2 * $pi * $u2)|sin) + mu ] ;
|
||||
|
||||
# Generate a random sample as specified by ., the task object (see `parameters`).
|
||||
# Output: updated task object with sample statistics and .bins for creating a histogram.
|
||||
# Each call to `input` should yield a string of random decimal digits
|
||||
# such that the ensemble of ("0." + input | tonumber) can be considered to be iid rv on [0,1].
|
||||
def generate:
|
||||
# uniformly distributed random variable on [0,1]:
|
||||
def udrv: "0." + input | tonumber;
|
||||
# Maybe compute the bucket size:
|
||||
(.HIST_CHAR_SIZE = (.HIST_CHAR_SIZE // (.N / (.NUM_BINS * 20) | ceil))) as $p
|
||||
| reduce range(0; $p.N/2) as $i ($p;
|
||||
([udrv, udrv] | normal($p.mu; $p.sigma)) as $rns
|
||||
| reduce (0,1) as $j (.;
|
||||
$rns[$j] as $rn
|
||||
| .n += 1
|
||||
| .sum += $rn
|
||||
| .ss += ($rn*$rn)
|
||||
| (if $rn < 0 then 0
|
||||
elif $rn >= 1 then ($p.NUM_BINS - 1)
|
||||
else ($rn/.binSize)|floor + 1
|
||||
end ) as $bn
|
||||
| .bins[$bn] += 1
|
||||
# to retain the observations: .samples[$i*2 + $j] = $rn
|
||||
)) ;
|
||||
|
||||
# Input: an object with
|
||||
# {NUM_BINS, HIST_CHAR_SIZE, HIST_CHAR, HIST_CHAR_ALT, binSize, bins}
|
||||
# Output: a stream of strings
|
||||
def histogram:
|
||||
def tidy: pp(2;1);
|
||||
range(0; .NUM_BINS) as $i
|
||||
| ((.bins[$i] / .HIST_CHAR_SIZE)|floor) as $bs
|
||||
| (if $i == 0 or $i == .NUM_BINS -1
|
||||
then .HIST_CHAR_ALT else .HIST_CHAR end) as $char
|
||||
| (if $bs == 0 then "" else $char * $bs end) as $hist
|
||||
| if $i == 0
|
||||
then " -∞ ..< 0.0 \($hist)" # .bins[0]
|
||||
elif ($i < .NUM_BINS - 1)
|
||||
then "\(.binSize * ($i-1) | tidy) ..<\(.binSize * $i|tidy) \($hist)" # .bins[$i]]
|
||||
else " 1.0 .. +∞ \($hist)" # .bins[.NUM_BINS - 1]
|
||||
end;
|
||||
|
||||
def task:
|
||||
parameters
|
||||
| generate
|
||||
| sample_mean_and_variance
|
||||
| (if .HIST_CHAR_SIZE == 1 then "" else "s" end) as $plural
|
||||
| "Summary statistics for \(.N) observations from N(\(.mu), \(.sigma)):",
|
||||
" mean: \(.mean | pp(2;4))",
|
||||
" variance: \(.variance | pp(2;4))",
|
||||
" unadjusted stddev: \(.variance | sqrt | pp(2;4))",
|
||||
" Range Number of observations (each \(.HIST_CHAR) represents \(.HIST_CHAR_SIZE) observation\($plural))",
|
||||
histogram ;
|
||||
|
||||
task
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
using Printf, Distributions, Gadfly
|
||||
|
||||
data = rand(Normal(0, 1), 1000)
|
||||
@printf("N = %i\n", length(data))
|
||||
@printf("μ = %2.2f\tσ = %2.2f\n", mean(data), std(data))
|
||||
@printf("range = (%2.2f, %2.2f\n)", minimum(data), maximum(data))
|
||||
h = plot(x=data, Geom.histogram)
|
||||
draw(PNG("norm_hist.png", 10cm, 10cm), h)
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
// version 1.1.2
|
||||
|
||||
val rand = java.util.Random()
|
||||
|
||||
fun normalStats(sampleSize: Int) {
|
||||
if (sampleSize < 1) return
|
||||
val r = DoubleArray(sampleSize)
|
||||
val h = IntArray(12) // all zero by default
|
||||
/*
|
||||
Generate 'sampleSize' normally distributed random numbers with mean 0.5 and SD 0.25
|
||||
and calculate in which box they will fall when drawing the histogram
|
||||
*/
|
||||
for (i in 0 until sampleSize) {
|
||||
r[i] = 0.5 + rand.nextGaussian() / 4.0
|
||||
when {
|
||||
r[i] < 0.0 -> h[0]++
|
||||
r[i] >= 1.0 -> h[11]++
|
||||
else -> h[1 + (r[i] * 10).toInt()]++
|
||||
}
|
||||
}
|
||||
|
||||
// adjust one of the h[] values if necessary to ensure they sum to sampleSize
|
||||
val adj = sampleSize - h.sum()
|
||||
if (adj != 0) {
|
||||
for (i in 0..11) {
|
||||
h[i] += adj
|
||||
if (h[i] >= 0) break
|
||||
h[i] -= adj
|
||||
}
|
||||
}
|
||||
|
||||
val mean = r.average()
|
||||
val sd = Math.sqrt(r.map { (it - mean) * (it - mean) }.average())
|
||||
|
||||
// Draw a histogram of the data with interval 0.1
|
||||
var numStars: Int
|
||||
// If sample size > 300 then normalize histogram to 300
|
||||
val scale = if (sampleSize <= 300) 1.0 else 300.0 / sampleSize
|
||||
println("Sample size $sampleSize\n")
|
||||
println(" Mean ${"%1.6f".format(mean)} SD ${"%1.6f".format(sd)}\n")
|
||||
for (i in 0..11) {
|
||||
when (i) {
|
||||
0 -> print("< 0.00 : ")
|
||||
11 -> print(">=1.00 : ")
|
||||
else -> print(" %1.2f : ".format(i / 10.0))
|
||||
}
|
||||
print("%5d ".format(h[i]))
|
||||
numStars = (h[i] * scale + 0.5).toInt()
|
||||
println("*".repeat(numStars))
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val sampleSizes = intArrayOf(100, 1_000, 10_000, 100_000)
|
||||
for (sampleSize in sampleSizes) normalStats(sampleSize)
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
define stat1(a) => {
|
||||
if(#a->size) => {
|
||||
local(mean = (with n in #a sum #n) / #a->size)
|
||||
local(sdev = math_pow(((with n in #a sum Math_Pow((#n - #mean),2)) / #a->size),0.5))
|
||||
return (:#sdev, #mean)
|
||||
else
|
||||
return (:0,0)
|
||||
}
|
||||
}
|
||||
define stat2(a) => {
|
||||
if(#a->size) => {
|
||||
local(sx = 0, sxx = 0)
|
||||
with x in #a do => {
|
||||
#sx += #x
|
||||
#sxx += #x*#x
|
||||
}
|
||||
local(sdev = math_pow((#a->size * #sxx - #sx * #sx),0.5) / #a->size)
|
||||
return (:#sdev, #sx / #a->size)
|
||||
else
|
||||
return (:0,0)
|
||||
}
|
||||
}
|
||||
define histogram(a) => {
|
||||
local(
|
||||
out = '\r',
|
||||
h = array(0,0,0,0,0,0,0,0,0,0,0),
|
||||
maxwidth = 50,
|
||||
sc = 0
|
||||
)
|
||||
with n in #a do => {
|
||||
if((#n * 10) <= 0) => {
|
||||
#h->get(1) += 1
|
||||
else((#n * 10) >= 10)
|
||||
#h->get(#h->size) += 1
|
||||
else
|
||||
#h->get(integer(decimal(#n)*10)+1) += 1
|
||||
}
|
||||
|
||||
}
|
||||
local(mx = decimal(with n in #h max #n))
|
||||
with i in #h do => {
|
||||
#out->append((#sc/10.0)->asString(-precision=1)+': '+('+' * integer(#i / #mx * #maxwidth))+'\r')
|
||||
#sc++
|
||||
}
|
||||
return #out
|
||||
}
|
||||
define normalDist(mean,sdev) => {
|
||||
// Uses Box-Muller transform
|
||||
return ((-2 * decimal_random->log)->sqrt * (2 * pi * decimal_random)->cos) * #sdev + #mean
|
||||
}
|
||||
|
||||
with scale in array(100,1000,10000) do => {^
|
||||
local(n = array)
|
||||
loop(#scale) => { #n->insert(normalDist(0.5, 0.2)) }
|
||||
local(sdev1,mean1) = stat1(#n)
|
||||
local(sdev2,mean2) = stat2(#n)
|
||||
#scale' numbers:\r'
|
||||
'Naive method: sd: '+#sdev1+', mean: '+#mean1+'\r'
|
||||
'Second method: sd: '+#sdev2+', mean: '+#mean2+'\r'
|
||||
histogram(#n)
|
||||
'\r\r'
|
||||
^}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
call sample 100000
|
||||
|
||||
end
|
||||
|
||||
sub sample n
|
||||
dim dat( n)
|
||||
for i =1 to n
|
||||
dat( i) =normalDist( 1, 0.2)
|
||||
next i
|
||||
|
||||
'// show mean, standard deviation. Find max, min.
|
||||
mx =-1000
|
||||
mn = 1000
|
||||
sum =0
|
||||
sSq =0
|
||||
for i =1 to n
|
||||
d =dat( i)
|
||||
mx =max( mx, d)
|
||||
mn =min( mn, d)
|
||||
sum =sum +d
|
||||
sSq =sSq +d^2
|
||||
next i
|
||||
print n; " data terms used."
|
||||
|
||||
mean =sum / n
|
||||
print "Largest term was "; mx; " & smallest was "; mn
|
||||
range =mx -mn
|
||||
print "Mean ="; mean
|
||||
|
||||
print "Stddev ="; ( sSq /n -mean^2)^0.5
|
||||
|
||||
'// show histogram
|
||||
nBins =50
|
||||
dim bins( nBins)
|
||||
for i =1 to n
|
||||
z =int( ( dat( i) -mn) /range *nBins)
|
||||
bins( z) =bins( z) +1
|
||||
next i
|
||||
for b =0 to nBins -1
|
||||
for j =1 to int( nBins *bins( b)) /n *30)
|
||||
print "#";
|
||||
next j
|
||||
print
|
||||
next b
|
||||
print
|
||||
end sub
|
||||
|
||||
function normalDist( m, s) ' Box Muller method
|
||||
u =rnd( 1)
|
||||
v =rnd( 1)
|
||||
normalDist =( -2 *log( u))^0.5 *cos( 2 *3.14159265 *v)
|
||||
end function
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
function gaussian (mean, variance)
|
||||
return math.sqrt(-2 * variance * math.log(math.random())) *
|
||||
math.cos(2 * math.pi * math.random()) + mean
|
||||
end
|
||||
|
||||
function mean (t)
|
||||
local sum = 0
|
||||
for k, v in pairs(t) do
|
||||
sum = sum + v
|
||||
end
|
||||
return sum / #t
|
||||
end
|
||||
|
||||
function std (t)
|
||||
local squares, avg = 0, mean(t)
|
||||
for k, v in pairs(t) do
|
||||
squares = squares + ((avg - v) ^ 2)
|
||||
end
|
||||
local variance = squares / #t
|
||||
return math.sqrt(variance)
|
||||
end
|
||||
|
||||
function showHistogram (t)
|
||||
local lo = math.ceil(math.min(unpack(t)))
|
||||
local hi = math.floor(math.max(unpack(t)))
|
||||
local hist, barScale = {}, 200
|
||||
for i = lo, hi do
|
||||
hist[i] = 0
|
||||
for k, v in pairs(t) do
|
||||
if math.ceil(v - 0.5) == i then
|
||||
hist[i] = hist[i] + 1
|
||||
end
|
||||
end
|
||||
io.write(i .. "\t" .. string.rep('=', hist[i] / #t * barScale))
|
||||
print(" " .. hist[i])
|
||||
end
|
||||
end
|
||||
|
||||
math.randomseed(os.time())
|
||||
local t, average, variance = {}, 50, 10
|
||||
for i = 1, 1000 do
|
||||
table.insert(t, gaussian(average, variance))
|
||||
end
|
||||
print("Mean:", mean(t) .. ", expected " .. average)
|
||||
print("StdDev:", std(t) .. ", expected " .. math.sqrt(variance) .. "\n")
|
||||
showHistogram(t)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
N = 100000;
|
||||
x = randn(N,1);
|
||||
mean(x)
|
||||
std(x)
|
||||
[nn,xx] = hist(x,100);
|
||||
bar(xx,nn);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
with(Statistics):
|
||||
n := 100000:
|
||||
X := Sample( Normal(0,1), n );
|
||||
Mean( X );
|
||||
StandardDeviation( X );
|
||||
Histogram( X );
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
x:= RandomReal[1]
|
||||
SampleNormal[n_] := (Print[#//Length, " numbers, Mean : ", #//Mean, ", StandardDeviation : ", #//StandardDeviation];
|
||||
Histogram[#, BarOrigin -> Left,Axes -> False])& [(Table[(-2*Log[x])^0.5*Cos[2*Pi*x], {n} ]]
|
||||
Invocation:
|
||||
SampleNormal[ 10000 ]
|
||||
->10000 numbers, Mean : -0.0122308, StandardDeviation : 1.00646
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
import math, random, sequtils, stats, strformat, strutils
|
||||
|
||||
proc drawHistogram(ns: seq[float]) =
|
||||
|
||||
# Distribute values in bins.
|
||||
const NBins = 50
|
||||
var minval = min(ns)
|
||||
var maxval = max(ns)
|
||||
var h = newSeq[int](NBins + 1)
|
||||
for n in ns:
|
||||
let pos = ((n - minval) * NBins / (maxval - minval)).toInt
|
||||
inc h[pos]
|
||||
|
||||
# Eliminate extremes values.
|
||||
const MaxWidth = 50
|
||||
let mx = max(h)
|
||||
var first = 0
|
||||
while (h[first] / mx * MaxWidth).toInt == 0: inc first
|
||||
var last = h.high
|
||||
while (h[last] / mx * MaxWidth).toInt == 0: dec last
|
||||
|
||||
# Draw the histogram.
|
||||
echo ""
|
||||
for n in first..last:
|
||||
echo repeat('+', (h[n] / mx * MaxWidth).toInt)
|
||||
echo ""
|
||||
|
||||
|
||||
const N = 100_000
|
||||
|
||||
randomize()
|
||||
|
||||
let u1, u2 = newSeqWith(N, rand(1.0))
|
||||
|
||||
var z = newSeq[float](N)
|
||||
for i in 0..<N:
|
||||
z[i] = sqrt(-2 * ln(u1[i])) * cos(2 * PI * u2[i])
|
||||
|
||||
echo &"μ = {z.mean:.12f} σ = {z.standardDeviation:.12f}"
|
||||
z.drawHistogram()
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
rnormal()={
|
||||
my(u1=random(1.),u2=random(1.);
|
||||
sqrt(-2*log(u1))*cos(2*Pi*u1)
|
||||
\\ Could easily be extended with a second normal at very little cost.
|
||||
};
|
||||
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,rnormal()),m=mean(v),s=stdev(v,m),h,sz=ceil(n/300));
|
||||
h=histogram(v,,vecmin(v)-.1,vecmax(v)+.1);
|
||||
for(i=1,#h,for(j=1,h[i]\sz,print1("#"));print());
|
||||
};
|
||||
show(10^4)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
rreal()={
|
||||
my(pr=32*ceil(default(realprecision)*log(10)/log(4294967296))); \\ Current precision
|
||||
random(2^pr)*1.>>pr
|
||||
};
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
GEN
|
||||
rnormal(long prec)
|
||||
{
|
||||
pari_sp ltop = avma;
|
||||
GEN u1, u2, left, right, ret;
|
||||
u1 = randomr(prec);
|
||||
u2 = randomr(prec);
|
||||
left = sqrtr_abs(shiftr(mplog(u1), 1));
|
||||
right = mpcos(mulrr(shiftr(mppi(prec), 1), u2));
|
||||
|
||||
ret = mulrr(left, right);
|
||||
ret = gerepileupto(ltop, ret);
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
Program Example40;
|
||||
{$IFDEF FPC}
|
||||
{$MOde objFPC}
|
||||
{$ENDIF}
|
||||
{ Program to demonstrate the randg function. }
|
||||
Uses Math;
|
||||
|
||||
type
|
||||
tTestData = extended;//because of math.randg
|
||||
ttstfunc = function (mean, sd: tTestData): tTestData;
|
||||
tExArray = Array of tTestData;
|
||||
tSolution = record
|
||||
SolExArr : tExArray;
|
||||
SollowVal,
|
||||
SolHighVal,
|
||||
SolMean,
|
||||
SolStdDiv : tTestData;
|
||||
SolSmpCnt : LongInt;
|
||||
end;
|
||||
|
||||
function getSol(genFunc:ttstfunc;Mean,StdDiv: tTestData;smpCnt: LongInt): tSolution;
|
||||
var
|
||||
GenValue,
|
||||
sumValue,
|
||||
sumsqrVal : extended;
|
||||
Begin
|
||||
with result do
|
||||
Begin
|
||||
SolSmpCnt := smpCnt;
|
||||
SolMean := 0;
|
||||
SolStdDiv := 0;
|
||||
SolLowVal := Mean+50* StdDiv;
|
||||
SolHighVal := Mean-50* StdDiv;
|
||||
setlength(SolExArr,smpCnt);
|
||||
if smpCnt <= 0 then
|
||||
EXIT;
|
||||
sumValue := 0;
|
||||
sumsqrVal := 0;
|
||||
repeat
|
||||
GenValue := genFunc(Mean,StdDiv);
|
||||
sumValue := sumvalue+GenValue;
|
||||
sumsqrVal := sumsqrVal+sqr(GenValue);
|
||||
IF GenValue < SollowVal then
|
||||
SollowVal:= GenValue
|
||||
else
|
||||
IF GenValue > SolHighVal then
|
||||
SolHighVal := GenValue;
|
||||
dec(smpCnt);
|
||||
SolExArr[smpCnt] := GenValue;
|
||||
until smpCnt<= 0;
|
||||
SolMean := sumValue/SolSmpCnt;
|
||||
SolStdDiv := sqrt(sumsqrVal/SolSmpCnt-sqr(SolMean));
|
||||
end;
|
||||
end;
|
||||
|
||||
//http://wiki.freepascal.org/Generating_Random_Numbers#Normal_.28Gaussian.29_Distribution
|
||||
function rnorm (mean, sd: tTestData): tTestData;
|
||||
{Calculates Gaussian random numbers according to the Box-Müller approach}
|
||||
var
|
||||
u1, u2: extended;
|
||||
begin
|
||||
u1 := random;
|
||||
u2 := random;
|
||||
rnorm := mean * abs(1 + sqrt(-2 * (ln(u1))) * cos(2 * pi * u2) * sd);
|
||||
end;
|
||||
|
||||
procedure Histo(const sol:TSolution;Colcnt,ColLen :LongInt);
|
||||
var
|
||||
CntHisto : array of integer;
|
||||
LoLmt,HiLmt,span : tTestData;
|
||||
i, j,cnt,maxCnt: LongInt;
|
||||
sCross : Ansistring;
|
||||
Begin
|
||||
setlength(CntHisto,Colcnt);
|
||||
with Sol do
|
||||
Begin
|
||||
span := solHighVal-solLowVal;
|
||||
LoLmt := solLowVal;
|
||||
writeln('Count: ',SolSmpCnt:10,' Mean ',SolMean:10:6,' StdDiv ',SolStdDIv:10:6);
|
||||
writeln('span : ',span:10:5,' Low ',solLowVal:10:6,' high ',solHighVal:10:6);
|
||||
|
||||
end;
|
||||
maxCnt := 0;
|
||||
For j := 0 to Colcnt-1 do
|
||||
Begin
|
||||
HiLmt:= LoLmt+span/Colcnt;
|
||||
cnt := 0;
|
||||
with sol do
|
||||
For i := 0 to High(SolExArr) do
|
||||
IF (HiLmt > SolExArr[i]) AND (SolExArr[i]>= LoLmt) then
|
||||
inc(cnt);
|
||||
CntHisto[j] := cnt;
|
||||
IF maxCnt < cnt then
|
||||
maxCnt := cnt;
|
||||
LoLmt:= HiLmt;
|
||||
end;
|
||||
inc(CntHisto[Colcnt]); // for HiLmt itself
|
||||
writeln;
|
||||
LoLmt := sol.solLowVal;
|
||||
For i := 0 to Colcnt-1 do
|
||||
Begin
|
||||
Writeln(LoLmt:8:4,': ');
|
||||
cnt:= Round(CntHisto[i]*ColLen/maxCnt);
|
||||
setlength(sCross,cnt+3);
|
||||
fillChar(sCross[1],3,' ');
|
||||
fillChar(sCross[4],cnt,'#');
|
||||
writeln(CntHisto[i]:10,sCross);
|
||||
LoLmt := LoLmt+span/Colcnt;
|
||||
end;
|
||||
Writeln(sol.solHighVal:8:4,': ');
|
||||
end;
|
||||
|
||||
const
|
||||
cHistCnt = 11;
|
||||
cColLen = 65;
|
||||
|
||||
cStdDiv = 0.25;
|
||||
cMean = 20*cStdDiv;
|
||||
var
|
||||
mySol : tSolution;
|
||||
begin
|
||||
Randomize;
|
||||
// test of randg of unit math
|
||||
Writeln('function randg');
|
||||
mySol := getSol(@randg,cMean,cMean*cStdDiv,100000);
|
||||
Histo(mySol,cHistCnt,cColLen);
|
||||
writeln;
|
||||
// test of rnorm from wiki
|
||||
Writeln('function rnorm');
|
||||
mySol := getSol(@rnorm,cMean,cStdDiv,1000000);
|
||||
Histo(mySol,cHistCnt,cColLen);
|
||||
end.
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
use constant pi => 3.14159265;
|
||||
use List::Util qw(sum reduce min max);
|
||||
|
||||
sub normdist {
|
||||
my($m, $sigma) = @_;
|
||||
my $r = sqrt -2 * log rand;
|
||||
my $theta = 2 * pi * rand;
|
||||
$r * cos($theta) * $sigma + $m;
|
||||
}
|
||||
|
||||
$size = 100000; $mean = 50; $stddev = 4;
|
||||
|
||||
push @dataset, normdist($mean,$stddev) for 1..$size;
|
||||
|
||||
my $m = sum(@dataset) / $size;
|
||||
print "m = $m\n";
|
||||
|
||||
my $sigma = sqrt( (reduce { $a + $b **2 } 0,@dataset) / $size - $m**2 );
|
||||
print "sigma = $sigma\n";
|
||||
|
||||
$hash{int $_}++ for @dataset;
|
||||
my $scale = 180 * $stddev / $size;
|
||||
my @subbar = < ⎸ ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ >;
|
||||
for $i (min(@dataset)..max(@dataset)) {
|
||||
my $x = ($hash{$i} // 0) * $scale;
|
||||
my $full = int $x;
|
||||
my $part = 8 * ($x - $full);
|
||||
my $t1 = '█' x $full;
|
||||
my $t2 = $subbar[$part];
|
||||
print "$i\t$t1$t2\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">sample</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: #000080;font-style:italic;">-- show mean, standard deviation. Find max, min.</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">dat</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;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">dat</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">()))*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">())</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d data terms used.\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
|
||||
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">mean</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dat</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dat</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dat</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">range</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mn</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Largest term was %g & smallest was %g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Mean = %g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mean</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Stddev = %g\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dat</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dat</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mean</span><span style="color: #0000FF;">*</span><span style="color: #000000;">mean</span><span style="color: #0000FF;">))</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- show histogram</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">nBins</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">50</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">bins</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;">nBins</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">bdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">dat</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">range</span><span style="color: #0000FF;">*</span><span style="color: #000000;">nBins</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">bins</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nBins</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nBins</span><span style="color: #0000FF;">*</span><span style="color: #000000;">bins</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">30</span><span style="color: #0000FF;">))&</span><span style="color: #008000;">"\n"</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;">procedure</span>
|
||||
|
||||
<span style="color: #000000;">sample</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100000</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">gaussian</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">variance</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">variance</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">()))</span> <span style="color: #0000FF;">*</span>
|
||||
<span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">variance</span> <span style="color: #0000FF;">*</span> <span style="color: #004600;">PI</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">())</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">mean</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">std</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">squares</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">avg</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">squares</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">avg</span><span style="color: #0000FF;">-</span><span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">variance</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">squares</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">variance</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">showHistogram</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">200</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d %s %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'='</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</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;">procedure</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">t</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;">100000</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">avg</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">50</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">variance</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">gaussian</span><span style="color: #0000FF;">(</span><span style="color: #000000;">avg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">variance</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Mean: %g, expected %g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span><span style="color: #000000;">avg</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"StdDev: %g, expected %g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">std</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">variance</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #000000;">showHistogram</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
Procedure.f randomf(resolution = 2147483647)
|
||||
ProcedureReturn Random(resolution) / resolution
|
||||
EndProcedure
|
||||
|
||||
Procedure.f normalDist() ;Box Muller method
|
||||
ProcedureReturn Sqr(-2 * Log(randomf())) * Cos(2 * #PI * randomf())
|
||||
EndProcedure
|
||||
|
||||
Procedure sample(n, nBins = 50)
|
||||
Protected i, maxBinValue, binNumber
|
||||
Protected.f d, mean, sum, sumSq, mx, mn, range
|
||||
|
||||
Dim dat.f(n)
|
||||
For i = 1 To n
|
||||
dat(i) = normalDist()
|
||||
Next
|
||||
|
||||
;show mean, standard deviation, find max & min.
|
||||
mx = -1000
|
||||
mn = 1000
|
||||
sum = 0
|
||||
sumSq = 0
|
||||
For i = 1 To n
|
||||
d = dat(i)
|
||||
If d > mx: mx = d: EndIf
|
||||
If d < mn: mn = d: EndIf
|
||||
sum + d
|
||||
sumSq + d * d
|
||||
Next
|
||||
|
||||
PrintN(Str(n) + " data terms used.")
|
||||
PrintN("Largest term was " + StrF(mx) + " & smallest was " + StrF(mn))
|
||||
mean = sum / n
|
||||
PrintN("Mean = " + StrF(mean))
|
||||
PrintN("Stddev = " + StrF((sumSq / n) - Sqr(mean * mean)))
|
||||
|
||||
;show histogram
|
||||
range = mx - mn
|
||||
Dim bins(nBins)
|
||||
For i = 1 To n
|
||||
binNumber = Int(nBins * (dat(i) - mn) / range)
|
||||
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 = Round(bins(binNumber) * #normalizedMaxValue / maxBinValue, #PB_Round_Nearest)
|
||||
PrintN(ReplaceString(Space(tickMarks), " ", "#"))
|
||||
Next
|
||||
PrintN("")
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
sample(100000)
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
from __future__ import division
|
||||
import matplotlib.pyplot as plt
|
||||
import random
|
||||
|
||||
mean, stddev, size = 50, 4, 100000
|
||||
data = [random.gauss(mean, stddev) for c in range(size)]
|
||||
|
||||
mn = sum(data) / size
|
||||
sd = (sum(x*x for x in data) / size
|
||||
- (sum(data) / size) ** 2) ** 0.5
|
||||
|
||||
print("Sample mean = %g; Stddev = %g; max = %g; min = %g for %i values"
|
||||
% (mn, sd, max(data), min(data), size))
|
||||
|
||||
plt.hist(data,bins=50)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
n <- 100000
|
||||
u <- sqrt(-2*log(runif(n)))
|
||||
v <- 2*pi*runif(n)
|
||||
x <- u*cos(v)
|
||||
y <- v*sin(v)
|
||||
hist(x)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
n <- 100000
|
||||
x <- rnorm(n, mean=0, sd=1)
|
||||
mean(x)
|
||||
sd(x)
|
||||
hist(x)
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*REXX program generates 10,000 normally distributed numbers (Gaussian distribution).*/
|
||||
numeric digits 20 /*use twenty decimal digs for accuracy.*/
|
||||
parse arg n seed . /*obtain optional arguments from the CL*/
|
||||
if n=='' | n=="," then n= 10000 /*Not specified? Then use the default.*/
|
||||
if datatype(seed, 'W') then call random ,,seed /*seed is for repeatable RANDOM numbers*/
|
||||
call pi /*call subroutine to define pi constant*/
|
||||
do g=1 for n; #.g= sqrt( -2 * ln( rand() ) ) * cos( 2 * pi * rand() )
|
||||
end /*g*/ /* [↑] uniform random number ───► #.g */
|
||||
s= 0
|
||||
mn= #.1; mx= mn; noise= n * .0005 /*calculate the noise: 1/20th % of N.*/
|
||||
ss= 0
|
||||
do j=1 for n; _= #.j /*the sum, and the sum of squares. */
|
||||
s= s + _; ss= ss + _ * _ /*the sum, and the sum of squares. */
|
||||
mn= min(mn, _); mx= max(mx, _) /*find the minimum and the maximum. */
|
||||
end /*j*/
|
||||
!.= 0
|
||||
say 'number of data points = ' fmt(n )
|
||||
say ' minimum = ' fmt(mn )
|
||||
say ' maximum = ' fmt(mx )
|
||||
say ' arithmetic mean = ' fmt(s/n)
|
||||
say ' standard deviation = ' fmt(sqrt( ss/n - (s/n) **2) )
|
||||
?mn= !.1; ?mx= ?mn /*define minimum & maximum value so far*/
|
||||
parse value scrSize() with sd sw . /*obtain the (true) screen size of term*/ /*◄──not all REXXes have this BIF*/
|
||||
sdE= sd - 4 /*the effective (usable) screen depth. */
|
||||
swE= sw - 1 /* " " " " width.*/
|
||||
$= 1 / max(1, mx-mn) * sdE /*$ is used for scaling depth of histo*/
|
||||
do i=1 for n; ?= trunc((#.i-mn) *$) /*calculate the relative line. */
|
||||
!.?= !.? + 1 /*bump the counter. */
|
||||
?mn= min(?mn, !.?) /*find the minimum. */
|
||||
?mx= max(?mx, !.?) /* " " maximum. */
|
||||
end /*i*/
|
||||
f= swE/?mx /*limit graph to 1 full screen*/
|
||||
do h=0 for sdE; _= !.h /*obtain a data point. */
|
||||
if _>noise then say copies('─', trunc(_*f) ) /*display a bar of histogram. */
|
||||
end /*h*/ /*[↑] use a hyphen for histo.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
fmt: parse arg @; return left('', (@>=0) + 2 * datatype(@, 'W'))@ /*prepend a blank if #>=0, add 2 blanks if whole.*/
|
||||
e: e = 2.7182818284590452353602874713526624977572470936999595749669676277240766303535; return e
|
||||
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi
|
||||
r2r: return arg(1) // (pi() * 2) /*normalize the given angle (in radians) to ±2pi.*/
|
||||
rand: return random(1, 1e5) / 1e5 /*REXX generates uniform random postive integers.*/
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ln: procedure; parse arg x,f; call e; ig= x>1.5; is= 1 -2*(ig\==1); ii= 0; xx= x; do while ig & xx>1.5 | \ig & xx<.5
|
||||
_= e; do k=-1; iz= xx*_ **-is; if k>=0 & (ig & iz<1 | \ig & iz>.5) then leave; _= _*_; izz= iz; end; xx= izz
|
||||
ii= ii +is*2**k; end; x= x*e**-ii-1; z=0; _=-1; p=z; do k=1;_=-_*x;z=z+_/k;if z=p then leave;p=z;end; return z+ii
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
cos: procedure; parse arg x; x=r2r(x); a=abs(x); hpi= pi*.5; numeric fuzz min(6, digits()-3); if a=pi then return -1
|
||||
if a=hpi | a=hpi*3 then return 0; if a=pi/3 then return .5; if a=pi*2/3 then return -.5; z= 1; _= 1
|
||||
x= x*x; p= z; do k=2 by 2; _= -_ * x / (k*(k-1)); z= z + _; if z=p then leave; p= z; end; return z
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d= digits(); m.= 9; numeric digits; numeric form; h= d+6
|
||||
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
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#lang racket
|
||||
(require math (planet williams/science/histogram-with-graphics))
|
||||
|
||||
(define data (sample (normal-dist 50 4) 100000))
|
||||
|
||||
(displayln (~a "Mean:\t" (mean data)))
|
||||
(displayln (~a "Stddev:\t" (stddev data)))
|
||||
(displayln (~a "Max:\t" (apply max data)))
|
||||
(displayln (~a "Min:\t" (apply min data)))
|
||||
|
||||
(define h (make-histogram-with-ranges-uniform 40 30 70))
|
||||
(for ([x data]) (histogram-increment! h x))
|
||||
(histogram-plot h "Normal distribution μ=50 σ=4")
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
|
||||
(define random-normal
|
||||
(let ([unit (uniform-dist)]
|
||||
[next #f])
|
||||
(λ (μ σ)
|
||||
(if next
|
||||
(begin0
|
||||
(+ μ (* σ next))
|
||||
(set! next #f))
|
||||
(let loop ()
|
||||
(let* ([v1 (- (* 2.0 (sample unit)) 1.0)]
|
||||
[v2 (- (* 2.0 (sample unit)) 1.0)]
|
||||
[s (+ (sqr v1) (sqr v2))])
|
||||
(cond [(>= s 1) (loop)]
|
||||
[else (define scale (sqrt (/ (* -2.0 (log s)) s)))
|
||||
(set! next (* scale v2))
|
||||
(+ μ (* σ scale v1))])))))))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
sub normdist ($m, $σ) {
|
||||
my $r = sqrt -2 * log rand;
|
||||
my $Θ = τ * rand;
|
||||
$r * cos($Θ) * $σ + $m;
|
||||
}
|
||||
|
||||
sub MAIN ($size = 100000, $mean = 50, $stddev = 4) {
|
||||
my @dataset = normdist($mean,$stddev) xx $size;
|
||||
|
||||
my $m = [+](@dataset) / $size;
|
||||
say (:$m);
|
||||
|
||||
my $σ = sqrt [+](@dataset X** 2) / $size - $m**2;
|
||||
say (:$σ);
|
||||
|
||||
(my %hash){.round}++ for @dataset;
|
||||
my $scale = 180 * $stddev / $size;
|
||||
constant @subbar = < ⎸ ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ >;
|
||||
for %hash.keys».Int.minmax(+*) -> $i {
|
||||
my $x = (%hash{$i} // 0) * $scale;
|
||||
my $full = floor $x;
|
||||
my $part = 8 * ($x - $full);
|
||||
say $i, "\t", '█' x $full, @subbar[$part];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
# Class to implement a Normal distribution, generated from a Uniform distribution.
|
||||
# Uses the Marsaglia polar method.
|
||||
|
||||
class NormalFromUniform
|
||||
# Initialize an instance.
|
||||
def initialize()
|
||||
@next = nil
|
||||
end
|
||||
# Generate and return the next Normal distribution value.
|
||||
def rand()
|
||||
if @next
|
||||
retval, @next = @next, nil
|
||||
return retval
|
||||
else
|
||||
u = v = s = nil
|
||||
loop do
|
||||
u = Random.rand(-1.0..1.0)
|
||||
v = Random.rand(-1.0..1.0)
|
||||
s = u**2 + v**2
|
||||
break if (s > 0.0) && (s <= 1.0)
|
||||
end
|
||||
f = Math.sqrt(-2.0 * Math.log(s) / s)
|
||||
@next = v * f
|
||||
return u * f
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
require('enumerable/statistics')
|
||||
|
||||
def show_stats_and_histogram(data, bins)
|
||||
puts("size = #{data.length} mean = #{data.mean()} stddev = #{data.stdev()}")
|
||||
hist = data.histogram(bins)
|
||||
scale = 100.0 / hist.weights.max
|
||||
inx_beg = nil
|
||||
inx_end = nil
|
||||
hist.weights.length.times do |inx|
|
||||
histstars = (0.5 + (scale * hist.weights[inx])).to_i
|
||||
inx_beg = inx if !inx_beg && (histstars > 0)
|
||||
inx_end = inx if (histstars > 0)
|
||||
end
|
||||
(inx_beg..inx_end).each do |inx|
|
||||
bincenter = 0.5 * (hist.edges[inx] + hist.edges[inx + 1])
|
||||
histstars = (0.5 + (scale * hist.weights[inx])).to_i
|
||||
puts('%6.2f: %s' % [bincenter, '*' * histstars])
|
||||
end
|
||||
end
|
||||
|
||||
puts
|
||||
puts('Uniform random number generator:')
|
||||
show_stats_and_histogram(1000000.times.map { Random.rand(-1.0..1.0) }, 20)
|
||||
|
||||
puts
|
||||
puts('Normal random numbers using the Marsaglia polar method:')
|
||||
gen_normal = NormalFromUniform.new
|
||||
show_stats_and_histogram(100.times.map { gen_normal.rand }, 40)
|
||||
show_stats_and_histogram(10000.times.map { gen_normal.rand }, 60)
|
||||
show_stats_and_histogram(1000000.times.map { gen_normal.rand }, 120)
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
s = 100000
|
||||
h$ = "============================================================="
|
||||
h$ = h$ + h$
|
||||
dim ndis(s)
|
||||
' mean and standard deviation.
|
||||
mx = -9999
|
||||
mn = 9999
|
||||
sum = 0
|
||||
sumSqr = 0
|
||||
for i = 1 to s ' find minimum and maximum
|
||||
ms = rnd(1)
|
||||
ss = rnd(1)
|
||||
nd = (-2 * log(ms))^0.5 * cos(2 *3.14159265 * ss) ' normal distribution
|
||||
ndis(i) = nd
|
||||
mx = max(mx, nd)
|
||||
mn = min(mn, nd)
|
||||
sum = sum + nd
|
||||
sumSqr = sumSqr + nd ^ 2
|
||||
next i
|
||||
|
||||
mean = sum / s
|
||||
range = mx - mn
|
||||
|
||||
print "Samples :"; s
|
||||
print "Largest :"; mx
|
||||
print "Smallest :"; mn
|
||||
print "Range :"; range
|
||||
print "Mean :"; mean
|
||||
print "Stand Dev :"; (sumSqr /s -mean^2)^0.5
|
||||
|
||||
'Show chart of histogram
|
||||
nBins = 50
|
||||
dim bins(nBins)
|
||||
for i = 1 to s
|
||||
z = int((ndis(i) -mn) /range *nBins)
|
||||
bins(z) = bins(z) + 1
|
||||
mb = max(bins(z),mb)
|
||||
next i
|
||||
for b = 0 to nBins -1
|
||||
print using("##",b);" ";using("#####",bins(b));" ";left$(h$,(bins(b) / mb) * 90)
|
||||
next b
|
||||
END
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
//! Rust rosetta example for normal distribution
|
||||
use math::{histogram::Histogram, traits::ToIterator};
|
||||
use rand;
|
||||
use rand_distr::{Distribution, Normal};
|
||||
|
||||
/// Returns the mean of the provided samples
|
||||
///
|
||||
/// ## Arguments
|
||||
/// * data -- reference to float32 array
|
||||
fn mean(data: &[f32]) -> Option<f32> {
|
||||
let sum: f32 = data.iter().sum();
|
||||
Some(sum / data.len() as f32)
|
||||
}
|
||||
|
||||
/// Returns standard deviation of the provided samples
|
||||
///
|
||||
/// ## Arguments
|
||||
/// * data -- reference to float32 array
|
||||
fn standard_deviation(data: &[f32]) -> Option<f32> {
|
||||
let mean = mean(data).expect("invalid mean");
|
||||
let sum = data.iter().fold(0.0, |acc, &x| acc + (x - mean).powi(2));
|
||||
Some((sum / data.len() as f32).sqrt())
|
||||
}
|
||||
|
||||
/// Prints a histogram in the shell
|
||||
///
|
||||
/// ## Arguments
|
||||
/// * data -- reference to float32 array
|
||||
/// * maxwidth -- the maxwidth of the histogram in # of characters
|
||||
/// * bincount -- number of bins in the histogram
|
||||
/// * ch -- character used to plot the graph
|
||||
fn print_histogram(data: &[f32], maxwidth: usize, bincount: usize, ch: char) {
|
||||
let min_val = data.iter().cloned().fold(f32::NAN, f32::min);
|
||||
let max_val = data.iter().cloned().fold(f32::NAN, f32::max);
|
||||
let histogram = Histogram::new(Some(&data.to_vec()), bincount, min_val, max_val).unwrap();
|
||||
let max_bin_value = histogram.get_counters().iter().max().unwrap();
|
||||
println!();
|
||||
for x in histogram.to_iter() {
|
||||
let (bin_min, bin_max, freq) = x;
|
||||
let bar_width = (((freq as f64) / (*max_bin_value as f64)) * (maxwidth as f64)) as u32;
|
||||
let bar_as_string = (1..bar_width).fold(String::new(), |b, _| b + &ch.to_string());
|
||||
println!(
|
||||
"({:>6},{:>6}) |{} {:.2}%",
|
||||
format!("{:.2}", bin_min),
|
||||
format!("{:.2}", bin_max),
|
||||
bar_as_string,
|
||||
(freq as f64) * 100.0 / (data.len() as f64)
|
||||
);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
/// Runs the demo to generate normal distribution of three different sample sizes
|
||||
fn main() {
|
||||
let expected_mean: f32 = 0.0;
|
||||
let expected_std_deviation: f32 = 4.0;
|
||||
let normal = Normal::new(expected_mean, expected_std_deviation).unwrap();
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
for &number_of_samples in &[1000, 10_000, 1_000_000] {
|
||||
let data: Vec<f32> = normal
|
||||
.sample_iter(&mut rng)
|
||||
.take(number_of_samples)
|
||||
.collect();
|
||||
println!("Statistics for sample size {}:", number_of_samples);
|
||||
println!("\tMean: {:?}", mean(&data).expect("invalid mean"));
|
||||
println!(
|
||||
"\tStandard deviation: {:?}",
|
||||
standard_deviation(&data).expect("invalid standard deviation")
|
||||
);
|
||||
print_histogram(&data, 80, 40, '-');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
data test;
|
||||
n=100000;
|
||||
twopi=2*constant('pi');
|
||||
do i=1 to n;
|
||||
u=ranuni(0);
|
||||
v=ranuni(0);
|
||||
r=sqrt(-2*log(u));
|
||||
x=r*cos(twopi*v);
|
||||
y=r*sin(twopi*v);
|
||||
z=rannor(0);
|
||||
output;
|
||||
end;
|
||||
keep x y z;
|
||||
|
||||
proc means mean stddev;
|
||||
|
||||
proc univariate;
|
||||
histogram /normal;
|
||||
|
||||
run;
|
||||
|
||||
/*
|
||||
Variable Mean Std Dev
|
||||
----------------------------------------
|
||||
x -0.0052720 0.9988467
|
||||
y 0.000023995 1.0019996
|
||||
z 0.0012857 1.0056536
|
||||
*/
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
define τ = Num.tau
|
||||
|
||||
func normdist (m, σ) {
|
||||
var r = sqrt(-2 * 1.rand.log)
|
||||
var Θ = (τ * 1.rand)
|
||||
r * Θ.cos * σ + m
|
||||
}
|
||||
|
||||
var size = 100_000
|
||||
var mean = 50
|
||||
var stddev = 4
|
||||
|
||||
var dataset = size.of { normdist(mean, stddev) }
|
||||
var m = (dataset.sum / size)
|
||||
say ("m: #{m}")
|
||||
|
||||
var σ = sqrt(dataset »**» 2 -> sum / size - m**2)
|
||||
say ("s: #{σ}")
|
||||
|
||||
var hash = Hash()
|
||||
dataset.each { |n| hash{ n.round } := 0 ++ }
|
||||
|
||||
var scale = (180 * stddev / size)
|
||||
const subbar = < ⎸ ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ >
|
||||
|
||||
for i in (hash.keys.map{.to_i}.sort) {
|
||||
var x = (hash{i} * scale)
|
||||
var full = x.int
|
||||
var part = (8 * (x - full))
|
||||
say (i, "\t", '█' * full, subbar[part])
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
clear all
|
||||
set obs 100000
|
||||
gen u=runiform()
|
||||
gen v=runiform()
|
||||
gen r=sqrt(-2*log(u))
|
||||
gen x=r*cos(2*_pi*v)
|
||||
gen y=r*sin(2*_pi*v)
|
||||
gen z=rnormal()
|
||||
sum x y z
|
||||
|
||||
Variable | Obs Mean Std. Dev. Min Max
|
||||
-------------+---------------------------------------------------------
|
||||
x | 100,000 .0025861 1.002346 -4.508192 4.164336
|
||||
y | 100,000 .0017389 1.001586 -4.631144 4.460274
|
||||
z | 100,000 .005054 .9998861 -5.134265 4.449522
|
||||
hist x, normal
|
||||
hist y, normal
|
||||
hist z, normal
|
||||
qqplot x z, msize(tiny)
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package require Tcl 8.5
|
||||
# Uses the Box-Muller transform to compute a pair of normal random numbers
|
||||
proc tcl::mathfunc::nrand {mean stddev} {
|
||||
variable savednormalrandom
|
||||
if {[info exists savednormalrandom]} {
|
||||
return [expr {$savednormalrandom*$stddev + $mean}][unset savednormalrandom]
|
||||
}
|
||||
set r [expr {sqrt(-2*log(rand()))}]
|
||||
set theta [expr {2*3.1415927*rand()}]
|
||||
set savednormalrandom [expr {$r*sin($theta)}]
|
||||
expr {$r*cos($theta)*$stddev + $mean}
|
||||
}
|
||||
proc stats {size {slotfactor 10}} {
|
||||
set sum 0.0
|
||||
set sum2 0.0
|
||||
for {set i 0} {$i < $size} {incr i} {
|
||||
set r [expr { nrand(0.5, 0.2) }]
|
||||
|
||||
incr histo([expr {int(floor($r*$slotfactor))}])
|
||||
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 [lsort -integer [array names histo]] {
|
||||
puts [string repeat "*" [expr {$histo($i)*350/int($size)}]]
|
||||
}
|
||||
}
|
||||
|
||||
stats 100
|
||||
puts ""
|
||||
stats 1000
|
||||
puts ""
|
||||
stats 10000
|
||||
puts ""
|
||||
stats 100000 20
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
Public Sub standard_normal()
|
||||
Dim s() As Variant, bins(71) As Single
|
||||
ReDim s(20000)
|
||||
For i = 1 To 20000
|
||||
s(i) = WorksheetFunction.Norm_S_Inv(Rnd())
|
||||
Next i
|
||||
For i = -35 To 35
|
||||
bins(i + 36) = i / 10
|
||||
Next i
|
||||
Debug.Print "sample size"; UBound(s), "mean"; mean(s), "standard deviation"; standard_deviation(s)
|
||||
t = WorksheetFunction.Frequency(s, bins)
|
||||
For i = -35 To 35
|
||||
Debug.Print Format((i - 1) / 10, "0.00");
|
||||
Debug.Print "-"; Format(i / 10, "0.00"),
|
||||
Debug.Print String$(t(i + 36, 1) / 10, "X");
|
||||
Debug.Print
|
||||
Next i
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
import "random" for Random
|
||||
import "/fmt" for Fmt
|
||||
import "/math" for Nums
|
||||
|
||||
var rgen = Random.new()
|
||||
|
||||
// Box-Muller method from Wikipedia
|
||||
var normal = Fn.new { |mu, sigma|
|
||||
var u1 = rgen.float()
|
||||
var u2 = rgen.float()
|
||||
var mag = sigma * (-2 * u1.log).sqrt
|
||||
var z0 = mag * (2 * Num.pi * u2).cos + mu
|
||||
var z1 = mag * (2 * Num.pi * u2).sin + mu
|
||||
return [z0, z1]
|
||||
}
|
||||
|
||||
var N = 100000
|
||||
var NUM_BINS = 12
|
||||
var HIST_CHAR = "■"
|
||||
var HIST_CHAR_SIZE = 250
|
||||
var bins = List.filled(NUM_BINS, 0)
|
||||
var binSize = 0.1
|
||||
var samples = List.filled(N, 0)
|
||||
var mu = 0.5
|
||||
var sigma = 0.25
|
||||
for (i in 0...N/2) {
|
||||
var rns = normal.call(mu, sigma)
|
||||
for (j in 0..1) {
|
||||
var rn = rns[j]
|
||||
var bn
|
||||
if (rn < 0) {
|
||||
bn = 0
|
||||
} else if (rn >= 1) {
|
||||
bn = 11
|
||||
} else {
|
||||
bn = (rn/binSize).floor + 1
|
||||
}
|
||||
bins[bn] = bins[bn] + 1
|
||||
samples[i*2 + j] = rn
|
||||
}
|
||||
}
|
||||
|
||||
Fmt.print("Normal distribution with mean $0.2f and S/D $0.2f for $,d samples:\n", mu, sigma, N)
|
||||
System.print(" Range Number of samples within that range")
|
||||
for (i in 0...NUM_BINS) {
|
||||
var hist = HIST_CHAR * (bins[i] / HIST_CHAR_SIZE).round
|
||||
if (i == 0) {
|
||||
Fmt.print(" -∞ ..< 0.00 $s $,d", hist, bins[0])
|
||||
} else if (i < NUM_BINS - 1) {
|
||||
Fmt.print("$4.2f ..< $4.2f $s $,d", binSize * (i-1), binSize * i, hist, bins[i])
|
||||
} else {
|
||||
Fmt.print("1.00 ... +∞ $s $,d", hist, bins[NUM_BINS - 1])
|
||||
}
|
||||
}
|
||||
Fmt.print("\nActual mean for these samples : $0.5f", Nums.mean(samples))
|
||||
Fmt.print("Actual S/D for these samples : $0.5f", Nums.stdDev(samples))
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fcn norm2{ // Box-Muller
|
||||
const PI2=(0.0).pi*2;;
|
||||
rnd:=(0.0).random.fp(1); // random number in [0,1), using partial application
|
||||
r,a:=(-2.0*rnd().log()).sqrt(), PI2*rnd();
|
||||
return(r*a.cos(), r*a.sin()); // z0,z1
|
||||
}
|
||||
const N=100000, BINS=12, SIG=3, SCALE=500;
|
||||
var sum=0.0,sumSq=0.0, h=BINS.pump(List(),0); // (0,0,0,...)
|
||||
fcn accum(v){
|
||||
sum+=v;
|
||||
sumSq+=v*v;
|
||||
b:=(v + SIG)*BINS/SIG/2;
|
||||
if(0<=b<BINS) h[b]+=1;
|
||||
};
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
foreach i in (N/2){ v1,v2:=norm2(); accum(v1); accum(v2); }
|
||||
println("Samples: %,d".fmt(N));
|
||||
println("Mean: ", m:=sum/N);
|
||||
println("Stddev: ", (sumSq/N - m*m).sqrt());
|
||||
foreach p in (h){ println("*"*(p/SCALE)) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue