Miguel Mattis Maximum Likelihood Estimation Exercise

#Maximum Likelihood Estimation

The Maximum likelihood function will calculate the overall functions maximum likelihood that it will hit a specific number. It follows the function $p^n * (1-p)^(n-1)$. These will be calculated manually and then compared to using the actual function

```python
%%html
<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{"highlight":"#0000ff","nav":true,"resize":true,"toolbar":"zoom layers lightbox","edit":"_blank","xml":"<mxfile modified=\"2019-04-08T02:57:38.239Z\" host=\"www.draw.io\" agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36\" etag=\"HCbSf2SMyf0mKBm6y943\" version=\"10.6.0\" type=\"google\"><diagram id=\"MUb6XdWlYKxxCSPsXOCQ\" name=\"Page-1\">5VfJbtswEP0aH11os+wcvSU5pEUBp0t6I8yxxJTSqDRlS/36UhJprTHcNkYK9CTNG3I4nDfzbI3cZZTdCZKE75ECHzkWzUbuauQ4tnvjqUeB5BUy9TUQCEb1ohrYsJ+gQUujKaOwby2UiFyypA1uMY5hK1sYEQKP7WU75O1TExJAD9hsCe+jXxiVYYXOJlaN3wMLQnOybWlPRMxiDexDQvHYgNz1yF0KRFm9RdkSeFE8U5dq3+0L3lNiAmJ5yYZv1h2df7p9SLObx8/wDJY9puOpzk3m5sJA1f21iUKGGGBM+LpGFwLTmEIR1VJWveYBMVGgrcBnkDLXZJJUooJCGXHtVQmL/KveXxpPhfFuYsxV1nSucm1VuRYJvlgCDe0xFVs4c2/TSkQEIM+s809EqQ4HjEDlo/YJ4ESyQzsPolstOK2r2VAvmpDfIEfHPRCe6pNGjs9Vuosdqgs3afN/pGgc431Z+Lla4PpJVjvVW1A874HQvQmlMquiVb5eP0jIZJu+vRT4HZbIUSgkxrhoih3jvAMRzoJYmVvFDih8cQAhmRqtuXZEjNKyo44hk7BJSEnZUQlJr8t0JVQAyM6T3yfLbJjpQczNDFfmsR5r24x12BjpqXUlep0BervDGNN5IWJ1URs8XDoO/Yo0rjx0Y4Nd3OX6hI/IyrbUBXesdsEdv1PJakr1rqZydQJ5TjuQ7XUCVWPcC1Sycrr2nxM1eVuRrHXxqSWLVxdJ90KR9N5SJN0rieQjYfz/E0nX7cyssZsq6Q1oxuxaKuldiV97/OFSdttVHuKhp8p/R4LXJsGzBkhwBkjoCuyrkeBfiYR/lgLP78zBwJ+FV2JAmfVHQPWTVX9Kuetf</diagram></mxfile>"}"></div>

```

<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{"highlight":"#0000ff","nav":true,"resize":true,"toolbar":"zoom layers lightbox","edit":"_blank","xml":"<mxfile modified=\"2019-04-08T02:57:38.239Z\" host=\"www.draw.io\" agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36\" etag=\"HCbSf2SMyf0mKBm6y943\" version=\"10.6.0\" type=\"google\"><diagram id=\"MUb6XdWlYKxxCSPsXOCQ\" name=\"Page-1\">5VfJbtswEP0aH11os+wcvSU5pEUBp0t6I8yxxJTSqDRlS/36UhJprTHcNkYK9CTNG3I4nDfzbI3cZZTdCZKE75ECHzkWzUbuauQ4tnvjqUeB5BUy9TUQCEb1ohrYsJ+gQUujKaOwby2UiFyypA1uMY5hK1sYEQKP7WU75O1TExJAD9hsCe+jXxiVYYXOJlaN3wMLQnOybWlPRMxiDexDQvHYgNz1yF0KRFm9RdkSeFE8U5dq3+0L3lNiAmJ5yYZv1h2df7p9SLObx8/wDJY9puOpzk3m5sJA1f21iUKGGGBM+LpGFwLTmEIR1VJWveYBMVGgrcBnkDLXZJJUooJCGXHtVQmL/KveXxpPhfFuYsxV1nSucm1VuRYJvlgCDe0xFVs4c2/TSkQEIM+s809EqQ4HjEDlo/YJ4ESyQzsPolstOK2r2VAvmpDfIEfHPRCe6pNGjs9Vuosdqgs3afN/pGgc431Z+Lla4PpJVjvVW1A874HQvQmlMquiVb5eP0jIZJu+vRT4HZbIUSgkxrhoih3jvAMRzoJYmVvFDih8cQAhmRqtuXZEjNKyo44hk7BJSEnZUQlJr8t0JVQAyM6T3yfLbJjpQczNDFfmsR5r24x12BjpqXUlep0BervDGNN5IWJ1URs8XDoO/Yo0rjx0Y4Nd3OX6hI/IyrbUBXesdsEdv1PJakr1rqZydQJ5TjuQ7XUCVWPcC1Sycrr2nxM1eVuRrHXxqSWLVxdJ90KR9N5SJN0rieQjYfz/E0nX7cyssZsq6Q1oxuxaKuldiV97/OFSdttVHuKhp8p/R4LXJsGzBkhwBkjoCuyrkeBfiYR/lgLP78zBwJ+FV2JAmfVHQPWTVX9Kuetf</diagram></mxfile>"}">

```python
import numpy as np
```

```python
def coin(p): #defines a function where coin(p), if generating a random number from 0 to 1, will return 1 if it less than and 0 if not, than the value p.

  return int(np.random.random() < p)
```

```python
N = 100 #iterates the function 100 times
p = 0.3

S = np.zeros((N,))

for i in range(N):

  S[i] = coin(p) #resets the function each time and for each iteration from 1 to 100, checks if the random variable from 0 to 1 is less than p
```

```python
coin(0.3)
```

    1

```python
S
```

    array([1., 0., 1., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,
           0., 0., 1., 0., 1., 1., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1.,
           0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 0., 0., 1., 1., 0., 0., 0.,
           0., 1., 0., 0., 0., 1., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
           0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0.,
           0., 1., 0., 1., 0., 1., 1., 0., 1., 1., 0., 0., 1., 0., 0.])

```python
sum(S)/S.shape[0] #the sum of the results being 1 divided by the shape of the function being 100
```

    0.31

```python
S.shape[0]
```

    100

```python
num_heads = np.sum(S) #if we indicate it as a coin, the 1 would be heads and thus num_heads is the sum of S which means below 0.3
```

```python
num_tails = N - np.sum(S) #if we indicate it as a coin, the 1 would be heads and thus num_tails is N minus the sum of S which means the values above 0.3
```

```python

```

```python
(p**num_heads)*(1-p)**num_tails #follows the formula p^n * (1-p)^n-1
```

    1.2662622426477919e-27

```python

```

```python
(p**np.sum(S))*((1-p)**np.sum(np.logical_not(S))) #another way of stating the above
```

    1.2662622426477919e-27

```python
np.logical_not(S).astype(int) #tells us the opposite of list S
```

    array([0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0,
           0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
           1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1,
           1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1,
           0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1])

```python

```

```python
def likelihood(p,S): #defining the likelihood function

  return (p**np.sum(S))*((1-p)**np.sum(np.logical_not(S)))
```

```python

```

```python
def likelihood(p,S): #defining the likelihood function with respect to coin values

  num_heads = np.sum(S)
  num_tails = np.sum(np.logical_not(S))

  return (p**num_heads)*((1-p)**num_tails)
```

```python
start = 0 #goes from value 0 to 1 with 100 even steps
stop = 1
steps = 100
p = np.linspace(start,stop,steps)
```

```python
p = np.linspace(0,1,100)
```

```python
 p
```

    array([0.        , 0.01010101, 0.02020202, 0.03030303, 0.04040404,
           0.05050505, 0.06060606, 0.07070707, 0.08080808, 0.09090909,
           0.1010101 , 0.11111111, 0.12121212, 0.13131313, 0.14141414,
           0.15151515, 0.16161616, 0.17171717, 0.18181818, 0.19191919,
           0.2020202 , 0.21212121, 0.22222222, 0.23232323, 0.24242424,
           0.25252525, 0.26262626, 0.27272727, 0.28282828, 0.29292929,
           0.3030303 , 0.31313131, 0.32323232, 0.33333333, 0.34343434,
           0.35353535, 0.36363636, 0.37373737, 0.38383838, 0.39393939,
           0.4040404 , 0.41414141, 0.42424242, 0.43434343, 0.44444444,
           0.45454545, 0.46464646, 0.47474747, 0.48484848, 0.49494949,
           0.50505051, 0.51515152, 0.52525253, 0.53535354, 0.54545455,
           0.55555556, 0.56565657, 0.57575758, 0.58585859, 0.5959596 ,
           0.60606061, 0.61616162, 0.62626263, 0.63636364, 0.64646465,
           0.65656566, 0.66666667, 0.67676768, 0.68686869, 0.6969697 ,
           0.70707071, 0.71717172, 0.72727273, 0.73737374, 0.74747475,
           0.75757576, 0.76767677, 0.77777778, 0.78787879, 0.7979798 ,
           0.80808081, 0.81818182, 0.82828283, 0.83838384, 0.84848485,
           0.85858586, 0.86868687, 0.87878788, 0.88888889, 0.8989899 ,
           0.90909091, 0.91919192, 0.92929293, 0.93939394, 0.94949495,
           0.95959596, 0.96969697, 0.97979798, 0.98989899, 1.        ])

```python
L = likelihood(np.array([0.0,0.1]),S) #chance of the probability of the coin
```

```python
L #the likelihood at 0 of it occuring
```

    array([0.00000000e+00, 6.96198609e-35])

```python
L = likelihood(p,S) #the likelihood off all the runs
```

```python
L
```

    array([0.00000000e+000, 6.77765975e-063, 7.17232681e-054, 1.00914719e-048,
           3.65821340e-045, 1.77980562e-042, 2.42377752e-040, 1.36735054e-038,
           4.03762710e-037, 7.25657159e-036, 8.79814264e-035, 7.74387187e-034,
           5.22309107e-033, 2.81266234e-032, 1.24840506e-031, 4.68360552e-031,
           1.51561319e-030, 4.30083635e-030, 1.08491197e-029, 2.46065809e-029,
           5.06616937e-029, 9.54566662e-029, 1.65746239e-028, 2.66797235e-028,
           4.00180762e-028, 5.61835880e-028, 7.41193626e-028, 9.21933260e-028,
           1.08444011e-027, 1.20943696e-027, 1.28181898e-027, 1.29362389e-027,
           1.24534951e-027, 1.14536585e-027, 1.00774018e-027, 8.49196352e-028,
           6.86050328e-028, 5.31814974e-028, 3.95850002e-028, 2.83086810e-028,
           1.94593834e-028, 1.28620933e-028, 8.17661565e-029, 5.00007509e-029,
           2.94129840e-029, 1.66432407e-029, 9.05749963e-030, 4.73963445e-030,
           2.38397145e-030, 1.15209773e-030, 5.34663081e-031, 2.38123018e-031,
           1.01703615e-031, 4.16219942e-032, 1.63061167e-032, 6.10883777e-033,
           2.18591270e-033, 7.46108699e-034, 2.42568529e-034, 7.49949988e-035,
           2.20103621e-035, 6.12031242e-036, 1.60894751e-036, 3.98942336e-037,
           9.30582313e-038, 2.03629114e-038, 4.16681668e-039, 7.94598842e-040,
           1.40673959e-040, 2.30233719e-041, 3.46722189e-042, 4.77962158e-043,
           5.99635942e-044, 6.80221280e-045, 6.92666925e-046, 6.27993840e-047,
           5.02253054e-048, 3.50637206e-049, 2.11127381e-050, 1.08140923e-051,
           4.63733641e-053, 1.63425182e-054, 4.63111472e-056, 1.02844295e-057,
           1.73554262e-059, 2.14437976e-061, 1.85363336e-063, 1.05945703e-065,
           3.72853868e-068, 7.37175251e-071, 7.25657159e-074, 3.01976262e-077,
           4.22345576e-081, 1.41869233e-085, 6.79789263e-091, 1.94111697e-097,
           6.43065692e-106, 6.27298145e-118, 1.46045738e-138, 0.00000000e+000])

```python

```

```python
import matplotlib.pyplot as plt
```

