我想知道是否有人可以帮助我将以下代码从 MatLab 翻译成 Python。该方程用于确定截断正态分布的 99% 置信区间。
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;
我将不胜感激任何帮助。