Logit panel with Simulated Maximum Likelihood. Convergence question with fminunc
5 views (last 30 days)
Show older comments
I'm testing a very basic logit panel model. The setup is as follows: We observe a binary variable where i is individual and t is time. For simplicity two periods. It is assumed that the true parameters are and and ε is an independent logit error. It's assumed that is individual specific but the same accross time.
I coded this, the problem is that when passing the function thorugh fminunc, the algorithm runs only one or two iterations and returns exactly the same values I provide as initial parameters. I coded this in other languages, but in this case I cannot see the problem.
I've tried different algorithms, other initial values, etc. The info after fminunc is pretty much the same:
Iteration Func-count f(x) Step-size optimality
0 4 1386.49 2.42e+07
1 64 1386.25 6.57442e-15 2.62e+07
Local minimum possible.
fminunc stopped because the size of the current step is less than the value of the step size tolerance.
My exact code is:
clear;
%%to 2 and the algorithm to Mersenne Twister
rng(66,'twister');
% goal: estimate the panel logit
%% DGP
T = 2; % maximum number of periods per agent
N = 1000; % number of agents
x = randn(N, T); % covariates: observables
beta_aux = 2+randn(N, 1); % random slope; agent specific but same in time
beta_i = [beta_aux, beta_aux];
beta_0 = 1;
z = beta_0 + beta_i .* x; % Calculate linear predictors
p = 1 ./ (1 + exp(-z)); %Calculate choice probs for each individual at every time (because independence)
% Generate binary outcomes based on probabilities
y = binornd(1, p); % N x T matrix of binary outcomes
%%
options = optimset('Display', 'iter', 'MaxIter', 1000);
negLogLik=@(params) logit_loglik(params,x,y,N,T);
params_init=[0, 0, 0];
[params_hat, fval] = fminunc(negLogLik, params_init, options);
%% Functions
function loglik = logit_loglik(params, x, y,N,T)
%unpack params
beta_0=params(1);
mu_beta=params(2);
aux_sigma=params(3);
sum_py=zeros([N,T]);
sigma_beta=exp(aux_sigma); %To ensure sigma is greater than 0
%Montecarlo draws
M=10000;
%We first need to get the unconditional choice probabilities by integrating
%over values of beta_i
for m = 1:M
beta_aux=mu_beta+sigma_beta*randn(N, 1);
beta_i = [beta_aux,beta_aux];
z = beta_0 + beta_i .* x;
% Calculate probabilities for all individuals, time periods, and MC simulations
py = 1 ./ (1 + exp(-z));
sum_py=sum_py+py;
end
%approximated integral of the choice probability Pr(y=1) for each
%person/time/choice
integrated_py=sum_py/M;
integrated_PY=log(y.*(integrated_py) + (1 - y).* (1 - integrated_py));
%We sum first over time
Lt=sum(integrated_PY,2);
%Then over individuals
loglik=-sum(Lt);
end
0 Comments
Accepted Answer
Torsten
on 4 Mar 2024
Edited: Torsten
on 4 Mar 2024
Your objective function depends on random values. This is not allowed for deterministic optimization as done by "fminunc". Convergence of these solvers depends on the differentiability of the objective function that gets lost in this case.
To estimate distribution parameters by maximum likelihood estimate, use "mle":
3 Comments
Torsten
on 5 Mar 2024
Edited: Torsten
on 5 Mar 2024
I don't have the background knowledge to follow what you are trying to do.
For "mle", usually realizations of a random variable are known (thus one vector of values). You guess that these realizations follow a certain distribution (from which usually the pdf is known). "mle" then tries to fit the distribution parameters such that the maximum likelihood function attains its maximum.
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox 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!