Rewrite Matlab into Python: Try to fit parametric probability distributions - Equivalent functions in Python
6 views (last 30 days)
Show older comments
Has anyone an idea how to rewrite this Mathlab Distribution Fitting function into Python code?
%%file distribution_fitting.m
function pd = distribution_fitting(feature)
distnames =["Poisson", "Exponential", "Gamma", "ExtremeValue", "Kernel"];
values_must_be_positive =["Poisson", "Exponential", "Gamma"];
x=feature.';
x_values = linspace(min(x),max(x));
for dn=distnames
if min(x)<0 & ~isempty(find(strcmp(dn, values_must_be_positive)))
continue;
end
distname = char(dn);
pd = fitdist(x.',distname);
res1=kstest(x, 'CDF', pd);
res2=chi2gof(x, 'CDF', pd);
if (~res1 && ~res2)
fprintf('%s with 5%% significance level\r',distname);
cdfplot(x)
hold on
plot(x_values,cdf(pd,x_values),'r-')
plot(x_values,pdf(pd,x_values),'g-')
legend('Empirical CDF',[distname ' CDF'],[distname ' PDF'],'Location','best');
%title(['Empirical CDF and ', [distname ' CDF/PDF']);
hold off
return;
else
fprintf('Not %s with 5%% significance level\n',distname);
end
end
end
My approach starts with
i
mport scipy.stats
import scipy
from sklearn.preprocessing import StandardScaler
def distribution_fitting(feature):
distnames = ["Poisson", "Exponential", "Gamma", "ExtremeValue", "Kernel"]
values_must_be_positive = ["Poisson", "Exponential", "Gamma"]
x = feature
x_values = np.linspace(np.min(x), np.max(x))
for dn in distnames:
distname = getattr(scipy.stats, dn)
probDist = distname.fit(x.T)
res1= scipy.stats.kstest(x, dn, args=param)[1]
...
I'm not sure, if my way is right, because there are so many functions I couldn't find equivalent Python functions to them.
0 Comments
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!