137 KiB
137 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)2.4890704720929593 [[11.60990408 7.82544198 14.11622885 1.78979284 6.76376813 14.09060397 14.04624055 3.77253612 5.11082097 14.99821162] [ 7.82544198 5.27459502 9.51478402 1.20637689 4.55899331 9.49751204 9.4676097 2.54280848 3.44485473 10.10926826] [14.11622885 9.51478402 17.16361442 2.17617003 8.22391797 17.13245767 17.07851716 4.58694429 6.21413561 18.23599801] [ 1.78979284 1.20637689 2.17617003 0.27591601 1.04270833 2.17221967 2.16538057 0.58157742 0.78788857 2.31213725] [ 6.76376813 4.55899331 8.22391797 1.04270833 3.94047694 8.20898927 8.18314376 2.19782691 2.97749299 8.73774883] [14.09060397 9.49751204 17.13245767 2.17221967 8.20898927 17.10135749 17.04751489 4.57861771 6.20285522 18.20289459] [14.04624055 9.4676097 17.07851716 2.16538057 8.18314376 17.04751489 16.99384182 4.56420221 6.18332591 18.14558387] [ 3.77253612 2.54280848 4.58694429 0.58157742 2.19782691 4.57861771 4.56420221 1.2258524 1.66071628 4.87353683] [ 5.11082097 3.44485473 6.21413561 0.78788857 2.97749299 6.20285522 6.18332591 1.66071628 2.24984554 6.60239515] [14.99821162 10.10926826 18.23599801 2.31213725 8.73774883 18.20289459 18.14558387 4.87353683 6.60239515 19.37538418]]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.