```python
plt.plot(p,L)
```

```python
np.argmax(L) #The maximum
```

    31

```python
p[np.argmax(L)] #probability at the maximum
```

    0.31313131313131315

```python
np.sum
```

    <function numpy.core.fromnumeric.sum>

```python

```

```python
L
```

    array([0.00000000e+000, 6.77765975e-063, 7.17232681e-054, 1.00914719e-048,
           3.65821340e-045, 1.77980562e-042, 2.42377752e-040, 1.36735054e-038,
           4.03762710e-037, 7.25657159e-036, 8.79814264e-035, 7.74387187e-034,
           5.22309107e-033, 2.81266234e-032, 1.24840506e-031, 4.68360552e-031,
           1.51561319e-030, 4.30083635e-030, 1.08491197e-029, 2.46065809e-029,
           5.06616937e-029, 9.54566662e-029, 1.65746239e-028, 2.66797235e-028,
           4.00180762e-028, 5.61835880e-028, 7.41193626e-028, 9.21933260e-028,
           1.08444011e-027, 1.20943696e-027, 1.28181898e-027, 1.29362389e-027,
           1.24534951e-027, 1.14536585e-027, 1.00774018e-027, 8.49196352e-028,
           6.86050328e-028, 5.31814974e-028, 3.95850002e-028, 2.83086810e-028,
           1.94593834e-028, 1.28620933e-028, 8.17661565e-029, 5.00007509e-029,
           2.94129840e-029, 1.66432407e-029, 9.05749963e-030, 4.73963445e-030,
           2.38397145e-030, 1.15209773e-030, 5.34663081e-031, 2.38123018e-031,
           1.01703615e-031, 4.16219942e-032, 1.63061167e-032, 6.10883777e-033,
           2.18591270e-033, 7.46108699e-034, 2.42568529e-034, 7.49949988e-035,
           2.20103621e-035, 6.12031242e-036, 1.60894751e-036, 3.98942336e-037,
           9.30582313e-038, 2.03629114e-038, 4.16681668e-039, 7.94598842e-040,
           1.40673959e-040, 2.30233719e-041, 3.46722189e-042, 4.77962158e-043,
           5.99635942e-044, 6.80221280e-045, 6.92666925e-046, 6.27993840e-047,
           5.02253054e-048, 3.50637206e-049, 2.11127381e-050, 1.08140923e-051,
           4.63733641e-053, 1.63425182e-054, 4.63111472e-056, 1.02844295e-057,
           1.73554262e-059, 2.14437976e-061, 1.85363336e-063, 1.05945703e-065,
           3.72853868e-068, 7.37175251e-071, 7.25657159e-074, 3.01976262e-077,
           4.22345576e-081, 1.41869233e-085, 6.79789263e-091, 1.94111697e-097,
           6.43065692e-106, 6.27298145e-118, 1.46045738e-138, 0.00000000e+000])

```python
#the symbolic representation of the maximum likelihood formula
```

```python
from sympy import *
```

```python
N_heads, N_total, p = symbols('N_heads,N_total,p')
```

```python
f = p**N_heads*(1-p)**(N_total-N_heads)
```

```python
f
```

    p**N_heads*(-p + 1)**(-N_heads + N_total)

```python
df_dp = diff(f,p)
```

```python
df_dp
```

    N_heads*p**N_heads*(-p + 1)**(-N_heads + N_total)/p + p**N_heads*(N_heads - N_total)*(-p + 1)**(-N_heads + N_total)/(-p + 1)

```python
solve(df_dp,p)
```

    [N_heads/N_total]

```python

```

```python

```

```python
import numpy as np
```

```python
mu = 2.1
sigma = 0.12
```

```python
x = sigma * np.random.randn(1,10) + mu #follows mx+b for each point
```

```python
np.random.random(1)
```

    array([0.62692432])

```python
plt.hist(np.random.randn(10000,),50); #demonstrates the normal curve and also proves the maximum likelihood function
```

```python
x = sigma * np.random.randn(1,10) + mu
```

```python
x = sigma * np.random.randn(1000,1) + mu
```

```python
plt.hist(x,50);
```

```python

```

```python

```

