# Segment 24

## Calculation Problems

1. Let $X$ be an R.V. that is a linear combination (with known, fixed coefficients $\alpha_k$) of twenty $N(0,1)$ deviates. That is, $X = \sum_{k=1}^{20} \alpha_k T_k$ where $T_k \sim N(0,1)$. How can you most simply form a t-value-squared (that is, something distributed as $\text{Chisquare}(1)$ from $X$? For some particular choice of $\alpha_k$'s (random is ok), generate a sample of $x$'s, plot their histogram, and show that it agrees with $\text{Chisquare}(1)$.

3. Reproduce the table of critical $\Delta\chi^2$ values shown in slide 7.

$\Delta\chi^2$ is the inverse cumulative function of the $\chi^2$ distribution with it's degree of freedom.

chi2.pph(q,df)


where q is the distribution's confidence leven and df the degree of freedom

## Class Activity

Worked with Jin and Sean

from scipy.stats import *
import scipy.optimize
def model(b, x):
err = norm.rvs(0,1)
return b[0] + (b[1]*x) + err
xvalue = np.arange(1,101)
x = [xvalue for i in range(1000)]

def genData():
yvalue = []
for i in range(len(xvalue)):
yvalue.append(model(np.array([3, 7]), xvalue[i]))
return yvalue

def chisq(b,x,y):
return (y-b[0]-(b[1]*x))

def gen1000Data():
return [genData() for i in range(1000)]

y = gen1000Data()
chimany = []
for i in range(1000):
bfit,cofit,chi,_,_ = scipy.optimize.leastsq(chisq, [1,2], args=(x[i],y[i]), full_output=True)
chimany.append(sum(chi['fvec']**2))

chichi = scipy.stats.chi2.pdf(xvalue,98)
chi = []
for i in range(len(chichi)):
chi.append(chichi[i]/sum(chichi))
plt.plot(chichi)
plt.hist(chimany,bins=50)