How to resolve the algorithm Statistics/Basic step by step in the Action! programming language
How to resolve the algorithm Statistics/Basic step by step in the Action! programming language
Table of Contents
Problem Statement
Statistics is all about large groups of numbers.
When talking about a set of sampled data, most frequently used is their mean value and standard deviation (stddev).
If you have set of data xi where i =1,2,...n
When examining a large quantity of data, one often uses a 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.)
For a finite population with equal probabilities at all points, one can derive:
Or, more verbosely.
See also: Statistics/Normal distribution
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Statistics/Basic step by step in the Action! programming language
Source code in the action! programming language
INCLUDE "H6:REALMATH.ACT"
DEFINE SIZE="10000"
DEFINE HIST_SIZE="10"
BYTE ARRAY data(SIZE)
CARD ARRAY hist(HIST_SIZE)
PROC Generate()
INT i
FOR i=0 TO SIZE-1
DO
data(i)=Rand(0)
OD
RETURN
PROC CalcMean(INT count REAL POINTER mean)
REAL tmp1,tmp2,r255
INT i
IntToReal(0,mean)
IntToReal(255,r255)
FOR i=0 TO count-1
DO
IntToReal(data(i),tmp1)
RealDiv(tmp1,r255,tmp2)
RealAdd(mean,tmp2,tmp1)
RealAssign(tmp1,mean)
OD
IntToReal(count,tmp1)
RealDiv(mean,tmp1,tmp2)
RealAssign(tmp2,mean)
RETURN
PROC CalcStdDev(INT count REAL POINTER mean,sdev)
REAL tmp1,tmp2,r255
INT i
IntToReal(0,sdev)
IntToReal(255,r255)
FOR i=0 TO count-1
DO
IntToReal(data(i),tmp1)
RealDiv(tmp1,r255,tmp2)
RealSub(tmp2,mean,tmp1)
RealMult(tmp1,tmp1,tmp2)
RealAdd(sdev,tmp2,tmp1)
RealAssign(tmp1,sdev)
OD
IntToReal(count,tmp1)
RealDiv(sdev,tmp1,tmp2)
Sqrt(tmp2,sdev)
RETURN
PROC ClearHistogram()
BYTE i
FOR i=0 TO HIST_SIZE-1
DO
hist(i)=0
OD
RETURN
PROC CalcHistogram(INT count)
INT i,index
ClearHistogram()
FOR i=0 TO count-1
DO
index=data(i)*10/256
hist(index)==+1
OD
RETURN
PROC PrintHistogram()
BYTE i,j,n
INT max
REAL tmp1,tmp2,rmax,rlen
max=0
FOR i=0 TO HIST_SIZE-1
DO
IF hist(i)>max THEN
max=hist(i)
FI
OD
IntToReal(max,rmax)
IntToReal(25,rlen)
FOR i=0 TO HIST_SIZE-1
DO
PrintF("0.%Bx: ",i)
IntToReal(hist(i),tmp1)
RealMult(tmp1,rlen,tmp2)
RealDiv(tmp2,rmax,tmp1)
n=RealToInt(tmp1)
FOR j=0 TO n
DO
Put('*)
OD
PrintF(" %U",hist(i))
IF i
PutE()
FI
OD
RETURN
PROC Test(INT count)
REAL mean,sdev
PrintI(count)
CalcMean(count,mean)
Print(": m=") PrintR(mean)
CalcStdDev(count,mean,sdev)
Print(" sd=") PrintRE(sdev)
CalcHistogram(count)
PrintHistogram()
RETURN
PROC Main()
Put(125) PutE() ;clear screen
MathInit()
Generate()
Test(100)
PutE() PutE()
Test(10000)
RETURN
You may also check:How to resolve the algorithm Conjugate transpose step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the Factor programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the Toka programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Power set step by step in the J programming language