```python
x
```

    array([[2.06685475],
           [1.81390802],
           [2.31810779],
           [2.17342273],
           [2.12584949],
           [2.19490566],
           [2.26468229],
           [2.19171028],
           [2.08141936],
           [2.1333973 ],
           [2.11643485],
           [1.99170918],
           [1.93998128],
           [2.16991389],
           [2.21206076],
           [2.00577596],
           [2.25319232],
           [2.11200648],
           [1.91577177],
           [2.19305921],
           [1.88463519],
           [2.26713262],
           [2.05838411],
           [1.94200784],
           [2.19344005],
           [1.8573774 ],
           [2.16821863],
           [2.23944842],
           [1.88607512],
           [1.96784607],
           [2.17942375],
           [2.27166169],
           [2.3497815 ],
           [2.0892895 ],
           [2.31610199],
           [2.29154415],
           [1.91837962],
           [1.77412449],
           [2.17677456],
           [2.3235171 ],
           [2.03354734],
           [1.92377108],
           [2.27859674],
           [2.09309044],
           [2.07193233],
           [2.07221959],
           [2.25835603],
           [2.06560394],
           [2.19544416],
           [2.20791498],
           [2.14424628],
           [1.9786931 ],
           [2.19149751],
           [2.02739117],
           [2.07314428],
           [2.06952293],
           [2.19083726],
           [2.05560549],
           [2.10424278],
           [2.00299266],
           [2.02674208],
           [2.16199625],
           [2.15309594],
           [2.19736195],
           [2.16400564],
           [2.16408321],
           [2.08365157],
           [2.1599499 ],
           [2.05293688],
           [1.93341125],
           [2.15469612],
           [1.91915973],
           [1.88379775],
           [2.07313185],
           [2.15040402],
           [1.89673783],
           [2.13035169],
           [2.21589488],
           [1.98913131],
           [2.00991705],
           [2.05759562],
           [1.99027609],
           [2.19331958],
           [2.16717387],
           [1.96725591],
           [1.98866287],
           [1.9006059 ],
           [1.98420654],
           [2.25067924],
           [1.98535412],
           [1.9061676 ],
           [2.03396138],
           [2.10850697],
           [2.2203154 ],
           [1.87717236],
           [2.25598364],
           [2.223821  ],
           [2.21002948],
           [2.0888746 ],
           [2.21048047],
           [2.04187809],
           [2.00526043],
           [2.39268396],
           [2.15293203],
           [1.94739815],
           [2.09756529],
           [2.1275176 ],
           [1.98673153],
           [2.12748657],
           [2.13859106],
           [1.92687741],
           [2.49801767],
           [2.17399161],
           [2.0163455 ],
           [2.08317063],
           [1.9544432 ],
           [1.9693243 ],
           [2.25417904],
           [2.09905561],
           [2.08972875],
           [2.30772735],
           [2.05245764],
           [1.97112785],
           [2.30517964],
           [1.92223261],
           [2.33640923],
           [1.96296693],
           [2.17847506],
           [1.91773483],
           [2.00417823],
           [2.02087478],
           [2.11507381],
           [2.23544865],
           [2.00182133],
           [1.98753494],
           [2.15251517],
           [2.05426755],
           [2.05860313],
           [2.10802003],
           [2.00586173],
           [2.19388503],
           [1.96251916],
           [2.0808349 ],
           [2.00883838],
           [2.26799234],
           [2.33918674],
           [2.14515259],
           [2.13213402],
           [2.1166617 ],
           [2.1154757 ],
           [1.9489489 ],
           [2.23730559],
           [2.21997275],
           [2.02465232],
           [2.1333076 ],
           [2.06600712],
           [2.04964263],
           [2.22047226],
           [2.1774106 ],
           [2.21939663],
           [2.02550359],
           [2.02735744],
           [2.18201036],
           [2.12645757],
           [2.02830853],
           [2.19864315],
           [2.19720294],
           [1.88698694],
           [2.20187529],
           [2.05317182],
           [2.02026733],
           [2.12361332],
           [1.87109206],
           [2.17719231],
           [2.21096214],
           [2.21510144],
           [1.97012036],
           [2.16052476],
           [1.84142124],
           [2.20456671],
           [2.24036725],
           [1.90627733],
           [2.31892642],
           [2.26391082],
           [2.16794906],
           [2.19673582],
           [2.12605907],
           [2.22331094],
           [1.82266133],
           [2.15473635],
           [2.21193877],
           [2.05682701],
           [2.03020103],
           [2.22545373],
           [2.17246337],
           [2.02797088],
           [1.93971211],
           [2.12300105],
           [2.10647709],
           [2.07836588],
           [2.13517772],
           [1.8177164 ],
           [2.22348536],
           [2.21864678],
           [2.33570309],
           [2.11997307],
           [2.12547092],
           [2.06857151],
           [2.03527858],
           [2.09626178],
           [2.09747024],
           [1.94863328],
           [2.23550094],
           [2.0498684 ],
           [2.06928464],
           [2.26274406],
           [2.04898804],
           [2.10588642],
           [2.09021734],
           [1.98128699],
           [2.12250107],
           [2.20129956],
           [2.09137905],
           [2.26821184],
           [2.37941714],
           [2.00947168],
           [1.88738024],
           [1.90268358],
           [2.22266873],
           [2.27618536],
           [2.3402416 ],
           [2.02268757],
           [2.2099784 ],
           [2.10309914],
           [1.94197999],
           [2.19562916],
           [1.98811745],
           [1.99858586],
           [2.03948585],
           [2.02266921],
           [2.19083884],
           [2.23548523],
           [2.05628169],
           [2.07062148],
           [2.32276411],
           [2.12881417],
           [1.97067475],
           [1.93242625],
           [2.0547178 ],
           [1.97891881],
           [2.23156326],
           [2.26494026],
           [2.16448006],
           [2.14592729],
           [2.25463947],
           [2.22182937],
           [2.1271648 ],
           [2.06835461],
           [1.86628428],
           [2.00190391],
           [1.96113039],
           [2.1625047 ],
           [2.12835552],
           [1.94076627],
           [2.16224787],
           [2.03195966],
           [2.30477498],
           [2.26502297],
           [1.85556359],
           [2.05366348],
           [2.02302777],
           [2.1436233 ],
           [2.06803273],
           [2.12080155],
           [2.06172736],
           [2.02654056],
           [2.13896157],
           [2.08070561],
           [1.940267  ],
           [1.93083157],
           [1.98745982],
           [1.96590401],
           [2.06823222],
           [2.00504152],
           [2.07602589],
           [1.9768243 ],
           [2.10522782],
           [2.10144959],
           [1.94607957],
           [2.00570971],
           [1.96725115],
           [2.09147261],
           [2.18337913],
           [2.10615698],
           [2.15604267],
           [2.20425509],
           [2.24378359],
           [2.17376081],
           [2.22753459],
           [2.02569811],
           [2.01367996],
           [2.08598083],
           [2.07295477],
           [2.16203692],
           [2.04238448],
           [2.23469748],
           [2.09606114],
           [2.20234004],
           [2.17234216],
           [2.02975222],
           [2.13289076],
           [2.09280352],
           [2.16842143],
           [2.24722127],
           [2.04579319],
           [1.96720774],
           [2.09294744],
           [2.26234757],
           [2.23325537],
           [1.93802087],
           [1.94582897],
           [2.08633811],
           [1.99169995],
           [2.19520968],
           [2.17372595],
           [2.02052873],
           [1.89019574],
           [2.08469011],
           [2.24025847],
           [2.00872521],
           [1.9348996 ],
           [1.99337564],
           [2.13842119],
           [1.96693322],
           [2.03752573],
           [2.19949457],
           [1.8540645 ],
           [1.97334141],
           [2.09094358],
           [2.05243301],
           [2.09897271],
           [2.18497396],
           [2.10824457],
           [2.02236033],
           [1.87409759],
           [2.03985448],
           [1.93824508],
           [2.05050092],
           [2.20892069],
           [2.11224439],
           [2.15976258],
           [2.06833868],
           [1.88252865],
           [1.90169351],
           [2.29584884],
           [1.95173625],
           [2.24015962],
           [2.09525657],
           [1.78940817],
           [2.12457966],
           [2.32964601],
           [2.04945341],
           [2.07316584],
           [2.26623125],
           [2.22802492],
           [1.96420405],
           [2.03799488],
           [2.08233998],
           [1.95706022],
           [2.05950863],
           [1.9049686 ],
           [2.3942698 ],
           [2.14968138],
           [2.21443648],
           [1.99078205],
           [1.89483133],
           [2.06279731],
           [1.96969841],
           [2.11700386],
           [2.08684568],
           [2.24807848],
           [2.08006956],
           [2.09605794],
           [2.09550728],
           [2.15228103],
           [2.10934719],
           [2.20633118],
           [2.05033005],
           [2.06063339],
           [1.86551453],
           [2.24746363],
           [2.33349535],
           [2.08421375],
           [1.92652876],
           [2.01463703],
           [2.23035235],
           [2.21261201],
           [2.30222029],
           [2.08141084],
           [2.16959936],
           [2.16608778],
           [2.31847881],
           [2.06109657],
           [1.91011524],
           [2.14628973],
           [2.02386173],
           [1.96937253],
           [2.11684862],
           [2.2260349 ],
           [2.12252472],
           [2.07548828],
           [2.18625621],
           [2.06717239],
           [1.96199112],
           [2.16736488],
           [2.05570422],
           [2.18197472],
           [1.99365786],
           [2.04533183],
           [2.05585632],
           [2.06290469],
           [2.10651492],
           [2.10418818],
           [2.05852054],
           [1.94354743],
           [1.86515784],
           [2.27789031],
           [2.16283699],
           [2.02735735],
           [2.09454129],
           [2.15446995],
           [2.11169858],
           [2.0238011 ],
           [2.09456997],
           [2.12563594],
           [2.05341457],
           [1.97564085],
           [1.73234324],
           [2.2966204 ],
           [1.79353232],
           [1.93404024],
           [2.02618644],
           [2.07398672],
           [1.97199306],
           [2.10864346],
           [2.05219594],
           [2.05259897],
           [2.07606541],
           [2.01619337],
           [2.04612295],
           [1.95054407],
           [2.07086131],
           [2.19587218],
           [1.98507223],
           [2.22697914],
           [2.2802287 ],
           [2.30123354],
           [1.90130439],
           [2.20988345],
           [1.99456598],
           [2.08591081],
           [2.14668771],
           [1.93617437],
           [2.15374806],
           [2.18757523],
           [2.22819916],
           [1.87709136],
           [2.08782392],
           [2.30560814],
           [2.02004125],
           [2.01041528],
           [2.13494822],
           [2.04925443],
           [2.24727058],
           [2.16653757],
           [2.06918966],
           [2.00937246],
           [1.96796468],
           [2.21908822],
           [2.47674177],
           [1.97675328],
           [1.94857987],
           [2.31403206],
           [1.97849418],
           [2.21234696],
           [2.05370566],
           [2.12181368],
           [2.21402259],
           [2.12918846],
           [2.24507158],
           [2.16673432],
           [2.2852832 ],
           [2.15140742],
           [2.07267306],
           [2.30441092],
           [2.33225387],
           [2.1813221 ],
           [2.0531461 ],
           [1.9407031 ],
           [1.81671758],
           [2.18937431],
           [2.08526568],
           [1.99518318],
           [2.05495993],
           [2.18627911],
           [2.03139057],
           [2.08541407],
           [2.16371451],
           [1.94373973],
           [1.9599917 ],
           [2.10598656],
           [2.10670161],
           [2.20374312],
           [2.1479496 ],
           [2.10382249],
           [1.8857241 ],
           [1.88646487],
           [2.1069343 ],
           [2.07000577],
           [2.1274314 ],
           [2.15014498],
           [2.07165594],
           [2.24718382],
           [2.12170445],
           [2.31049256],
           [1.93482823],
           [2.13666641],
           [2.06460908],
           [2.20403929],
           [2.16858739],
           [2.15494449],
           [2.08898473],
           [2.22573341],
           [2.28647317],
           [1.93983245],
           [2.10168811],
           [2.29929111],
           [1.83307069],
           [2.04141476],
           [2.00508452],
           [2.05055125],
           [2.24457246],
           [1.80569453],
           [2.01562205],
           [2.01504776],
           [2.15855581],
           [2.17983163],
           [2.02570524],
           [2.11626858],
           [2.06173114],
           [2.10859558],
           [2.09198799],
           [2.18127738],
           [1.97427871],
           [2.01315293],
           [2.20690627],
           [2.05331056],
           [2.055664  ],
           [2.12270644],
           [2.04878063],
           [2.14607312],
           [2.08769985],
           [2.0949881 ],
           [1.85893222],
           [2.24516016],
           [2.00115167],
           [2.16704654],
           [2.02474217],
           [2.36870857],
           [2.12195004],
           [2.03769918],
           [2.19721654],
           [1.97416244],
           [1.97285461],
           [2.1214597 ],
           [2.30499901],
           [2.04164586],
           [1.92933482],
           [1.98208297],
           [2.15302131],
           [2.17252357],
           [2.01551379],
           [2.14522815],
           [2.25846047],
           [2.10083359],
           [1.76691982],
           [1.85674321],
           [1.93658877],
           [2.03071589],
           [2.1139108 ],
           [2.18944199],
           [2.0805019 ],
           [2.14032781],
           [2.10384606],
           [2.01509272],
           [2.02844693],
           [1.99672021],
           [2.10784023],
           [2.02900741],
           [1.97487638],
           [1.9413389 ],
           [2.18881561],
           [2.22717444],
           [2.23853186],
           [2.20243636],
           [2.07933969],
           [2.33238748],
           [2.16126755],
           [2.10232322],
           [2.27128991],
           [1.98865163],
           [2.02670264],
           [1.92508857],
           [2.01464843],
           [2.0231763 ],
           [2.0869    ],
           [2.02345306],
           [2.11757287],
           [2.40111167],
           [2.09628962],
           [2.11110291],
           [2.24726399],
           [2.0113867 ],
           [2.1983956 ],
           [2.07261531],
           [2.18492554],
           [2.08325284],
           [2.10418714],
           [2.28985317],
           [2.21507706],
           [2.1213812 ],
           [2.18265222],
           [2.12684268],
           [2.18523434],
           [2.36671666],
           [2.05894365],
           [1.97979086],
           [2.06157382],
           [2.10955778],
           [2.17016697],
           [2.01487212],
           [2.16922527],
           [2.12217307],
           [1.91979239],
           [2.1562103 ],
           [1.93736178],
           [2.14033855],
           [2.06986454],
           [2.13527998],
           [2.10521235],
           [2.18870165],
           [2.11672036],
           [2.09906567],
           [1.82319022],
           [2.24445536],
           [2.16415651],
           [2.24786287],
           [1.90253265],
           [2.14353483],
           [2.11870962],
           [2.19186144],
           [2.18072219],
           [1.90123404],
           [1.86541227],
           [2.31354621],
           [2.29385389],
           [2.15463989],
           [1.8731619 ],
           [2.1513847 ],
           [2.01985372],
           [1.96693462],
           [2.25195482],
           [2.06749569],
           [2.11484208],
           [2.10899706],
           [2.05759741],
           [1.84323164],
           [2.16262399],
           [2.27756178],
           [2.10769192],
           [1.94014103],
           [2.00958062],
           [1.97663666],
           [2.17820623],
           [2.23069061],
           [2.18374986],
           [2.04748918],
           [2.0619193 ],
           [2.22999587],
           [2.30222636],
           [2.27817161],
           [2.01927116],
           [2.24203997],
           [2.12413774],
           [2.01399536],
           [2.09570221],
           [2.12504899],
           [2.09932392],
           [2.41782195],
           [2.11775083],
           [1.93457276],
           [2.3455057 ],
           [1.94152099],
           [1.77605199],
           [2.27944887],
           [2.16622016],
           [1.93399337],
           [1.88080203],
           [1.93214454],
           [1.97079133],
           [2.05886305],
           [2.12431129],
           [2.12355692],
           [2.18296362],
           [2.09438216],
           [2.20596791],
           [2.014826  ],
           [2.1725357 ],
           [2.04842681],
           [2.16591738],
           [2.17945099],
           [2.20844963],
           [1.97984249],
           [1.98389784],
           [2.03057282],
           [1.99731445],
           [1.97443278],
           [2.22275691],
           [2.03918136],
           [2.06902134],
           [2.20417056],
           [1.98084428],
           [2.12673307],
           [2.11197176],
           [1.92337745],
           [2.11828017],
           [2.05855037],
           [2.22548305],
           [2.23602909],
           [2.18135643],
           [1.99141979],
           [2.19482192],
           [2.07569464],
           [1.79574857],
           [1.93758908],
           [2.27446404],
           [2.11078817],
           [1.95072314],
           [2.14584367],
           [2.01846363],
           [2.14566222],
           [2.07153026],
           [2.26984385],
           [2.05193495],
           [2.111677  ],
           [2.04176536],
           [2.18947794],
           [2.10345149],
           [2.15825612],
           [2.27846637],
           [1.99066082],
           [2.28287466],
           [2.06842843],
           [1.83610624],
           [2.16064601],
           [2.05877332],
           [2.21052743],
           [2.03181057],
           [2.06731083],
           [1.87772854],
           [2.24758414],
           [2.03482969],
           [2.18270949],
           [2.20060619],
           [2.21170043],
           [2.04194058],
           [2.11815028],
           [2.05235141],
           [2.11598525],
           [2.15211973],
           [2.04981475],
           [2.05019754],
           [2.15034373],
           [2.16564   ],
           [2.03039497],
           [2.00777306],
           [2.20168416],
           [2.03659021],
           [1.95210855],
           [2.06210836],
           [1.99333481],
           [2.02919583],
           [2.18412766],
           [2.10694653],
           [2.10854516],
           [2.33041871],
           [2.10594779],
           [2.15712427],
           [2.34021765],
           [2.0967218 ],
           [2.31653698],
           [2.05181555],
           [2.24675627],
           [2.26463388],
           [2.03315751],
           [2.02481142],
           [2.03890416],
           [2.23864932],
           [2.19464609],
           [2.25502326],
           [2.23000838],
           [2.09067193],
           [2.25414866],
           [2.10807886],
           [2.33303439],
           [2.03664491],
           [1.8709613 ],
           [2.14035006],
           [2.11018105],
           [2.11330252],
           [2.21956574],
           [2.0697035 ],
           [2.0393856 ],
           [2.09809029],
           [2.10015466],
           [2.0257366 ],
           [2.22718999],
           [2.26674553],
           [1.94891674],
           [2.1059159 ],
           [2.06412148],
           [1.91493971],
           [1.87756779],
           [2.06946583],
           [2.10683974],
           [1.99452602],
           [2.16202671],
           [1.84880753],
           [2.24443067],
           [1.97306518],
           [2.13314811],
           [2.08781184],
           [2.26355744],
           [2.09779182],
           [1.96193131],
           [2.20091134],
           [2.04357742],
           [2.15051654],
           [2.11273153],
           [2.14111638],
           [1.97446034],
           [2.32305803],
           [2.0505491 ],
           [1.96599257],
           [2.21583022],
           [1.81732353],
           [2.20855983],
           [2.25113505],
           [2.02976663],
           [1.97777673],
           [2.13497096],
           [2.07604595],
           [2.14679841],
           [2.20729523],
           [1.98009422],
           [2.08023948],
           [2.51286635],
           [2.17235732],
           [2.13674022],
           [1.93328224],
           [2.27803568],
           [2.14738926],
           [2.00881908],
           [2.10997471],
           [2.07555257],
           [1.91261558],
           [2.2216809 ],
           [2.00500325],
           [2.13093045],
           [2.24164422],
           [2.27315042],
           [2.1996608 ],
           [1.81532318],
           [2.07922291],
           [2.09072107],
           [2.27523784],
           [1.88823583],
           [2.19638786],
           [2.29954202],
           [1.93337706],
           [1.97819266],
           [2.11522523],
           [2.19287277],
           [2.22616794],
           [2.16823007],
           [1.94071121],
           [2.04812204],
           [2.00760447],
           [1.86500482],
           [1.97048544],
           [2.21739157],
           [2.03940739],
           [2.14610698],
           [2.12261753],
           [2.09765853],
           [1.83910377],
           [2.34371749],
           [2.0497198 ],
           [2.38118174],
           [2.18073265],
           [2.09078152],
           [2.09363983],
           [2.13570187],
           [2.15080575],
           [2.17381212],
           [2.14248036],
           [1.95502392],
           [2.06094099],
           [2.09540936],
           [2.26803922],
           [2.05537933],
           [2.14595769],
           [1.98752321],
           [1.99427504],
           [1.87329103],
           [2.01600594],
           [2.08065209],
           [2.32172282],
           [2.00720141],
           [1.91916717],
           [2.21791002],
           [2.14045715],
           [2.11747602],
           [2.04134271],
           [1.92075442],
           [1.88757698],
           [2.03358484],
           [2.12710028],
           [2.18323802],
           [2.1533083 ],
           [2.25105053],
           [2.21155017],
           [2.16413857],
           [2.18347906],
           [2.23166715],
           [2.04018211],
           [2.0850005 ],
           [2.02741497],
           [2.19298445],
           [1.89960472],
           [2.06140795],
           [1.71357179],
           [2.10613771],
           [1.87997775],
           [2.19725986],
           [2.10401712],
           [2.14464102],
           [2.23696538],
           [2.27813513],
           [2.01408863],
           [1.88431219],
           [1.95075563],
           [1.89903188],
           [1.97980344],
           [2.2206825 ],
           [2.33727413],
           [1.87363355],
           [2.00784542],
           [2.11328206],
           [1.98954698],
           [1.98257584],
           [1.99104743],
           [2.02644187],
           [2.11507806],
           [2.0331019 ],
           [1.90941888],
           [2.02844259],
           [2.11879475],
           [1.87359322],
           [2.13403765],
           [2.27495915],
           [2.20061109],
           [2.06754622],
           [2.14488442],
           [2.50242006],
           [1.91823858],
           [2.28757758],
           [2.01539259],
           [2.2521258 ],
           [2.17555835],
           [2.41000996],
           [2.06169623],
           [2.13568683],
           [2.16020335],
           [2.2214577 ],
           [2.18803088],
           [2.0488424 ],
           [1.92389841],
           [2.1033012 ],
           [2.04088102]])

