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)0.9524796542295857 [[12.32978852 17.93351637 8.83074569 6.12730791 9.17878199 10.22862364 14.56202678 10.49110643 8.12113897 14.46581332] [17.93351637 26.08406534 12.84420428 8.91208933 13.35041853 14.87739949 21.18027777 15.25917728 11.81209057 21.04033655] [ 8.83074569 12.84420428 6.32468833 4.38845304 6.57395619 7.32586565 10.4295021 7.51385904 5.81645929 10.36059285] [ 6.12730791 8.91208933 4.38845304 3.04497536 4.56141023 5.0831307 7.23662224 5.21357193 4.03581286 7.18880881] [ 9.17878199 13.35041853 6.57395619 4.56141023 6.83304817 7.61459179 10.8405484 7.80999435 6.04569689 10.7689233 ] [10.22862364 14.87739949 7.32586565 5.0831307 7.61459179 8.48552604 12.08045792 8.70327816 6.73718563 12.00064055] [14.56202678 21.18027777 10.4295021 7.23662224 10.8405484 12.08045792 17.19839911 12.39046173 9.59142511 17.08476676] [10.49110643 15.25917728 7.51385904 5.21357193 7.80999435 8.70327816 12.39046173 8.92661816 6.91007256 12.30859612] [ 8.12113897 11.81209057 5.81645929 4.03581286 6.04569689 6.73718563 9.59142511 6.91007256 5.3490697 9.52805315] [14.46581332 21.04033655 10.36059285 7.18880881 10.7689233 12.00064055 17.08476676 12.30859612 9.52805315 16.97188519]]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.