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.941866404575299 [[ 2.85091337 3.42078103 10.33711888 6.57154252 3.22094791 6.36117602 8.03761519 5.89565264 3.47387858 5.73484667] [ 3.42078103 4.10455924 12.40340043 7.88512489 3.86478158 7.63270833 9.64425009 7.07413172 4.16827044 6.88118231] [10.33711888 12.40340043 37.48133064 23.82773778 11.67882608 23.06497046 29.1435666 21.37703055 12.59592669 20.7939646 ] [ 6.57154252 7.88512489 23.82773778 15.14783702 7.42449643 14.66292841 18.52723079 13.58986647 8.00751823 13.21919813] [ 3.22094791 3.86478158 11.67882608 7.42449643 3.63901111 7.18682538 9.08085812 6.66087937 3.92477093 6.47920156] [ 6.36117602 7.63270833 23.06497046 14.66292841 7.18682538 14.19354258 17.93414191 13.15483121 7.75118364 12.79602861] [ 8.03761519 9.64425009 29.1435666 18.52723079 9.08085812 17.93414191 22.66054752 16.62168613 9.79394867 16.16832385] [ 5.89565264 7.07413172 21.37703055 13.58986647 6.66087937 13.15483121 16.62168613 12.19213479 7.18393678 11.85959007] [ 3.47387858 4.16827044 12.59592669 8.00751823 3.92477093 7.75118364 9.79394867 7.18393678 4.23297056 6.9879924 ] [ 5.73484667 6.88118231 20.7939646 13.21919813 6.47920156 12.79602861 16.16832385 11.85959007 6.9879924 11.53611562]]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.