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.6199381169260247 [[ 8.87625493 2.80561191 3.391602 7.14932651 3.61808351 8.54780216 10.9760832 12.55578041 3.30559547 13.98346748] [ 2.80561191 0.88679947 1.07201957 2.25976336 1.14360598 2.70179437 3.46932688 3.96863851 1.04483457 4.41990268] [ 3.391602 1.07201957 1.29592539 2.73174557 1.38246359 3.26610075 4.19394283 4.79754246 1.26306244 5.34305928] [ 7.14932651 2.25976336 2.73174557 5.75838233 2.91416375 6.88477619 8.84062065 10.11297834 2.66247212 11.26290036] [ 3.61808351 1.14360598 1.38246359 2.91416375 1.47478057 3.48420165 4.47400238 5.11790868 1.34740615 5.69985355] [ 8.54780216 2.70179437 3.26610075 6.88477619 3.48420165 8.2315033 10.56992937 12.09117221 3.18327677 13.46602982] [10.9760832 3.46932688 4.19394283 8.84062065 4.47400238 10.56992937 13.57266138 15.5260627 4.08759 17.29149329] [12.55578041 3.96863851 4.79754246 10.11297834 5.11790868 12.09117221 15.5260627 17.76060096 4.67588315 19.78011544] [ 3.30559547 1.04483457 1.26306244 2.66247212 1.34740615 3.18327677 4.08759 4.67588315 1.23103285 5.20756638] [13.98346748 4.41990268 5.34305928 11.26290036 5.69985355 13.46602982 17.29149329 19.78011544 5.20756638 22.02926396]]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.