```python
def normal_pdf(x,mu,sigma): #defines the normal pdf

  return (1/(np.sqrt(2*np.pi*sigma**2)))*np.exp((-(x-mu)**2)/(2*sigma**2))
```

```python
x = np.linspace(-4,4,100)
```

```python
mu = 2.1
```

```python
sigma = 0.12
```

```python
y = normal_pdf(x,mu,sigma)
```

```python
plt.plot(x,y) #normal distribution curve centered around mu with a modifier of sigma
```

```python
S = sigma * np.random.randn(1,10) + mu
```

```python
normal_pdf(S,mu,sigma)
```

    array([[3.01202393, 2.1766703 , 1.78150436, 2.6597182 , 3.15139146,
            3.0922822 , 0.27392432, 1.2010006 , 1.19032295, 2.87027938]])

```python
np.prod(normal_pdf(S,mu,sigma))
```

    340.26544804025076

```python

```

```python
mu = 1
```

```python
sigma = 1
```

```python
plt.plot(x,y)
```

```python
S = sigma * np.random.randn(1,10) + mu
```

```python
normal_pdf(S,mu,sigma)
```

    array([[0.34452204, 0.39327053, 0.38180022, 0.39648104, 0.35918281,
            0.0659108 , 0.35551477, 0.22107085, 0.36164105, 0.22071873]])

```python
np.prod(normal_pdf(S,mu,sigma))
```

    3.0461071747888764e-06

```python
def normal_likelihood(S,mu,sigma):

  return np.prod(normal_pdf(S,mu,sigma))
```

```python
start = -5
stop = 5
step = 0.1

L = []

for m in np.arange(start,stop,step):

  L.append((m,normal_likelihood(S,m,sigma)))
```

```python
L = np.asarray(L)
```

```python
L.shape
```

    (100, 2)

```python
plt.plot(L[:,0],L[:,1])
```

```python
np.argmax(L[:,1])
```

    56

```python
L[71,0]
```

    2.0999999999999748

```python
mu = 1.123
sigma = 0.123
```

```python
S = sigma * np.random.randn(1,100) + mu
```

```python
mu = np.linspace(-4,4,1000)
sigma = np.linspace(-4,4,1000)
```

```python
L = np.zeros ((mu.shape[0],sigma.shape[0]))
for i in range(mu.shape[0]):
  for j in range(sigma.shape[0]):
    L[i,j] = normal_likelihood(S,mu[i],sigma[j])
```

```python
def plot(x): #graphs the pdf
  fig, ax = plt.subplots()
  im = ax.imshow(x,cmap=plt.get_cmap('cool'))
  plt.show
```

```python
plot(L)
```

```python
np.argmax(L)
```

    641483

```python
np.unravel_index(np.argmax(L), L.shape)
```

    (641, 483)

```python
L[639,485]
```

    5.345720989505384e+25

```python
mu[639]
```

    1.1171171171171173

```python
sigma[485]
```

    -0.11611611611611616

```python
np.mean(x)
```

    3.2862601528904633e-16


# Lambda School Data Science Module 142
## Sampling, Confidence Intervals, and Hypothesis Testing

## Prepare - examine other available hypothesis tests

If you had to pick a single hypothesis test in your toolbox, t-test would probably be the best choice - but the good news is you don't have to pick just one! Here's some of the others to be aware of:

```
import numpy as np
from scipy.stats import chisquare  # One-way chi square test

# Chi square can take any crosstab/table and test the independence of rows/cols
# The null hypothesis is that the rows/cols are independent -> low chi square
# The alternative is that there is a dependence -> high chi square
# Be aware! Chi square does *not* tell you direction/causation

ind_obs = np.array([[1, 1], [2, 2]]).T
print(ind_obs)
print(chisquare(ind_obs, axis=None))

dep_obs = np.array([[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]).T
print(dep_obs)
print(chisquare(dep_obs, axis=None))
```

    [[1 2]
     [1 2]]
    Power_divergenceResult(statistic=0.6666666666666666, pvalue=0.8810148425137847)
    [[16 32]
     [18 24]
     [16 16]
     [14 28]
     [12 20]
     [12 24]]
    Power_divergenceResult(statistic=23.31034482758621, pvalue=0.015975692534127565)

```
# Distribution tests:
# We often assume that something is normal, but it can be important to *check*

# For example, later on with predictive modeling, a typical assumption is that
# residuals (prediction errors) are normal - checking is a good diagnostic

from scipy.stats import normaltest
# Poisson models arrival times and is related to the binomial (coinflip)
sample = np.random.poisson(5, 1000)
print(normaltest(sample))  # Pretty clearly not normal
```

    NormaltestResult(statistic=26.720219361393852, pvalue=1.5768053617771425e-06)

```
# Kruskal-Wallis H-test - compare the median rank between 2+ groups
# Can be applied to ranking decisions/outcomes/recommendations
# The underlying math comes from chi-square distribution, and is best for n>5
from scipy.stats import kruskal

x1 = [1, 3, 5, 7, 9]
y1 = [2, 4, 6, 8, 10]
print(kruskal(x1, y1))  # x1 is a little better, but not "significantly" so

x2 = [1, 1, 1]
y2 = [2, 2, 2]
z = [2, 2]  # Hey, a third group, and of different size!
print(kruskal(x2, y2, z))  # x clearly dominates
```

    KruskalResult(statistic=0.2727272727272734, pvalue=0.6015081344405895)
    KruskalResult(statistic=7.0, pvalue=0.0301973834223185)

And there's many more! `scipy.stats` is fairly comprehensive, though there are even more available if you delve into the extended world of statistics packages. As tests get increasingly obscure and specialized, the importance of knowing them by heart becomes small - but being able to look them up and figure them out when they *are* relevant is still important.

## Live Lecture - let's explore some more of scipy.stats

Candidate topics to explore:

- `scipy.stats.chi2` - the Chi-squared distribution, which we can use to reproduce the Chi-squared test
- Calculate the Chi-Squared test statistic "by hand" (with code), and feed it into `chi2`
- Build a confidence interval with `stats.t.ppf`, the t-distribution percentile point function (the inverse of the CDF) - we can write a function to return a tuple of `(mean, lower bound, upper bound)` that you can then use for the assignment (visualizing confidence intervals)

```
import pandas as pd
import numpy as np
from scipy import stats

df = pd.read_csv('https://raw.githubusercontent.com/ryanleeallred/datasets/master/adult.csv', na_values=" ?")
print(df.shape)
df.head(20)
```

    (32561, 15)

      age
      workclass
      fnlwgt
      education
      education-num
      marital-status
      occupation
      relationship
      race
      sex
      capital-gain
      capital-loss
      hours-per-week
      country
      salary

      0
      39
      State-gov
      77516
      Bachelors
      13
      Never-married
      Adm-clerical
      Not-in-family
      White
      Male
      2174
      0
      40
      United-States
      <=50K

      1
      50
      Self-emp-not-inc
      83311
      Bachelors
      13
      Married-civ-spouse
      Exec-managerial
      Husband
      White
      Male
      0
      0
      13
      United-States
      <=50K

      2
      38
      Private
      215646
      HS-grad
      9
      Divorced
      Handlers-cleaners
      Not-in-family
      White
      Male
      0
      0
      40
      United-States
      <=50K

      3
      53
      Private
      234721
      11th
      7
      Married-civ-spouse
      Handlers-cleaners
      Husband
      Black
      Male
      0
      0
      40
      United-States
      <=50K

      4
      28
      Private
      338409
      Bachelors
      13
      Married-civ-spouse
      Prof-specialty
      Wife
      Black
      Female
      0
      0
      40
      Cuba
      <=50K

      5
      37
      Private
      284582
      Masters
      14
      Married-civ-spouse
      Exec-managerial
      Wife
      White
      Female
      0
      0
      40
      United-States
      <=50K

      6
      49
      Private
      160187
      9th
      5
      Married-spouse-absent
      Other-service
      Not-in-family
      Black
      Female
      0
      0
      16
      Jamaica
      <=50K

      7
      52
      Self-emp-not-inc
      209642
      HS-grad
      9
      Married-civ-spouse
      Exec-managerial
      Husband
      White
      Male
      0
      0
      45
      United-States
      >50K

      8
      31
      Private
      45781
      Masters
      14
      Never-married
      Prof-specialty
      Not-in-family
      White
      Female
      14084
      0
      50
      United-States
      >50K

      9
      42
      Private
      159449
      Bachelors
      13
      Married-civ-spouse
      Exec-managerial
      Husband
      White
      Male
      5178
      0
      40
      United-States
      >50K

      10
      37
      Private
      280464
      Some-college
      10
      Married-civ-spouse
      Exec-managerial
      Husband
      Black
      Male
      0
      0
      80
      United-States
      >50K

      11
      30
      State-gov
      141297
      Bachelors
      13
      Married-civ-spouse
      Prof-specialty
      Husband
      Asian-Pac-Islander
      Male
      0
      0
      40
      India
      >50K

      12
      23
      Private
      122272
      Bachelors
      13
      Never-married
      Adm-clerical
      Own-child
      White
      Female
      0
      0
      30
      United-States
      <=50K

      13
      32
      Private
      205019
      Assoc-acdm
      12
      Never-married
      Sales
      Not-in-family
      Black
      Male
      0
      0
      50
      United-States
      <=50K

      14
      40
      Private
      121772
      Assoc-voc
      11
      Married-civ-spouse
      Craft-repair
      Husband
      Asian-Pac-Islander
      Male
      0
      0
      40
      NaN
      >50K

      15
      34
      Private
      245487
      7th-8th
      4
      Married-civ-spouse
      Transport-moving
      Husband
      Amer-Indian-Eskimo
      Male
      0
      0
      45
      Mexico
      <=50K

      16
      25
      Self-emp-not-inc
      176756
      HS-grad
      9
      Never-married
      Farming-fishing
      Own-child
      White
      Male
      0
      0
      35
      United-States
      <=50K

      17
      32
      Private
      186824
      HS-grad
      9
      Never-married
      Machine-op-inspct
      Unmarried
      White
      Male
      0
      0
      40
      United-States
      <=50K

      18
      38
      Private
      28887
      11th
      7
      Married-civ-spouse
      Sales
      Husband
      White
      Male
      0
      0
      50
      United-States
      <=50K

      19
      43
      Self-emp-not-inc
      292175
      Masters
      14
      Divorced
      Exec-managerial
      Unmarried
      White
      Female
      0
      0
      45
      United-States
      >50K

```
df.isnull().sum()
```

    age                  0
    workclass         1836
    fnlwgt               0
    education            0
    education-num        0
    marital-status       0
    occupation        1843
    relationship         0
    race                 0
    sex                  0
    capital-gain         0
    capital-loss         0
    hours-per-week       0
    country            583
    salary               0
    dtype: int64

