138 KiB
138 KiB
In [1]:
%matplotlib inline
import numpy as np
from math import acos, exp, sqrt
from matplotlib import pyplot as plt
from matplotlib import rc, rcParams
import matplotlib.units as units
import matplotlib.ticker as ticker
rc('text',usetex=True)
rc('font',**{'family':'serif','serif':['Gaussian distribution']})
font = {'family' : 'serif',
'color' : 'darkred',
'weight' : 'normal',
'size' : 16,
}
pi = acos(-1.0)
mu0 = 0.0
sigma0 = 1.0
mu1= 1.0
sigma1 = 2.0
mu2 = 2.0
sigma2 = 4.0
x = np.linspace(-20.0, 20.0)
v0 = np.exp(-(x*x-2*x*mu0+mu0*mu0)/(2*sigma0*sigma0))/sqrt(2*pi*sigma0*sigma0)
v1 = np.exp(-(x*x-2*x*mu1+mu1*mu1)/(2*sigma1*sigma1))/sqrt(2*pi*sigma1*sigma1)
v2 = np.exp(-(x*x-2*x*mu2+mu2*mu2)/(2*sigma2*sigma2))/sqrt(2*pi*sigma2*sigma2)
plt.plot(x, v0, 'b-', x, v1, 'r-', x, v2, 'g-')
plt.title(r'{\bf Gaussian distributions}', fontsize=20)
plt.text(-19, 0.3, r'Parameters: $\mu = 0$, $\sigma = 1$', fontdict=font)
plt.text(-19, 0.18, r'Parameters: $\mu = 1$, $\sigma = 2$', fontdict=font)
plt.text(-19, 0.08, r'Parameters: $\mu = 2$, $\sigma = 4$', fontdict=font)
plt.xlabel(r'$x$',fontsize=20)
plt.ylabel(r'$p(x)$ [MeV]',fontsize=20)
# Tweak spacing to prevent clipping of ylabel
plt.subplots_adjust(left=0.15)
plt.savefig('gaussian.pdf', format='pdf')
plt.show()In [2]:
# Importing various packages
from math import exp, sqrt
from random import random, seed
import numpy as np
import matplotlib.pyplot as plt
def covariance(x, y, n):
sum = 0.0
mean_x = np.mean(x)
mean_y = np.mean(y)
for i in range(0, n):
sum += (x[(i)]-mean_x)*(y[i]-mean_y)
return sum/n
n = 10
x=np.random.normal(size=n)
y = 4+3*x+np.random.normal(size=n)
covxy = covariance(x,y,n)
print(covxy)
z = np.vstack((x, y))
c = np.cov(z.T)
print(c)4.323291478597321 [[4.06126507e+00 5.22717936e+00 3.58228342e-01 1.09103481e+01 4.42847770e+00 5.02161783e+00 8.06412177e-03 6.67407338e+00 1.12366979e+01 6.04205220e+00] [5.22717936e+00 6.72780613e+00 4.61069091e-01 1.40425078e+01 5.69981195e+00 6.46323168e+00 1.03791824e-02 8.59007674e+00 1.44625466e+01 7.77661393e+00] [3.58228342e-01 4.61069091e-01 3.15979239e-02 9.62359224e-01 3.90618734e-01 4.42937310e-01 7.11304709e-04 5.88693966e-01 9.91145266e-01 5.32945844e-01] [1.09103481e+01 1.40425078e+01 9.62359224e-01 2.93100040e+01 1.18968431e+01 1.34902789e+01 2.16637855e-02 1.79295029e+01 3.01867234e+01 1.62316154e+01] [4.42847770e+00 5.69981195e+00 3.90618734e-01 1.18968431e+01 4.82889306e+00 5.47566390e+00 8.79326583e-03 7.27753165e+00 1.22527008e+01 6.58836420e+00] [5.02161783e+00 6.46323168e+00 4.42937310e-01 1.34902789e+01 5.47566390e+00 6.20906175e+00 9.97101567e-03 8.25226753e+00 1.38937995e+01 7.47079457e+00] [8.06412177e-03 1.03791824e-02 7.11304709e-04 2.16637855e-02 8.79326583e-03 9.97101567e-03 1.60122668e-05 1.32521615e-02 2.23117916e-02 1.19972087e-02] [6.67407338e+00 8.59007674e+00 5.88693966e-01 1.79295029e+01 7.27753165e+00 8.25226753e+00 1.32521615e-02 1.09678277e+01 1.84658093e+01 9.92919670e+00] [1.12366979e+01 1.44625466e+01 9.91145266e-01 3.01867234e+01 1.22527008e+01 1.38937995e+01 2.23117916e-02 1.84658093e+01 3.10896672e+01 1.67171347e+01] [6.04205220e+00 7.77661393e+00 5.32945844e-01 1.62316154e+01 6.58836420e+00 7.47079457e+00 1.19972087e-02 9.92919670e+00 1.67171347e+01 8.98892195e+00]]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.