I'm trying find a solution to integrate Density of multivariate normal distribution.
I have 100 points dataset (x,y) and a covariance matrix (sigma) of these data
I have an idea to integrate density that I integrate each value of covariance matrix (x[i] to x[j]) and then sum all integrated values. Is it correct?
def gaussian(x, mu, sig):
return np.exp(-(x - mu)**2/ (2 * sig**2))
I = np.zeros(len(sigma), dtype=float)
for i in range(0, len(sigma)):
I[i] = quad(gaussian, x1[i] , x1[i+1] , args=(0, sigma[i]))[0]
sum(I)
Related
I am trying to learn gaussian fitting using scipy and least-squares in Python.
I have a further question relates the answer to this question How to fit a double Gaussian distribution in Python?,
From the code of the answer, how can I estimate the parameters c1, mu1, sigma1, c2, mu2, sigma2
in
params = [c1, mu1, sigma1, c2, mu2, sigma2], since I want to use leastsq?
Hope this answer will not be too late..
There's a simple way to estimate those parameters actually.
There it is how I do it in my own code for gaussian fitting on spectra :
#Détermination des paramètres initiaux
mu0, m = xdata[roiDeb], ydata[roiDeb]
for j in range(roiDeb+1, roiFin+1) :
if ydata[j] > m:
mu0 = xdata[j]
m = ydata[j]
h0 = m
fwhmd, fwhmf, sigma0 = ydata[roiDeb], ydata[roiFin+1], 0
for j in range(roiDeb, mu0+1) :
if ydata[j] > h0/2 :
fwhmd = j
break
for j in range(mu0, roiFin+1) :
if ydata[j] < h0/2 :
fwhmf = j
break
sigma0 = (fwhmf-fwhmd)/2.355
To determinate the centroid you can just do a if condition and check for the higher y value in your region of interest.
Then you can calculate the full width at half maximum (FWHM) at both sides.
To finish the formula : sigma = FWHM / 2.355 can be demonstrate simply (or can be find on internet)
I let you discover by yourself how to use those values to do a gaussian fit...
I need to create an nxn matrix in which the numbers in the cells are distributed following a Gaussian distribution.
This code may not go well because it fills a cell with a sequence.
how can I do?
mu, sigma = 8, 0.5 # mean and standard deviation
def KHead(nx, ny, mu, sigma):
KH0=np.zeros((nx,ny))
N=1000
for k in range(1,ny-1):
for i in range(0,nx-1):
KH0[(i,k)]= np.random.normal(mu, sigma, N )
return KH0
Edited for border of zeros
np.random.normal takes a size keyword argument.
You can use it like this:
KH0 = np.zeros((nx, ny))
KH0[1:-1,1:-1] = np.random.normal(mu, sigma, (nx -2, ny - 2))
I do not understand this homework question i have received,our task is to develop the Python function normdist(x,mu,sigma) , which evaluates the multivariate
Gaussian probability density function for the k dimensional vector x , the mean vector μ and the covariance
matrix Σ . In the special case where k = 1 , this function evaluates the univariate Gaussian probability
density function for the scalar x , the mean μ and the standard deviation σ .
My attempt is below:
def normcdf(x, mu, sigma):
t = x-mu;
y = 0.5*erfcc(-t/(sigma*sqrt(2.0)));
if y>1.0:
y = 1.0;
return y
def normpdf(x, mu, sigma):
u = (x-mu)/abs(sigma)
y = (1/(sqrt(2*pi)**k*abs(sigma)))*exp(-u*u/2)
return y
def normdist(x, mu, sigma, k):
if k:
y = normcdf(x,mu,sigma)
else:
y = normpdf(x,mu,sigma)
return y
Above code credited to Cerin
How do i handle the case of k =1 ?
Right now, I am using the Box-Muller method to generate 10 24 Gaussian random numbers in python. I am supposed to plot the power spectrum, and see a Gaussian curve. My code is below:
import numpy as np
import matplotlib.pyplot as plt
def fast_fourier_transform(y):
'''Return the fast Fourier transform of y.'''
Y = np.fft.fft(y)
f = np.fft.fftfreq(len(y),1.0/1024)
return f,Y
rlist=[]
for i in range((2**10)/2):
mu=0
sigma=1
u = np.random.random()
v = np.random.random()
z1 = np.sqrt(-2.0 * np.log(u)) * np.sin(2.0 * np.pi * v)
z2 = np.sqrt(-2.0 * np.log(u)) * np.cos(2.0 * np.pi * v)
x1 = mu + z1 * sigma
x2 = mu + z2 * sigma
rlist.append(x1)
rlist.append(x2)
print u, v, x1, x2
f,Y=fast_fourier_transform(rlist)
plt.plot(f,Y)
plt.show()
However, when I plot this, I don't get a Gaussian distribution. My question is this: why am I not getting a Gaussian distribution in my Gaussian-generated white noise power spectrum? Am I plotting something wrong? Thank you in advance.
To see the Gaussian curve, you want a histogram rather than a power spectrum. The power spectrum of independent random variables is uniform (flat). The term "white noise" is itself a big hint - white light is comprised of equal amounts of light at all frequencies.
I have a function, a gaussian, I have fitted this to my data from a data file. I now need to integrate the gaussian function to give the area under it.
This is my gaussian function
def I(theta,max_x,max_y,sigma):
return (max_y/(sigma*(math.sqrt(2*pi))))*np.exp(-((theta-max_x)**2)/(2*sigma**2))
COMPARING WITH GENERAL FORMULA
N(x | mu, sigma, n) := (n/(sigma*sqrt(2*pi))) * exp((-(x-mu)^2)/(2*sigma^2))
i.e n = max_y , MU = max_x , x = theta
this is what is given on another page:
If Phi(z) = integral(N(x|0,1,1), -inf, z); that is, Phi(z) is the integral of the standard normal distribution from >minus infinity up to z, then it's true by the definition of the error function that
Phi(z) = 0.5 + 0.5 * erf(z / sqrt(2)).
Likewise, if Phi(z | mu, sigma, n) = integral( N(x|sigma, mu, n),
-inf, z); that is, Phi(z | mu, sigma, n) is the integral of the normal distribution given parameters mu, sigma, and n from minus infinity up
to z, then it's true by the definition of the error function that
Phi(z | mu, sigma, n) = (n/2) * (1 + erf((x - mu) / (sigma *
sqrt(2)))).
I am unsure how this helps?? I just want to integrate my function over the plotted values under the curve. Is it saying this is the integral:
Phi(z | mu, sigma, n) = (n/2) * (1 + erf((x - mu) / (sigma * sqrt(2))))
The answer you have there is the indefinite integral. If you would like a numerical answer between two x limits, you can evaluate that function at two points and take the difference.
Your gaussian function is defined over all real numbers (−∞, +∞) but in practice, you are only interested in the middle part as the tails are very close to 0. To obtain a numerical estimate of the total area you can do as you say: evaluate the error function at two points suitably close to 0 on each side of the gaussian's peak and take the difference.
If Phi(z | mu, sigma, n) returns a function you could do:
integral = Phi(z | mu, sigma, n)
area = integral(X_HIGH) - integral(X_LOW)