```
df.describe()
```

      age
      fnlwgt
      education-num
      capital-gain
      capital-loss
      hours-per-week

      count
      32561.000000
      3.256100e+04
      32561.000000
      32561.000000
      32561.000000
      32561.000000

      mean
      38.581647
      1.897784e+05
      10.080679
      1077.648844
      87.303830
      40.437456

      std
      13.640433
      1.055500e+05
      2.572720
      7385.292085
      402.960219
      12.347429

      min
      17.000000
      1.228500e+04
      1.000000
      0.000000
      0.000000
      1.000000

      25%
      28.000000
      1.178270e+05
      9.000000
      0.000000
      0.000000
      40.000000

      50%
      37.000000
      1.783560e+05
      10.000000
      0.000000
      0.000000
      40.000000

      75%
      48.000000
      2.370510e+05
      12.000000
      0.000000
      0.000000
      45.000000

      max
      90.000000
      1.484705e+06
      16.000000
      99999.000000
      4356.000000
      99.000000

```
df.describe(exclude='number')
```

      workclass
      education
      marital-status
      occupation
      relationship
      race
      sex
      country
      salary

      count
      30725
      32561
      32561
      30718
      32561
      32561
      32561
      31978
      32561

      unique
      8
      16
      7
      14
      6
      5
      2
      41
      2

      top
      Private
      HS-grad
      Married-civ-spouse
      Prof-specialty
      Husband
      White
      Male
      United-States
      <=50K

      freq
      22696
      10501
      14976
      4140
      13193
      27816
      21790
      29170
      24720

```
df['hours-per-week']
```

    0        40
    1        13
    2        40
    3        40
    4        40
    5        40
    6        16
    7        45
    8        50
    9        40
    10       80
    11       40
    12       30
    13       50
    14       40
    15       45
    16       35
    17       40
    18       50
    19       45
    20       60
    21       20
    22       40
    23       40
    24       40
    25       40
    26       40
    27       60
    28       80
    29       40
             ..
    32531    99
    32532    60
    32533    50
    32534    39
    32535    35
    32536    55
    32537    46
    32538    45
    32539    10
    32540    40
    32541    32
    32542    25
    32543    48
    32544    30
    32545    20
    32546    40
    32547    40
    32548    60
    32549    40
    32550    50
    32551    40
    32552    45
    32553    11
    32554    40
    32555    40
    32556    38
    32557    40
    32558    40
    32559    20
    32560    40
    Name: hours-per-week, Length: 32561, dtype: int64

```
cut_points = [0, 9, 19, 29, 39, 49, 1000]
label_names = ['0-9', '10-19', '20-29', '30-39', '40-49', '50+']
df['hours_per_week_categories'] = pd.cut(df['hours-per-week'], cut_points, labels=label_names)

df.head()
```

      age
      workclass
      fnlwgt
      education
      education-num
      marital-status
      occupation
      relationship
      race
      sex
      capital-gain
      capital-loss
      hours-per-week
      country
      salary
      hours_per_week_categories

      0
      39
      State-gov
      77516
      Bachelors
      13
      Never-married
      Adm-clerical
      Not-in-family
      White
      Male
      2174
      0
      40
      United-States
      <=50K
      40-49

      1
      50
      Self-emp-not-inc
      83311
      Bachelors
      13
      Married-civ-spouse
      Exec-managerial
      Husband
      White
      Male
      0
      0
      13
      United-States
      <=50K
      10-19

      2
      38
      Private
      215646
      HS-grad
      9
      Divorced
      Handlers-cleaners
      Not-in-family
      White
      Male
      0
      0
      40
      United-States
      <=50K
      40-49

      3
      53
      Private
      234721
      11th
      7
      Married-civ-spouse
      Handlers-cleaners
      Husband
      Black
      Male
      0
      0
      40
      United-States
      <=50K
      40-49

      4
      28
      Private
      338409
      Bachelors
      13
      Married-civ-spouse
      Prof-specialty
      Wife
      Black
      Female
      0
      0
      40
      Cuba
      <=50K
      40-49

```
df['sex'].iloc[0]
```

    ' Male'

```
df['hours_per_week_categories'].iloc[0]
```

    '40-49'

```
df['sex'].value_counts()
```

     Male      21790
     Female    10771
    Name: sex, dtype: int64

```
df['hours_per_week_categories'].value_counts()

```

    40-49    18336
    50+       6462
    30-39     3667
    20-29     2392
    10-19     1246
    0-9        458
    Name: hours_per_week_categories, dtype: int64

```
df = df.sort_values(by='hours_per_week_categories', ascending=True)

df.head()
```

      age
      workclass
      fnlwgt
      education
      education-num
      marital-status
      occupation
      relationship
      race
      sex
      capital-gain
      capital-loss
      hours-per-week
      country
      salary
      hours_per_week_categories

      31290
      55
      Self-emp-not-inc
      41938
      Bachelors
      13
      Married-civ-spouse
      Prof-specialty
      Wife
      White
      Female
      0
      0
      8
      United-States
      <=50K
      0-9

      5172
      32
      NaN
      134886
      HS-grad
      9
      Married-civ-spouse
      NaN
      Wife
      White
      Female
      0
      0
      2
      United-States
      >50K
      0-9

      22928
      17
      NaN
      332666
      10th
      6
      Never-married
      NaN
      Own-child
      White
      Female
      0
      0
      4
      United-States
      <=50K
      0-9

      7902
      35
      Private
      359131
      Bachelors
      13
      Married-civ-spouse
      Prof-specialty
      Wife
      White
      Female
      7298
      0
      8
      NaN
      >50K
      0-9

      6604
      41
      Private
      406603
      HS-grad
      9
      Never-married
      Other-service
      Not-in-family
      White
      Male
      0
      0
      6
      Iran
      <=50K
      0-9

```
contingency_table = pd.crosstab(df['sex'], df['hours_per_week_categories'], margins=True)

contingency_table
```

      hours_per_week_categories
      0-9
      10-19
      20-29
      30-39
      40-49
      50+
      All

      sex

      Female
      235
      671
      1287
      1914
      5636
      1028
      10771

      Male
      223
      575
      1105
      1753
      12700
      5434
      21790

      All
      458
      1246
      2392
      3667
      18336
      6462
      32561

```
femalecount = contingency_table.iloc[0][0:6].values
femalecount
```

    array([ 235,  671, 1287, 1914, 5636, 1028])

```
malecount = contingency_table.iloc[1][0:6].values
malecount
```

    array([  223,   575,  1105,  1753, 12700,  5434])

```
import matplotlib.pyplot as plt
import seaborn as sns

#Plots the bar chart
fig = plt.figure(figsize=(10, 5))
sns.set(font_scale=1.8)
categories = ["0-9","10-19","20-29","30-39","40-49","50+"]
p1 = plt.bar(categories, malecount, 0.55, color='#d62728')
p2 = plt.bar(categories, femalecount, 0.55, bottom=malecount)
plt.legend((p2[0], p1[0]), ('Male', 'Female'))
plt.xlabel('Hours per Week Worked')
plt.ylabel('Count')
plt.show()
```

### Expected Value Calculation
\begin{align}
expected_{i,j} =\frac{(row_{i} \text{total})(column_{j} \text{total}) }{(\text{total observations})}
\end{align}

```
# Get Row Sums
row_sums = contingency_table.iloc[0:2, 6].values
col_sums = contingency_table.iloc[2, 0:6].values

print(row_sums)
print(col_sums)
```

    [10771 21790]
    [  458  1246  2392  3667 18336  6462]

```
total = contingency_table.loc['All','All']
total
```

    32561

```
expected = []
for i in range(len(row_sums)):
  expected_row = []
  for column in col_sums:
    expected_val = column*row_sums[i]/total
    expected_row.append(expected_val)
  expected.append(expected_row)

print(np.array(expected))
```

    [[  151.50388502   412.16995793   791.26046497  1213.02346365
       6065.44811277  2137.59411566]
     [  306.49611498   833.83004207  1600.73953503  2453.97653635
      12270.55188723  4324.40588434]]

```
observed = pd.crosstab(df['sex'], df['hours_per_week_categories']).values
observed
```

    array([[  235,   671,  1287,  1914,  5636,  1028],
           [  223,   575,  1105,  1753, 12700,  5434]])

## Chi-Squared Statistic with Numpy

\begin{align}
\chi^2 = \sum \frac{(observed_{i}-expected_{i})^2}{(expected_{i})}
\end{align}

For the $observed$ values we will just use a version of our contingency table without the margins as a numpy array. In this way, if our observed values array and our expected values array are the same shape, then we can subtract them and divide them directly which makes the calculations a lot cleaner. No for loops!

```
chi_squared = ((observed - expected)**2/(expected)).sum()
print(f"Chi-Squared: {chi_squared}")
```

    Chi-Squared: 2287.190943926107

```
# Calculate Degrees of Freedom
dof = (len(row_sums)-1)*(len(col_sums)-1)
print(f"Degrees of Freedom: {dof}")
```

    Degrees of Freedom: 5

```
chi_squared, p_value, dof, expected = stats.chi2_contingency(observed)

print(f"Chi-Squared: {chi_squared}")
print(f"P-value: {p_value}")
print(f"Degrees of Freedom: {dof}")
print("Expected: \n", np.array(expected))
```

    Chi-Squared: 2287.190943926107
    P-value: 0.0
    Degrees of Freedom: 5
    Expected:
     [[  151.50388502   412.16995793   791.26046497  1213.02346365
       6065.44811277  2137.59411566]
     [  306.49611498   833.83004207  1600.73953503  2453.97653635
      12270.55188723  4324.40588434]]

# Confidence Interval Example

```
#confidence_interval = [lower_bound, upper_bound]

coinflips = np.random.binomial(n=1, p=.5, size=100)
print(coinflips)
```

    [1 1 0 0 1 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0
     0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 1
     0 1 1 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 0 1 1 0 0 1 1]

```
coinflips.mean()
```

    0.52

```
stats.ttest_1samp(coinflips, .5)
```

    Ttest_1sampResult(statistic=0.39831375340784614, pvalue=0.6912566363051549)

```
coinflips_100 = np.random.binomial(n=1, p=.5, size=100)
coinflips_1000 = np.random.binomial(n=1, p=.5, size=1000)

print("100 Coinflips Mean:", coinflips_100.mean())
print("1000 Coinflips Mean:", coinflips_1000.mean())

print("100 Coinflips Standard Deviation:", np.std(coinflips_100))
print("1000 Coinflips Standard Deviation:", np.std(coinflips_1000))
```

    100 Coinflips Mean: 0.57
    1000 Coinflips Mean: 0.488
    100 Coinflips Standard Deviation: 0.4950757517794625
    1000 Coinflips Standard Deviation: 0.49985597925802594

```
print("100 Coinflips Standard Error:", stats.sem(coinflips_100))
print("1000 Coinflips Standard :", stats.sem(coinflips_1000))
```

    100 Coinflips Standard Error: 0.0497569851956243
    1000 Coinflips Standard : 0.01581474331458169

```
0.4950757517794625/np.sqrt(100-1)
```

    0.0497569851956243

```
0.49985597925802594/np.sqrt(1000-1)
```

    0.015814743314581686

## Confidence Interval Equation

```
# Confidence intervals!
# Similar to hypothesis testing, but centered at sample mean
# Generally better than reporting the "point estimate" (sample mean)
# Why? Because point estimates aren't always perfect

import numpy as np
from scipy import stats

def confidence_interval(data, confidence=0.95):
  """
  Calculate a confidence interval around a sample mean for given data.
  Using t-distribution and two-tailed test, default 95% confidence.

  Arguments:
    data - iterable (list or numpy array) of sample observations
    confidence - level of confidence for the interval

  Returns:
    tuple of (mean, lower bound, upper bound)
  """
  data = np.array(data)
  mean = np.mean(data)
  n = len(data)
  stderr = stats.sem(data)
  interval = stderr * stats.t.ppf((1 + confidence) / 2.0, n - 1)
  return (mean, mean - interval, mean + interval)

def report_confidence_interval(confidence_interval):
  """
  Return a string with a pretty report of a confidence interval.

  Arguments:
    confidence_interval - tuple of (mean, lower bound, upper bound)

  Returns:
    None, but prints to screen the report
  """
  #print('Mean: {}'.format(confidence_interval[0]))
  #print('Lower bound: {}'.format(confidence_interval[1]))
  #print('Upper bound: {}'.format(confidence_interval[2]))
  s = "our mean lies in the interval [{:.5}, {:.5}]".format(
      confidence_interval[1], confidence_interval[2])
  return s
```

