User:Noah/Segment7

From Computational Statistics (CSE383M and CS395T)
Jump to navigation Jump to search

Segment 7 (1 February 2013)

Computation Problems

1. <math>0=\frac{d\Delta^2}{da}=\frac{d}{da}(\langle x^2\rangle-2a\langle x\rangle+a^2)=-2\langle x\rangle+2a\implies a=\langle x\rangle</math>

2.

3.

In-class problems

Multinomial parameter estimation. With Dan, Kai, and User:Trettels.

1.

Noah Inclass 7 1.png

3.

<math>P(WBD|wbd) = \frac{N!}{W!B!D!}w^Wb^Bd^D</math>

2. For w=0.4, b=0.3, d=0.3, N=10, W=3, B=5, D=2, <math>\frac{N!}{W!B!D!}w^Wb^Bd^D = \frac{10!}{3!5!2!}0.4^3 0.3^5 0.3^2\approx 3.5%</math>

4.

<math>\begin{align} P(wbd|WBD)&\propto P(WBD|wbd)P(wbd|I) = P(WBD|wbd)\cdot 2 \\ P(wbd|WBD)&=\frac{w^Wb^Bd^D}{\int_0^1\int_0^{1-b} w^Wb^B(1-w-b)^D\,dw\,db}=\frac{w^Wb^Bd^D(W+B+D+2)!}{W!B!D!} \end{align}</math>

5. Code.

import collections
def load(n):
    c = collections.Counter()
    if n <= 0: return c
    for x in open("./data/first_10000_games.pgn"):
        if "Result" in x:
            c.update([x.split('"')[1]])
            n-= 1
            if n <= 0:
                return c
    return c
from math import exp, lgamma, factorial as fact
def lfact(n):
    return lgamma(n+1)
fine_xs = np.linspace(0, 1, num=200)
fine_ys = np.linspace(0, 1, num=200)
fine_xx, fine_yy = np.meshgrid(fine_xs, fine_ys)
def obs(N, W, B):
    D = N - W - B
    def posterior(w, b):
        if w+b>1: return 0
        d = 1-w-b
        return exp(W*log(w)+B*log(b)+D*log(d)-lfact(W)-lfact(B)-lfact(D)+lfact(W+B+D+2))
    print posterior(0,0),posterior(1,0),posterior(0,1),posterior(1,1)
    vectorized = np.vectorize(posterior)
    return plt.imshow(vectorized(fine_xx, fine_yy), extent=[0, 1, 0, 1], origin='lower')
def ngames(N):
    x = load(N)
    print N, x, sum(x)
    assert(N == len(tuple(x.elements())))
    print (N, x['1-0'],x['0-1'])
    return obs(N, x['1-0'],x['0-1'])

obs(0,0,0) # No prior knowledge, like part 1. Also like part 5a, N=0.
obs(10,3,5) # 3 white, 5 black, 2 draws, like part 2
ngames(3)
ngames(10)
ngames(100)
ngames(1000)
ngames(10000)