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)4.752993882123058 [[ 4.70339379 7.80064137 18.20965525 1.99515112 14.01360746 10.49675434 5.81431951 6.88529462 8.48275939 8.04252289] [ 7.80064137 12.93746782 30.20095624 3.30898476 23.24175499 17.40900715 9.64312649 11.41935301 14.06876966 13.33863154] [18.20965525 30.20095624 70.50048516 7.72442532 54.25507027 40.63922482 22.51071426 26.65710056 32.84184377 31.13742451] [ 1.99515112 3.30898476 7.72442532 0.84633101 5.9444873 4.45265953 2.46639907 2.92070022 3.59833509 3.41158944] [14.01360746 23.24175499 54.25507027 5.9444873 41.75308359 31.27473511 17.32357417 20.51450938 25.27410326 23.96243304] [10.49675434 17.40900715 40.63922482 4.45265953 31.27473511 23.42603161 12.97605223 15.36619075 18.93131756 17.94882393] [ 5.81431951 9.64312649 22.51071426 2.46639907 17.32357417 12.97605223 7.18764212 8.51157793 10.48635849 9.94213961] [ 6.88529462 11.41935301 26.65710056 2.92070022 20.51450938 15.36619075 8.51157793 10.07937759 12.41790507 11.77344318] [ 8.48275939 14.06876966 32.84184377 3.59833509 25.27410326 18.93131756 10.48635849 12.41790507 15.29899688 14.50501268] [ 8.04252289 13.33863154 31.13742451 3.41158944 23.96243304 17.94882393 9.94213961 11.77344318 14.50501268 13.75223451]]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.