Effacer les filtres
Effacer les filtres

How to obtain a negative log-likelihood value for custom pdf similar to negloglik?

9 vues (au cours des 30 derniers jours)
How to obtain a negative log-likelihood value for custom pdf similar to negloglik?
I used the code below to aproximate a nlogpdf, now a want to get negative log-likelihood value similar to negloglik. This will be useful for compute the Akaike infrmation criterion.
% Fit that using log(alpha) to avoid problems with alpha <= 0
[p,ci] = mle(deg,'nlogpdf',@mylogpdf,'start',1);
alphahat = exp(p);
function logf = mylogpdf(x,logalpha,xmin)
alpha = exp(logalpha);
if nargin<3
xmin = min(x);
end
% f = ((alpha-1)/xmin) * (x/xmin).^-alpha;
logf = log(alpha-1) - log(xmin) - alpha.*log(x/xmin);
logf(x<xmin) = -Inf;
end

Réponses (1)

Abhishek Kumar Singh
Abhishek Kumar Singh le 26 Nov 2023
Hello Aldo,
I understand that you want to calculate negative log likelihood since ‘negloglik’ function doesn’t currently supports custom probability distribution function.
You can start by creating a custom probability distribution object that includes the necessary methods for calculating the negative log likelihood. Since you are using a power-law distribution, you've already implemented the logarithm of the probability density function (‘mylogpdf’).
Now, you need to create a custom probability distribution object and create a new custom ‘negloglik’ like function accordingly.
Here’s a reference code for you:
% Assuming you have alphahat and xmin from your fitting procedure
alphahat = exp(p);
xmin = %... assign the actual values
% Create a custom distribution object
customDistribution.logalpha = log(alphahat);
customDistribution.xmin = xmin;
% Specify censoring and frequency information (modify as needed)
cens = false(size(deg));
freq = ones(size(deg));
Then, you can use a function like defined below to work specifically with a custom PDF:
function nll = customnegloglik(pd, data, cens, freq)
% CUSTOMNEGLOGLIK Custom negative log likelihood for a power-law distribution.
% Extract data and information
x = data;
% Handle missing frequency and censoring information
if isempty(freq)
freq = ones(size(x));
end
if isempty(cens)
cens = false(size(x));
else
cens = logical(cens);
end
% Calculate negative log likelihood
nll = 0;
% Implement custom calculation based on the properties of pd
alpha = exp(pd.logalpha);
xmin = pd.xmin;
% Power-law distribution negative log likelihood calculation
nll = nll - sum(freq(~cens) .* (log(alpha-1) - log(xmin) - alpha.*log(x(~cens)/xmin)));
% Handle censoring and other calculations as needed
% (Note: You may need to modify this part based on your specific requirements)
if any(cens)
nll = nll - sum(freq(cens) .* log(1 - cdf_customdist(pd, x(cens))));
end
end
% Define a custom cumulative distribution function for censoring
function y = cdf_customdist(pd, x)
alpha = exp(pd.logalpha);
xmin = pd.xmin;
y = 1 - (x./xmin).^(-alpha+1);
y(x < xmin) = 0;
end
Further you can calculate the value using the function and display the results.
nll = customnegloglik(customDistribution, deg, cens, freq);
% Display results
disp(['Negative Log Likelihood: ', num2str(nll)]);
Please note that this is a sample code and you may need to modify it to work according to your specific requirements.
I hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by