On 6/29/07, Oleg Kobchenko <[EMAIL PROTECTED]> wrote:
It would be good to see the model.
Ok. I've not made this very pretty, and I avoided treating
a few statements which were not accompanied by definitions,
and beware of email induced line breaks, but here it is:
assertions=:4 :0
NB. y is population
NB. x is indices of sample values (we are only concerned with size, here)
mean=: +/ % #
s0=: %:@((%<:)[EMAIL PROTECTED] * mean)@:*:@:- mean NB. "sample standard
deviation"
s1=: %:@mean@:*:@:- mean NB. "population standard deviation"
n=:#x
n1=:n-1
NB. uncomment to use population model for samples:
NB. n1=:n
NB. s0=:s1
mu=: mean y
samples=:y {~"1 (n##y)#:i.n^~#y
NB. $\sum (X_i-\bar X)^2$
t0=: ([:+/2^~]-mean)"1 samples
NB. $=\sum ((X_i-\mu)-(\bar X -\mu))^2$
t1=: ([:+/2^~(mu-~])-mu-~mean)"1 samples
assert.t0-:t1
NB. $=\sum ((X_i-\mu)^2) -n(\bar X-\mu)^2$,
t2=: (([:+/2^~mu-~])-n*2^~mu-~mean)"1 samples
assert.t0-:t2
NB. since $\sum (X_i-\mu)=(\sum X_i) - n\mu = n(\bar X-\mu)$.
assert. 1=#~.(([:+/mu-~])"1;((n*mu)-~+/)"1;(n*mu-~mean)"1) samples
NB. Then $E(S^2)=E((1/n-1)\sum (X_i-\bar X)^2$
t3=: mean *:@s0"1 samples
t4=: mean ((%n1)*[:+/2^~]-mean)"1 samples
assert. t3-:t4
NB. $=(1/n-1)( \sum E((X_i-\mu)^2)-nE((\bar X-\mu)^2 ))$
t5=:(%n1)*+/"1 mean ([:*:(-mean))"1 samples
assert. t3-:t5
NB. $=(1/n-1)( \sum \sigma^2(X_i)- n\sigma^2(\bar X) )$
t6=:(%n1)* (+/*:n#s1 y) - n* *:@s1@:(mean"1) samples
assert. t3-:t6
NB. But $\sigma^2(X_i)=\sigma^2$, $\sigma^2(\bar X)=\sigma^2/n$, so
NB. $E(S^2)=(1/n-1)(n\sigma^2 -n(\sigma^2/n))=\sigma^2$.
t7=: (%n1)* (n**:s1 y) - n**:s1 mean"1 samples
assert. t3-:t7
)
Example use:
0 1 assertions 0 1
0.25
0 1 2 assertions 1 2 3 4 5 6
2.91667
Remove the leading NB. from these two lines:
NB. n1=:n
NB. s0=:s1
And the assertions are still valid:
0 1 assertions 0 1
0.125
0 1 2 assertions 1 2 3 4 5 6
1.94444
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm