title: Matlab to Python Stat Equation
body: <p>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.</p>
  
  <pre><code>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&gt;(mu-3000)&amp;data&lt;(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;
  </code></pre>
  
  <p>I would appreciate any help.</p>