Reputation: 1869
I was wondering if anybody could help me translate the following code from MatLab into Python. The equation is used for determining the 99% Confidence Interval of a truncated normal distribution.
function sigma = var_truncNormal( a, b, mu, sigma, data )
x1 = (a-mu)/sigma * normpdf( a, mu, sigma );
x2 = (b-mu)/sigma * normpdf( b, mu, sigma );
cx = normcdf( b, mu, sigma) - normcdf( a, mu, sigma );
yhat = var( data(data>(mu-3000)&data<(mu+3000)) );
sigma2 = yhat/((1+(x1-x2)/cx - ((x1-x2)/cx)^2));
sigma = sqrt( sigma2 );
return;
function ci99 = GetCI99( data )
mu = median( data );
sigma = std( data );
fprintf( 1, 'initial sigma = %.1f\n', sigma );
sigma = var_truncNormal( mu-3000, mu+3000, mu, sigma, data );
fprintf( 1, 'updated sigma = %.1f\n', sigma );
sigma = var_truncNormal( mu-3000, mu+3000, mu, sigma, data );
fprintf( 1, 'updated sigma = %.1f\n', sigma );
ci99 = 2*mu-norminv( 0.01, mu, sigma );
figure( 'visible', 'off' );
hist( data, 5000:200:20000 );
axis( [5000 35000 0 550] );
hold;
[n2, xx] = ksdensity( data, 'npoints', 100 );
plot( xx, n2*length(data)*200, 'r' );
hdl = plot( xx, normpdf( xx, mu, sigma )*length(data)*200, 'k' );
set( hdl, 'linewidth', 2 );
line( [ci99 ci99], [0 550] );
print( '-dpdf', 'testFigure' );
close;
return;
I would appreciate any help.
Upvotes: 0
Views: 1608
Reputation: 11
the correct formula is:
def normpdf_python(x, mu, sigma):
return 1/(sigma*np.sqrt(2*np.pi))*np.exp(-1*(x-mu)**2/ (2*sigma**2) )
you forgot the parentheses in (2*sigma**2)
btw: norm.pdf(x,loc,scale) results in the same
Upvotes: 1
Reputation: 7335
I think you will be very happy going from matlab to python/numpy. You just have to go through it line by line.
For example the first line of your function:
x1 = (a-mu)/sigma * normpdf( a, mu, sigma );
The normpdf in python is: 1/(sigma*sqrt(2*pi))exp(-1(a-mu)**2/2*sigma**2).
So we can define a little function in python:
a = numpy.array([3,4,5,2,2,2,1,3,3,3,3])
mu = 3.1
sigma = .7
def normpdf_python(x, mu, sigma):
return 1/(sigma*sqrt(2*pi))*exp(-1*(x-mu)**2/2*sigma**2)
x1 = (a-mu)/sigma*normpdf_python(a,mu,sigma)
Numpy/Scipy has a deep set of statistical packages, you should always check for pre-existing functions that do what you need. In this case http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy.stats.norm pdf(x, loc=0, scale=1) is close, but might not be enough as it is defined as:
norm.pdf(x) = exp(-x**2/2)/sqrt(2*pi)
Upvotes: 0