You are on page 1of 14

PH16MSCST11013

January 25, 2018

1 QUESTION 1
In [25]: import numpy as np
import matplotlib.pyplot as plt

In [26]: n=1000
x=np.linspace(0,1,n)
y=np.linspace(0,1,n)

In [27]: def f(x,y):


U=np.zeros((n,n))
for i in range(n):
for j in range(n):
U[i][j]=x[i]+y[j]
return U
P=f(x,y)
plt.plot(P)
plt.ylabel('P(xy)')
plt.title('JOINT PDF')
plt.show()

1
In [28]: Px= P.sum(0)/sum(sum(P))
plt.plot(Px)
plt.xlabel('x')
plt.ylabel('P(x)')
plt.title('JOINT PDF')
plt.show()

2
In [29]: Py=P.sum(1)/sum(sum(P))
plt.plot(Px)
plt.xlabel('Y')
plt.ylabel('P(Y)')
plt.title('JOINT PDF')
plt.show()

3
In [30]: a=int(.2*n)
Px1=P[:,a]#conditional probability
plt.plot(Px1)
plt.xlabel('x')
plt.ylabel('P(x|y=0.2)')
plt.title('CONDITIONAL PDF')
plt.show()

4
In [31]: b=int(.4*n)
Py1=P[:,b]#conditional probability
plt.plot(Py1)
plt.xlabel('x')
plt.ylabel('P(x|y=0.4)')
plt.title('CONDITIONAL PDF')
plt.show()

5
6
QUESTION_2

January 24, 2018

In [92]: import numpy as np


import matplotlib.pyplot as plt
from scipy.stats import norm
In [93]: mean= 1.5
stdvn= 0.5
x = np.linspace(-5, 5,1000)
d = norm(mean,stdvn)
plt.plot(x, d.pdf(x))
plt.xlabel('X')
plt.ylabel('Y')
plt.title('GAUSSIAN DISTRIBUTION')
plt.legend()
In [94]: plt.show()

1
In [95]: s = d.rvs(size = 1000)

In [96]: sum = 0
for i in range(len(s)):
sum += s[i]
s_mean = sum / len(s)
s_mean

Out[96]: 1.4863405509930319

In [97]: variance = 0
for i in range(len(s)):
variance += (s[i] - s_mean)*(s[i] - s_mean)
variance = variance / len(s)
variance

Out[97]: 0.24084781075156478

In [98]: stdvn =np.sqrt(variance)

In [99]: stdvn

Out[99]: 0.49076247895653635

In [100]: skew = 0
for i in range(len(s)):
skew += ((s[i] - s_mean)/stdvn)**3
skew = skew / len(s)
skew

Out[100]: 0.0057756341161426909

In [101]: kurt = 0
for i in range(len(s)):
kurt += ((s[i] - s_mean)/stdvn)**4
kurt = (kurt / len(s)) - 3
kurt

Out[101]: -0.17225372905982717

2
QUESTION_3

January 23, 2018

In [12]: import numpy as np


import matplotlib.pyplot as plt
from scipy.stats import norm
from scipy.stats import cauchy

In [26]: mu = 0
sigma = 1.5
x = np.arange(-10, 10, 0.001)
d=norm.pdf(x, mu, sigma)
plt.plot(x, d,'--',label='Normal')

Out[26]: [<matplotlib.lines.Line2D at 0xbd16b089b0>]

In [27]: mu1=0
gamma=1.5
x1 = np.arange(-10, 10, 0.001)
d1=cauchy.pdf(x1, mu1, gamma)
plt.plot(x, d1,'r-.',label='Cauchy')

Out[27]: [<matplotlib.lines.Line2D at 0xbd16a129e8>]

In [28]: plt.xlabel('X')
plt.ylabel('Y')
plt.title('DISTRIBUTION FUNCTIONS')
plt.legend()

Out[28]: <matplotlib.legend.Legend at 0xbd16a94b38>

In [29]: plt.show()

1
2
QUESTION 4

January 25, 2018

In [1]: import numpy as np

In [2]: lt=np.array([0.8920,0.881,0.8913,0.9837,0.8958])

In [3]: sigma=np.array([0.00044,0.009,0.00032,0.00048,0.00045])

In [4]: sigmasq=sigma**2

In [5]: sigmasq

Out[5]: array([ 1.93600000e-07, 8.10000000e-05, 1.02400000e-07,


2.30400000e-07, 2.02500000e-07])

In [6]: num=0
for i in range(len(lt)-1):
num+=lt[i]/sigmasq[i]

In [7]: num

Out[7]: 17591947.3722388

In [8]: den=0
for i in range(len(lt)-1):
den+=1/sigmasq[i]

In [9]: den

Out[9]: 19283537.712988466

In [10]: ltmean=num/den

In [11]: ltmean

Out[11]: 0.91227800801248771

In [12]: E=0
for i in range(len(lt)-1):
E+=(lt[i]-ltmean)**2
s=np.sqrt(E/(len(lt)-1))

1
In [13]: s

Out[13]: 0.041625380340064258

In [14]: unctymean=s/np.sqrt(len(lt))

In [15]: unctymean

Out[15]: 0.018615436005933399

In [16]: print('The weighted mean lifetime of K Meson is',ltmean,


'and uncertainty of the mean is ',unctymean)

The weighted mean lifetime of K Meson is 0.912278008012 and uncertainty of the mean is 0.01861

2
QUESTION 5

January 25, 2018

In [120]: import numpy as np


import matplotlib.pyplot as plt
from scipy import stats

In [121]: data= np.genfromtxt('ecc.txt' , delimiter=',')

In [122]: plt.hist(data)

Out[122]: (array([ 679., 263., 152., 88., 55., 36., 23., 5., 1., 1.]),
array([ 0. , 0.1196, 0.2392, 0.3588, 0.4784, 0.598 , 0.7176,
0.8372, 0.9568, 1.0764, 1.196 ]),
<a list of 10 Patch objects>)

In [123]: plt.xlabel('eccentricity')
plt.ylabel('dN')
plt.title('ECCENTRICITY DISTRIBUTION OF EXOPLANETS (HISTOGRAM BEFORE GAUSSIANISING)')
plt.legend()
plt.show()
plt.show()

1
In [124]: s=np.sort(data)
s
Out[124]: array([ 0. , 0. , 0. , ..., 0.93366, 0.97 , 1.196 ])
In [125]: t=np.trim_zeros(s)
In [126]: t
Out[126]: array([ 9.00000000e-04, 1.00000000e-03, 2.00000000e-03, ...,
9.33660000e-01, 9.70000000e-01, 1.19600000e+00])
In [127]: data1=stats.boxcox(t)
In [128]: plt.hist(data1)
Out[128]: ([array([ 10., 43., 95., 153., 203., 209., 175., 108., 55., 4.]),
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.])],
array([-3.30806736, -2.95227123, -2.59647509, -2.24067896, -1.88488283,
-1.5290867 , -1.17329056, -0.81749443, -0.4616983 , -0.10590217,
0.24989397]),
<a list of 2 Lists of Patches objects>)
In [129]: plt.xlabel('lambda e')
plt.ylabel('dN')
plt.title('HISTOGRAM AFTER GAUSSIANISING')
plt.legend()
plt.show()

You might also like