Jump to content

Statistics/Basic: Difference between revisions

J: closer reading of task requirements
(J: closer reading of task requirements)
Line 197:
=={{header|J}}==
 
J has library routines to compute mean and standard deviation:
<lang j> require'statfns'
(mean,stddev) ?1000#0
0.484669 0.287482
0.294753
(mean,stddev) ?10000#0
0.503642 0.290777
0.289972
(mean,stddev) ?100000#0
0.288626499677 0.288726</lang>
 
but they are not quite what is being asked for here.
Line 210:
Instead:
 
<lang j>stddevPmeanstddevP=:3 :0
NB. compute mean and std dev of y random numbers
NB. picked from even distribution between 0 and 1
s=.t=. 0
for_n.i.<.y%1e6 do.
ts=.ts+ +/(( data=. ?1e6#0)-0.5)^2
t=.t+ +/(data-0.5)^2
end.
ts=.ts+ +/(( data=. ?(1e6|y)#0)-0.5)^2
t=.t++/(data-0.5)^2
(s%y),%:t%y
)</lang>
 
For a histogram:
 
<lang j>histogram=: <: @ (#/.~) @ (i.@#@[ , I.)
require'plot'
plot ((%*1+i.)100) ([;histogram) ?10000#0</lang>
 
(should upload an image generated this way)
 
Example use:
 
<lang j> stddevPmeanstddevP 1000
0.501617 0.288271
0.281084
stddevPmeanstddevP 10000
0.49732 0.290061
0.287244
stddevPmeanstddevP 100000
0.288254498912 0.289179</lang>
 
(That said, note that these numbers are random, so reported standard deviation will vary with the random sample being tested.)
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.