```
report_confidence_interval(confidence_interval(coinflips_100))
```

    'our mean lies in the interval [0.47, 0.67]'

```
report_confidence_interval(confidence_interval(coinflips_1000))
```

    'our mean lies in the interval [0.46, 0.52]'

```
stats.t.ppf(0.05, 5) # p-value, dof
```

    -2.0150483726691575

```
coinflips_1M = np.random.binomial(n=1, p=.5, size=1000000)
report_confidence_interval(confidence_interval(coinflips_1M))
```

    'our mean lies in the interval [0.49912, 0.50108]'

## Assignment - Build a confidence interval

A confidence interval refers to a neighborhood around some point estimate, the size of which is determined by the desired p-value. For instance, we might say that 52% of Americans prefer tacos to burritos, with a 95% confidence interval of +/- 5%.

52% (0.52) is the point estimate, and +/- 5% (the interval $[0.47, 0.57]$) is the confidence interval. "95% confidence" means a p-value $\leq 1 - 0.95 = 0.05$.

In this case, the confidence interval includes $0.5$ - which is the natural null hypothesis (that half of Americans prefer tacos and half burritos, thus there is no clear favorite). So in this case, we could use the confidence interval to report that we've failed to reject the null hypothesis.

But providing the full analysis with a confidence interval, including a graphical representation of it, can be a helpful and powerful way to tell your story. Done well, it is also more intuitive to a layperson than simply saying "fail to reject the null hypothesis" - it shows that in fact the data does *not* give a single clear result (the point estimate) but a whole range of possibilities.

How is a confidence interval built, and how should it be interpreted? It does *not* mean that 95% of the data lies in that interval - instead, the frequentist interpretation is "if we were to repeat this experiment 100 times, we would expect the average result to lie in this interval ~95 times."

For a 95% confidence interval and a normal(-ish) distribution, you can simply remember that +/-2 standard deviations contains 95% of the probability mass, and so the 95% confidence interval based on a given sample is centered at the mean (point estimate) and has a range of +/- 2 (or technically 1.96) standard deviations.

Different distributions/assumptions (90% confidence, 99% confidence) will require different math, but the overall process and interpretation (with a frequentist approach) will be the same.

