Statistics/Basic: Difference between revisions

Content deleted Content added
Rdm (talk | contribs)
J: closer reading of task requirements
Line 197: Line 197:
=={{header|J}}==
=={{header|J}}==


J has library routines to compute standard deviation:
J has library routines to compute mean and standard deviation:
<lang j> require'statfns'
<lang j> require'statfns'
stddev ?1000#0
(mean,stddev) ?1000#0
0.484669 0.287482
0.294753
stddev ?10000#0
(mean,stddev) ?10000#0
0.503642 0.290777
0.289972
stddev ?100000#0
(mean,stddev) ?100000#0
0.288626</lang>
0.499677 0.288726</lang>


but they are not quite what is being asked for here.
but they are not quite what is being asked for here.
Line 210: Line 210:
Instead:
Instead:


<lang j>stddevP=:3 :0
<lang j>meanstddevP=:3 :0
NB. compute std dev of y random numbers
NB. compute mean and std dev of y random numbers
NB. picked from even distribution between 0 and 1
NB. picked from even distribution between 0 and 1
t=. 0
s=.t=. 0
for_n.i.<.y%1e6 do.
for_n.i.<.y%1e6 do.
t=.t++/((?1e6#0)-0.5)^2
s=.s+ +/ data=. ?1e6#0
t=.t+ +/(data-0.5)^2
end.
end.
t=.t++/((?(1e6|y)#0)-0.5)^2
s=.s+ +/ data=. ?(1e6|y)#0
t=.t++/(data-0.5)^2
%:t%y
(s%y),%:t%y
)</lang>
)</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:
Example use:


<lang j> stddevP 1000
<lang j> meanstddevP 1000
0.501617 0.288271
0.281084
stddevP 10000
meanstddevP 10000
0.49732 0.290061
0.287244
stddevP 100000
meanstddevP 100000
0.288254</lang>
0.498912 0.289179</lang>


(That said, note that these numbers are random, so reported standard deviation will vary with the random sample being tested.)
(That said, note that these numbers are random, so reported standard deviation will vary with the random sample being tested.)