Your assignment - using the data from the prior module ([congressional voting records](https://archive.ics.uci.edu/ml/datasets/Congressional+Voting+Records)):

1. Generate and numerically represent a confidence interval
2. Graphically (with a plot) represent the confidence interval
3. Interpret the confidence interval - what does it tell you about the data and its distribution?

Stretch goals:

1. Write a summary of your findings, mixing prose and math/code/results. *Note* - yes, this is by definition a political topic. It is challenging but important to keep your writing voice *neutral* and stick to the facts of the data. Data science often involves considering controversial issues, so it's important to be sensitive about them (especially if you want to publish).
2. Apply the techniques you learned today to your project data or other data of your choice, and write/discuss your findings here.
3. Refactor your code so it is elegant, readable, and can be easily run for all issues.

```
import pandas as pd

df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data', names = ['Class_Name',
'handicapped-infants',
'water-project-cost-sharing',
'adoption-of-the-budget-resolution',
'physician-fee-freeze',
'el-salvador-aid',
'religious-groups-in-schools',
'anti-satellite-test-ban',
'aid-to-nicaraguan-contras',
'mx-missile',
'immigration',
'synfuels-corporation-cutback',
'education-spending',
'superfund-right-to-sue',
'crime',
'duty-free-exports',
'export-administration-act-south-africa'])
print(df.shape)
df.head()
```

    (435, 17)

      Class_Name
      handicapped-infants
      water-project-cost-sharing
      adoption-of-the-budget-resolution
      physician-fee-freeze
      el-salvador-aid
      religious-groups-in-schools
      anti-satellite-test-ban
      aid-to-nicaraguan-contras
      mx-missile
      immigration
      synfuels-corporation-cutback
      education-spending
      superfund-right-to-sue
      crime
      duty-free-exports
      export-administration-act-south-africa

      0
      republican
      n
      y
      n
      y
      y
      y
      n
      n
      n
      y
      ?
      y
      y
      y
      n
      y

      1
      republican
      n
      y
      n
      y
      y
      y
      n
      n
      n
      n
      n
      y
      y
      y
      n
      ?

      2
      democrat
      ?
      y
      y
      ?
      y
      y
      n
      n
      n
      n
      y
      n
      y
      y
      n
      n

      3
      democrat
      n
      y
      y
      n
      ?
      y
      n
      n
      n
      n
      y
      n
      y
      n
      n
      y

      4
      democrat
      y
      y
      y
      n
      y
      y
      n
      n
      n
      n
      y
      ?
      y
      y
      y
      y

```
df = df.replace('?',0.5)
```

```
df = df.replace('n', 0)
```

```
df = df.replace('y', 1)

print(df.shape)
df.head()
```

    (435, 17)

      Class_Name
      handicapped-infants
      water-project-cost-sharing
      adoption-of-the-budget-resolution
      physician-fee-freeze
      el-salvador-aid
      religious-groups-in-schools
      anti-satellite-test-ban
      aid-to-nicaraguan-contras
      mx-missile
      immigration
      synfuels-corporation-cutback
      education-spending
      superfund-right-to-sue
      crime
      duty-free-exports
      export-administration-act-south-africa

      0
      republican
      0.0
      1.0
      0.0
      1.0
      1.0
      1.0
      0.0
      0.0
      0.0
      1.0
      0.5
      1.0
      1.0
      1.0
      0.0
      1.0

      1
      republican
      0.0
      1.0
      0.0
      1.0
      1.0
      1.0
      0.0
      0.0
      0.0
      0.0
      0.0
      1.0
      1.0
      1.0
      0.0
      0.5

      2
      democrat
      0.5
      1.0
      1.0
      0.5
      1.0
      1.0
      0.0
      0.0
      0.0
      0.0
      1.0
      0.0
      1.0
      1.0
      0.0
      0.0

      3
      democrat
      0.0
      1.0
      1.0
      0.0
      0.5
      1.0
      0.0
      0.0
      0.0
      0.0
      1.0
      0.0
      1.0
      0.0
      0.0
      1.0

      4
      democrat
      1.0
      1.0
      1.0
      0.0
      1.0
      1.0
      0.0
      0.0
      0.0
      0.0
      1.0
      0.5
      1.0
      1.0
      1.0
      1.0

```
rep = df[df['Class_Name'] == 'republican']

dem = df[df['Class_Name'] == 'democrat']
```

```
print("Democrat Support: ", dem['religious-groups-in-schools'].mean())
print("Republican Support: ", rep['religious-groups-in-schools'].mean())
```

    Democrat Support:  0.47752808988764045
    Republican Support:  0.8928571428571429

```
print(df['religious-groups-in-schools'].shape)
df['religious-groups-in-schools']
```

    (435,)

    0      1.0
    1      1.0
    2      1.0
    3      1.0
    4      1.0
    5      1.0
    6      1.0
    7      1.0
    8      1.0
    9      0.0
    10     0.0
    11     1.0
    12     0.0
    13     1.0
    14     1.0
    15     1.0
    16     1.0
    17     0.0
    18     1.0
    19     0.0
    20     0.5
    21     0.0
    22     0.0
    23     0.0
    24     0.0
    25     0.0
    26     0.0
    27     0.0
    28     0.0
    29     0.0
          ...
    405    1.0
    406    1.0
    407    1.0
    408    1.0
    409    1.0
    410    1.0
    411    1.0
    412    1.0
    413    1.0
    414    0.0
    415    1.0
    416    1.0
    417    0.0
    418    0.0
    419    0.0
    420    1.0
    421    1.0
    422    1.0
    423    1.0
    424    0.5
    425    0.0
    426    0.0
    427    1.0
    428    0.0
    429    0.0
    430    1.0
    431    0.0
    432    1.0
    433    1.0
    434    1.0
    Name: religious-groups-in-schools, Length: 435, dtype: float64

```
print(dem['religious-groups-in-schools'].shape)
dem['religious-groups-in-schools'].count()
```

    (267,)

    267

```
print(dem['religious-groups-in-schools'].count(None))
```

    267

```
print(rep['religious-groups-in-schools'].shape)
rep['religious-groups-in-schools'].count()
```

    (168,)

    168

```
import numpy as np
from scipy import stats
```

```
def confidence_interval(data, confidence=0.95)

data = np.array(data)
  mean = np.mean(data)
  n = len(data)
  stderr = stats.sem(data)
  interval = stderr * stats.t.ppf((1 + confidence) / 2.0, n - 1)
  return (mean, mean - interval, mean + interval)

def report_confidence_interval(confidence_interval):
  """
  Return a string with a pretty report of a confidence interval.

  Arguments:
    confidence_interval - tuple of (mean, lower bound, upper bound)

  Returns:
    None, but prints to screen the report
  """
  #print('Mean: {}'.format(confidence_interval[0]))
  #print('Lower bound: {}'.format(confidence_interval[1]))
  #print('Upper bound: {}'.format(confidence_interval[2]))
  s = "our mean lies in the interval [{:.5}, {:.5}]".format(
      confidence_interval[1], confidence_interval[2])
  return s
```

```
report_confidence_interval(confidence_interval(coinflips_100))
```

```
stats.t.ppf(0.05, 5) # p-value, dof
```

## Resources

- [Interactive visualize the Chi-Squared test](https://homepage.divms.uiowa.edu/~mbognar/applets/chisq.html)
- [Calculation of Chi-Squared test statistic](https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test)
- [Visualization of a confidence interval generated by R code](https://commons.wikimedia.org/wiki/File:Confidence-interval.svg)
- [Expected value of a squared standard normal](https://math.stackexchange.com/questions/264061/expected-value-calculation-for-squared-normal-distribution) (it's 1 - which is why the expected value of a Chi-Squared with $n$ degrees of freedom is $n$, as it's the sum of $n$ squared standard normals)


# Лабораторая работа 4.09
# Исследование поляризации света (стопа Столетова)

## Выполнил: Коняхин Всеволод Владимирович, M32051

## Краткие теоретические сведения
Поляризация света - это свойство света, проявляющееся в пространсвенно временной упорядоченности ориентации электрического и магнитного векторов процесса получения поляризованного света.

Свет, в котором направление колебаний светового вектора какимто образом упорядочено, называется поляризованным. Если в результате каких-то внешнший воздействий появляется преимущественное, но не единственное направление колебаний вектора 𝐸, то такой свет называют частично поляризованным.

Свет, в котором вектор 𝐸 колеблется только в одном выделенном направлении, перпендикулярном лучу, называется линейно поляризованным

Плоскость, проходящая через направление колебаний светового
вектора плоскополяризованной волны и направление распространения этой волны, называется плоскостью поляризации.

Естественный свет можно превратить в плоскополяризованый с помощью устройства, которое называют поляризатором.

#### Закон Малюса
$I = I_0 \cos^2 \phi$, где $\phi$ - угол между направлениями пропускания поляризатора и
направлением колебаний светового вектора, $I_0$ - интенсивность падающего на поляризатор света, $I$ -
интенсивность прошедшего через поляризатор света.

#### Формулы Френеля
Количественные соотношения между интенсивностями падающего, отраженного и прошедшего света можно получить из граничных условий для напряженностей электрического и магнитного полей на границе раздела двух диэлектриков. Эти соотношения были получены Френелем и носят название формул Френеля.

Коэффициент отражения $R = \frac{I_{refl}}{I_{fal}}$

Если учитывать, что интенсивность пропорциональна квадрату амплитуды, выразим коэффициенты отражения для продольной и поперечной составляющих:

$R^{\parallel} = \left( \frac{E_{refl}^{\parallel}}{E_{fal}^{\parallel}} \right)^2 = \frac{\tan^2 (\phi - \psi)}{\tan^2 (\phi + \psi)}$

$R^{\bot} = \left( \frac{E_{refl}^{\bot}}{E_{fal}^{\bot}} \right)^2 = \frac{\sin^2 (\phi - \psi)}{\sin^2 (\phi + \psi)}$

Степень поляризации света зависит от угла падения лучей и показателя преломления. Шотландский физик Д. Брюстер (1781-1868) установил закон, согласно которому при угле падения $\phi_{br}$ (угол Брюстера), определяемом соотношением:

$\tan \phi_{br} = n_{21}$,

где $n_{21}$ - это показатель преломления второй среды относительно первой, отражённый луч является плоскополяризованным (содержит только колебания, перпендикулярные плоскости падения).

Преломлённый же луч, при угле падения $\phi_{br}$ поляризуется максимально, но не полностью. Если свет падает на границу раздела под углом Брюстера, то отражённый и преломлённый лучи взаимно перпендикулярны.

$\tan \phi_{br} = \frac{\sin \phi_{br}}{\cos \phi_{br}}$; $\space \space n_{21} = \frac{\sin \phi_{br}}{\sin \psi}$

$\cos \phi_{br} = \sin \psi$

$\phi_{br} + \psi = \frac{\pi}{2}$

Степень поляризации преломленного света может быть значительно повышена многократным преломлением при условии падения света каждый раз на границу раздела под углом Брюстера.
Если, например, для стекла 𝑛 = 1, 53, степень поляризации преломленного луча составляет ≈ 15%, то после преломления на 8-10 наложенных друг на друга стеклянных пластинках, вышедший из
такой системы свет будет практически полностью поляризованным.
Такая совокупность пластинок называется стопой Столетова. Стопа может служить для анализа поляризованного света как при его
отражении, так и при его преломлении.

## Цель работы
Изучение поляризованного света и определение показателей преломления.

## Рабочие формулы и исходные данные

### Формулы
$I = I_0 \cos^2 \phi$

## Схема установки

#### Изучение закона Малюса

1 - Оптическая скамья, 2 - Источник света, 3 - Два рейтера с поляроидами, 4 - Рейтер с фоторезистором

#### Изучение поляризованного света с помощью стопы Столетова

1 - Оптическая скамья, 2 - Источник света, 3 - Рейтер с поляроидом, 4 - Стопа Столетова

```python
import sympy
import scipy
import numpy as np
import pandas as pd
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10,5)
%matplotlib inline
```

## Результаты измерений и расчеты

### Экспериментальные значения при изучение закона Малюса
$\alpha$ - градус угла поворота анализатора, $I$ - интенсивность света на анализаторе (фототок через фоторегестрирующее устройство)

```python
df1 = pd.DataFrame({
    'α, градусы': [i * 10 for i in range(19)],
    '$I$, мкА':
        [72.3, 71.3, 65.8, 60.3, 50.1, 39.4, 26.5,
         14.7, 6.5, 2.5, 4.7, 12.3, 24.5, 36.8, 47.4,
         56.8, 64.4, 69.1, 70.4]
})

df1['cos α'] = np.cos(df1['α, градусы'] * np.pi / 180)
df1['$\cos^2 α$'] = np.cos(df1['α, градусы'] * np.pi / 180) ** 2

df1
```

      α, градусы
      $I$, мкА
      cos α
      $\cos^2 α$

      0
      0
      72.3
      1.000000e+00
      1.000000e+00

      1
      10
      71.3
      9.848078e-01
      9.698463e-01

      2
      20
      65.8
      9.396926e-01
      8.830222e-01

      3
      30
      60.3
      8.660254e-01
      7.500000e-01

      4
      40
      50.1
      7.660444e-01
      5.868241e-01

      5
      50
      39.4
      6.427876e-01
      4.131759e-01

      6
      60
      26.5
      5.000000e-01
      2.500000e-01

      7
      70
      14.7
      3.420201e-01
      1.169778e-01

      8
      80
      6.5
      1.736482e-01
      3.015369e-02

      9
      90
      2.5
      6.123234e-17
      3.749399e-33

      10
      100
      4.7
      -1.736482e-01
      3.015369e-02

      11
      110
      12.3
      -3.420201e-01
      1.169778e-01

      12
      120
      24.5
      -5.000000e-01
      2.500000e-01

      13
      130
      36.8
      -6.427876e-01
      4.131759e-01

      14
      140
      47.4
      -7.660444e-01
      5.868241e-01

      15
      150
      56.8
      -8.660254e-01
      7.500000e-01

      16
      160
      64.4
      -9.396926e-01
      8.830222e-01

      17
      170
      69.1
      -9.848078e-01
      9.698463e-01

      18
      180
      70.4
      -1.000000e+00
      1.000000e+00

### Построение графика фототока $I$ от $\cos^2 \alpha$

```python
fig, ax = plt.subplots(figsize=(20, 5))
ax.set_title('Зависимость фототока $I$ от $\cos^2 α$')

ax.scatter(df1['$\cos^2 α$'], df1['$I$, мкА'], c='r')
ax.plot(df1['$\cos^2 α$'], df1['$I$, мкА'], 'r--')

plt.show()
```

### Экспериментальные значения при изучении поляризованного света с помощью стопы Столетова
$\alpha$ - угол падения поляризованного луча света из поляризатора к нормали поверхности стопы Столетова,

$i_{refr}^{(p)}$ - интенсивность прошедшего $p$-поляризовнного света

$i_{refl}^{(p)}$ - интенсивность отраженного $p$-поляризовнного света

$i_{refr}^{(s)}$ - интенсивность прошедшего $s$-поляризовнного света

$i_{refl}^{(s)}$ - интенсивность отраженного $s$-поляризовнного света

```python
df2 = pd.DataFrame({
    'α, градусы': [i for i in range(5, 90, 5)],
    '$i_{refr}^{(p)}$, мкА': [
        2.75, 3.48, 3.32, 3.05, 2.68, 2.2, 1.7, 1.11, 0.58,
        5.01, 4.22, 0.61, 0.59, 0.65, 0.34, 0.18, 0.12],
    '$i_{refl}^{(p)}$, мкА': [
        3.4, 3.26, 3.34, 3.43, 3.11, 4.11, 4.58, 5.38, 6.35,
        1.4, 1.51, 6.3, 6.77, 6.8, 6.14, 6.17, 6.52],
    '$i_{refr}^{(s)}$, мкА': [
        3.32, 3.24, 3.37, 3.57, 3.83, 4.15, 4.35, 4.73, 5.1,
        5.56, 6.7, 7.13, 7.72, 7.33, 7.83, 7.62, 7.85],
    '$i_{refl}^{(s)}$, мкА': [
        2.83, 2.7, 2.53, 2.28, 2.01, 1.73, 1.71, 1.48, 1.11,
        0.91, 0.65, 0.51, 0.37, 0.33, 0.44, 0.17, 0.1],
})

df2
```

      α, градусы
      $i_{refr}^{(p)}$, мкА
      $i_{refl}^{(p)}$, мкА
      $i_{refr}^{(s)}$, мкА
      $i_{refl}^{(s)}$, мкА

      0
      5
      2.75
      3.40
      3.32
      2.83

      1
      10
      3.48
      3.26
      3.24
      2.70

      2
      15
      3.32
      3.34
      3.37
      2.53

      3
      20
      3.05
      3.43
      3.57
      2.28

      4
      25
      2.68
      3.11
      3.83
      2.01

      5
      30
      2.20
      4.11
      4.15
      1.73

      6
      35
      1.70
      4.58
      4.35
      1.71

      7
      40
      1.11
      5.38
      4.73
      1.48

      8
      45
      0.58
      6.35
      5.10
      1.11

      9
      50
      5.01
      1.40
      5.56
      0.91

      10
      55
      4.22
      1.51
      6.70
      0.65

      11
      60
      0.61
      6.30
      7.13
      0.51

      12
      65
      0.59
      6.77
      7.72
      0.37

      13
      70
      0.65
      6.80
      7.33
      0.33

      14
      75
      0.34
      6.14
      7.83
      0.44

      15
      80
      0.18
      6.17
      7.62
      0.17

      16
      85
      0.12
      6.52
      7.85
      0.10

### Построение графика зависимости фототоков $i_{refr}^{(p)}$,  $i_{refl}^{(p)}$,  $i_{refr}^{(s)}$, $i_{refl}^{(s)}$ от $\alpha$

```python
fig, ax = plt.subplots(figsize=(20, 5))
ax.set_title('Зависимость фототоков $i_{refr}^{(p)}$,  $i_{refl}^{(p)}$,  $i_{refr}^{(s)}$, $i_{refl}^{(s)}$ от α')

ax.scatter(df2['α, градусы'], df2['$i_{refl}^{(s)}$, мкА'], c='r')
ax.plot(df2['α, градусы'], df2['$i_{refl}^{(s)}$, мкА'], 'r--', label='$i_{refl}^{(s)}$')

ax.scatter(df2['α, градусы'], df2['$i_{refr}^{(s)}$, мкА'], c='b')
ax.plot(df2['α, градусы'], df2['$i_{refr}^{(s)}$, мкА'], 'b--', label='$i_{refr}^{(s)}$')

ax.scatter(df2['α, градусы'], df2['$i_{refr}^{(p)}$, мкА'], c='g')
ax.plot(df2['α, градусы'], df2['$i_{refr}^{(p)}$, мкА'], 'g--', label='$i_{refr}^{(p)}$')

ax.scatter(df2['α, градусы'], df2['$i_{refl}^{(p)}$, мкА'], c='c')
ax.plot(df2['α, градусы'], df2['$i_{refl}^{(p)}$, мкА'], 'c--', label='$i_{refl}^{(p)}$')

ax.legend()

plt.show()
```

По графику видно, что свет становится поляризованным в промежутке от 50 до 60 градусов.

Тогда $\phi_{br} = 55^{\circ}$, а значит $n = \tan \phi_{br} = \tan 55^{\circ} = 1.43$

## Выводы и анализ результатов работы
В ходе данной работы было проведено исследование поляризации света.

Первая часть данной лабораторной заключалась в проверки закона Малюса, при установке с двумя поляроидами, первый из которых служил поляризатором проходящего света, а второй анализатором. При вращении анализатора были получены 19 точек, с помощью которых можно проверить зависимость фототока $I$ через анализатор от $\cos^2 \alpha$. Поскольку итоговая зависимость получилась линейной с учетом погрешности, можно утверждать, что закон Малюса выполняется, а именно $I = I_0 \cos^2 \alpha$

Во второй части данной работы предлагалось изучить поляризованный свет с помощью стопы Столетова. Для всевозможных углов падения луча света из поляризатора к нормали поверхности стопы были измерены фототоки отраженного и прошедшего света для $s$ и $p$ компонент поляризованного света. По полученному графику был определен $\phi_{br}$, показатель преломления материала $n$. Значения получились равными $55^{\circ}$ градусов и $1.43$, соответственно


```python
# 그래프, 수학 기능 추가
# Add graph and math features
import pylab as py
import numpy as np
import numpy.linalg as nl
# 기호 연산 기능 추가
# Add symbolic operation capability
import sympy as sy

```

```python
sy.init_printing()

```

# 적용사례 : 오일러 베르누이 외팔보
Application Example : Euler-Bernoulli Cantilever

룽게-쿠타법은 다음과 같이 구현할 수 있다.
We can implement the Runge-Kutta method as follows.

```python
def rk4_step(f, x0, t0, t1):
    """
    One time step of Runge-Kutta method

    f: dx_dt function
    x0 : initial condition
    t0 : this step time
    t1 : next step time
    """
    delta_t = (t1 - t0)
    delta_t_half = delta_t * 0.5
    t_half = t0 + delta_t_half

    # Step 1
    s1 = f(x0, t0)

    # Step 2
    s2 = f(x0 + s1 * delta_t_half, t_half)

    # Step 3
    s3 = f(x0 + s2 * delta_t_half, t_half)

    # Step 4
    s4 = f(x0 + s3 * delta_t, t1)

    # Step 5
    s = (1.0 / 6.0) * (s1 + (s2 + s3) * 2 + s4)

    # Step 6
    x1 = x0 + s * delta_t

    return x1

```

```python
def rk4(f, t_array, x_0):
    time_list = [t_array[0]]
    result_list = [x_0]

    x_i = x_0

    for k, t_i in enumerate(t_array[:-1]):
        # time step
        x_i_plus_1 = rk4_step(f, x_i, t_i, t_array[k+1])

        time_list.append(t_array[k+1])
        result_list.append(x_i_plus_1)

        x_i = x_i_plus_1

    return time_list, result_list

```

## 오일러 베르누이 외팔보
Euler-Bernoulli Cantilever

Ref : Wikipedia contributors, 'Euler–Bernoulli beam theory', Wikipedia, The Free Encyclopedia, 19 November 2018, 22:12 UTC, https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Bernoulli_beam_theory&oldid=869647128 [accessed 24 November 2018]

외팔보는 한쪽 끝에서 처짐과 기울기가 0인 보 이다.
A cantilever is a beam with both zero deflection and rotation at one end.

오일러 베르누이 보 이론은 분포하중과 보의 처짐은 다음과 같은 관계를 가진다고 가정한다.

Euler-Bernoulli beam theory assumes that the deflection of a beam and the distributed load has following relationship.

$$
\frac{d^2}{dx^2} \left( EI \frac{d^2w(x)}{dx^2} \right) = q(x)
$$

여기서 $w(x)$와 $q(x)$는 각각 보의 위치 $x$에서의 $z$방향 처짐과 분포하중이다.

Here, $w(x)$ and $q$ are, respectively, $z$ directional deflection and distributed load at a location $x$ of the beam.

단순화 하기 위해 $EI$는 일정하다고 가정하자.

To simplify, let's assume $EI$ is constant.

$$
E[Nm^{-2}]I[m^4] \frac{d^4w(x)}{dx^4}[m^{-3}] = q(x)[Nm^{-1}]
$$

상태 변수 $\mathbb{r}(x)$ 를 다음과 같이 정해 보자.

Let state variable $\mathbb{r}(x)$ be as follows.

$$
\mathbb{r}(x)
=
\begin{pmatrix}
r_0\\
r_1\\
r_2\\
r_3\\
\end{pmatrix}
=
\begin{pmatrix}
w(x) \\
\frac{d}{dx}w(x) \\
\frac{d^2}{dx^2}w(x) \\
\frac{d^3}{dx^3}w(x) \\
\end{pmatrix}
=
\begin{pmatrix}
w(x) \\
\theta(x) \\
(EI)^{-1}M(x) \\
(EI)^{-1}V(x) \\
\end{pmatrix}
$$

여기서 $\theta(x), M(x), V(x)$ 는 각각 $x$ 에서의 기울기, 모멘트, 전단력이다.

Here, $\theta(x), M(x), V(x)$ are, repectively, slope, moment, and shear force at $x$.

미분해보자.

Let's differentiate.

$$
\frac{d}{dx}\mathbb{r}(x) = \begin{pmatrix}
\frac{d}{dx}w(x) \\
\frac{d^2}{dx^2}w(x) \\
\frac{d^3}{dx^3}w(x) \\
\frac{d^4}{dx^4}w(x) \\
\end{pmatrix}
=
\begin{pmatrix}
r_1\\
r_2\\
r_3\\
(EI)^{-1}q(x) \\
\end{pmatrix}
$$

행렬로 다시 써 보면 다음과 같다.

Let's rewrite using matrices.

$$
\frac{d}{dx}\mathbb{r}(x) =
\begin{bmatrix}
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & 1\\
0 & 0 & 0 & 0\\
\end{bmatrix}
\begin{pmatrix}
r_0\\
r_1\\
r_2\\
r_3 \\
\end{pmatrix}
+
\begin{pmatrix}
0\\
0\\
0\\
(EI)^{-1} \\
\end{pmatrix}
q(x)
$$

$x=0$ 지점에서의 초기조건을 생각해 보자.
Let's think about the initial conditions at $x=0$.

$$
\mathbb{r}(0)
=
\begin{pmatrix}
w(0)[m] \\
\frac{d}{dx}w(0)[rad]  \\
\frac{d^2}{dx^2}w(0)[m^{-1}] \\
\frac{d^3}{dx^3}w(0)[m^{-2}] \\
\end{pmatrix}
=
\begin{pmatrix}
w(0)[m] \\
\theta(0)[rad] \\
(EI)^{-1}[N^{-1}m^{-2}]M(0)[Nm] \\
(EI)^{-1}[N^{-1}m^{-2}]V(0)[N] \\
\end{pmatrix}
=
\begin{pmatrix}
0 \\
0 \\
(EI)^{-1}M(0) \\
(EI)^{-1}V(0) \\
\end{pmatrix}
$$

외팔보의 경우, $V(0)$ 는 $q(x)$ 를 $x=0$에서 $x=L$ 까지 적분한 힘과 평형을 이룰 것이다.

In case of a cantilever, $V(0)$ would be in equilibrium with the integration of $q(x)$ from $x=0$ to $x=L$.

$$
\begin{align}
V(0) &+ \int_{x=0}^{x=L}q(x)dx = 0 \\
V(0) &= - \int_{x=0}^{x=L}q(x)dx
\end{align}
$$

$M(0)$는 $q(x)$의 도심 $\bar{x}$에 $V(0)$가 작용할 경우 $x=0$에서의 모멘트이다.

$M(0)$ is the moment at $x=0$ when $V(0)$ is located at the centroid $\bar{x}$ of $q(x)$.

$$
M(0) = \bar{x}V(0)
$$

도심 $\bar{x}$ 는 다음과 같이 구할 수 있다.
We can find the cetroid $\bar{x}$ as follows.

$$
\bar{x} = \frac{1}{L}\int_{x=0}^{x=L}xq(x)dx
$$

모멘트 $M(0)$는 $x$ 에서의 전단력 $V(x)$ 가 $x=0$ 점에 작용하는 모멘트와 균형을 이룬다.

The moment $M(0)$ is in equilibrium with the moment by shear force $V(x)$ at $x$ on $x=0$ point.

$$
M(0) + \int_{x=0}^{x=L}xV(x)dx = 0 \\
M(0) + \int_{x=0}^{x=L}x\left(V_0+ \int_{\xi=0}^{\xi=x}q(\xi)d\xi  \right)dx = 0
$$

우선 초기조건을 찾아보자.
Let's first find the initial conditions.

(Pytel & Kiusalaas, Mechanics of Materials, 2nd Ed., Cengage Learning, 2013, Example 6.1.)

```python
w_N_m = 2000

def q_N_m(x):
    return w_N_m

```

```python
def find_init(r, x):
    return np.array([-q_N_m(x), x*(q_N_m(x))])

```

```python
L_m = 4

E_N_m2 = 200e9

I_mm4 = 113e6
I_m4 = I_mm4 * ((1e-3)**4)

EI_Nm2 = E_N_m2 * I_m4

one_over_EI_1_Nm2 = 1.0 / EI_Nm2

```

```python
x_m_array = np.linspace(0, L_m, 1000+1)

x, V_M = rk4(find_init, x_m_array, np.array([0, 0]))

V_0_N = V_M[-1][0]
M_0_Nm = V_M[-1][1]
[V_0_N, M_0_Nm]

```

구한 초기값을 확인해 보자.
Let's check the initial values that we just found.

```python
assert abs((-w_N_m * L_m)-V_0_N) < 1e-6
assert abs((0.5 * w_N_m * L_m**2)-M_0_Nm) < 1e-6

```

이제 처짐 곡선을 구해 보자.
Let's find out the deflection curve.

```python
mat_A = np.array(
    [
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
        [0, 0, 0, 0],
    ]
)

mat_B = np.array([0, 0, 0, one_over_EI_1_Nm2])

```

```python
def dr_dx(r, x):
    return mat_A @ r + mat_B * q_N_m(x)

```

```python
r0 = np.array(
    [
        0, 0, M_0_Nm*one_over_EI_1_Nm2, V_0_N*one_over_EI_1_Nm2
    ]
)

```

```python
x, r = rk4(dr_dx, x_m_array, r0)

r[-1]

```

```python
r_array = np.array(r).T
r_array.shape

```

### 엄밀해와 비교
Compare with exact solutions

#### 처짐
Deflection

처짐 $w(x)$의 이론해는 다음과 같다.
Exact solution of the deflection $w(x)$ is as follows.

$$
w(x) = \frac{w_0 x^2}{24EI}\left(6L^2 -4Lx + x^2 \right)
$$

```python
w_table_6_2 = (w_N_m * (x_m_array ** 2) / (24 * E_N_m2 * I_m4)) * \
    (6 * L_m ** 2 - 4 * L_m * x_m_array + x_m_array**2)
w_table_6_2[-1]

```

```python
py.plot(x, r_array[0, :], '.', label='numerical')
py.plot(x_m_array, w_table_6_2, label='exact')

py.xlabel('x[m]')
py.ylabel('w(x)[m]')

py.legend(loc=0)

py.grid(True)

```

상대오차

Relative error

```python
w_numerical_m = np.interp(x_m_array, x, r_array[0, :])
w_error = nl.norm(w_table_6_2 - w_numerical_m) / nl.norm(w_table_6_2)

assert 1e-7 > w_error, f"deflection error {w_error*100}% larger than expected"

```

####  전단력
Shear force

전단력의 이론해

Shear force (in theory)

```python
sf_N = V_0_N + w_N_m * x_m_array

```

전단력의 수치해
Numerical solution of the shear force

```python
sf_numerical_N = np.interp(x_m_array, x, r_array[3, :]) * EI_Nm2

```

```python
py.plot(x_m_array, sf_numerical_N, '.', label='numerical')
py.plot(x_m_array, sf_N, label='exact')
py.xlabel('x[m]')
py.ylabel('V[N]')

py.legend(loc=0)

py.grid(True)

```

```python
sf_error = nl.norm(sf_N - sf_numerical_N) / nl.norm(sf_N)

assert 1e-7 > sf_error, f"shear force error ({sf_error}) larger than expected"

```

#### 굽힘모멘트
Bending moment

굽힘모멘트의 이론해

Bending Moment (in theory)

```python
bm_Nm = M_0_Nm + V_0_N * x_m_array + 0.5 * w_N_m * x_m_array ** 2

```

굽힘모멘트의 수치해
Numerical solution of the bending moment

```python
bm_numerical_Nm = np.interp(x_m_array, x, r_array[2, :]) * EI_Nm2

```

```python
py.plot(x_m_array, bm_numerical_Nm, '.', label='numerical')
py.plot(x_m_array, bm_Nm, label='exact')

py.xlabel('x[m]')
py.ylabel('M[Nm]')

py.legend(loc=0)

py.grid(True)

```

```python
bm_error = nl.norm(bm_Nm - bm_numerical_Nm) / nl.norm(bm_Nm)

assert 1e-7 > bm_error, f"bending moment error {bm_error} larger than expected"

```

#### 기울기
Slope

기울기의 엄밀해

Exact solution of the slope

```python
theta_table_6_2 = (w_N_m  / (24 * E_N_m2 * I_m4)) * \
    (12 * L_m ** 2 * x_m_array - 12 * L_m * x_m_array ** 2 + 4 * x_m_array ** 3)
theta_table_6_2[-1]

```

기울기의 수치해

Numerical solution of the slope

```python
slope_numerical_radian = np.interp(x_m_array, x, r_array[1, :])

```

```python
py.plot(x_m_array, py.rad2deg(slope_numerical_radian), '.', label='numerical')
py.plot(x_m_array, py.rad2deg(theta_table_6_2), label='exact')

py.xlabel('x[m]')
py.ylabel('$\\theta$(deg)')

py.grid(True)

```

상대 오차 확인
Check the relative error of the slope

```python
theta_error = nl.norm(theta_table_6_2 - slope_numerical_radian) / nl.norm(theta_table_6_2)

assert 1e-7 > bm_error, f"slope moment error {theta_error} larger than expected"

```

ref : Leif Rune Hellevik, Numerical Methods for Engineers, 2018, [Online] Available : https://www.ntnu.no/wiki/download/attachments/69242130/main.pdf

## 도전 과제
Try This

외팔보 상의 분포하중 $q(x)$가 다음과 같을 때 질문에 답하시오:
When the distributed load is as follows, answer the question:
$$
q(x) = 2\left(1 - \frac{1}{L}x \right)
$$

따로 주어지지 않은 값은 위의 예의 값을 사용할 수 있음.
You may use values above if not given.

$x=0$ 에서의 전단력의 초기 조건을 구하시오.
Find the initial condition of the shear force at $x=0$.

$x=0$ 에서의 굽힘모멘트의 초기 조건을 구하시오.
Find the initial condition of the bending moment at $x=0$.

$x$ 에서의 처짐 곡선을 구하시오.
Find the deflection curve at $x$.

이론해와 비교해 보시오.
Compare with the exact solution.

$$
w(x)=\frac{w_0 x^2}{120 L \cdot EI}\left( 10L^3 - 10L^2x + 5Lx^2 - x^3 \right)
$$

## Final Bell
마지막 종

```python
# stackoverfow.com/a/24634221
import os
os.system("printf '\a'");

```

```python

```