Failure of initial objective function

I am getting the dreaded "Failure in initial objective function evaluation. FMINCON cannot continue." Notification.
I am trying to use fmincon, but also using a simple loop within the function to see how parameters change over time.
Here is my code:
```
%Define Function- fmincon%
opts = optimset ('Display','iter','TolX',2e-9,'TolFun',2e-9,'MaxIter',100000,'MaxFunEvals',10000000,'FunValCheck','on');
[param,fval,exitflag,output,lambda,grad,hessian] = fmincon(@(param) Posterior(data,idx,param),param0,A,b,Aeq,beq,lb,ub,nonlcon,opts);
err = sqrt(diag(inv(hessian)));
%Run posterior function that is solved for Ep1.5%
k = 1; %number of predictors
function LL = Posterior(data,idx,param)
%n = 13; %number of obs: simple case
n = max(data.AgeMinus1); %Will update to go by ID
% define components of the function:
%EpPrior0 = param(idx.EpPrior);
Tau0 =param(idx.Tau); %Prior Variance/precision
Sigma_sq0 = param(idx.Sigma_sq); %Variance of
ShockInfo0 =param(idx.ShockInfo); % n = how informative the observation was
% Define vector to hold the shocks:
Shock = zeros(n, 1);
% Define a vector to hold the precision of the prior:
Tau = zeros(n+1,1);
Tau(1,:) = Tau0(1,:); % Variance of prior going into period 1
for i=1:n % Start the sampling
Shock(i,:) = data.Ep2(i,:) + (data.Ep2(i,:)- data.Ep1(i,:)).*(Sigma_sq0(i,:)./(ShockInfo0(i,:).*Tau(i,:))); %Ep 1.5
Tau(i+1,:) = ((Sigma_sq0(i,:)./ShockInfo0(i,:)).*Tau(i,:))./(Tau(i,:) + (Sigma_sq0(i,:)./ShockInfo0(i,:)));
end
LL = -sum(log(normpdf(Shock, 0, Sigma_sq0)));
end
```
How can I fix this error?
Thank you in advance!

6 件のコメント

Chris
Chris 2022 年 11 月 15 日
Try calling
Posterior(data,idx,param0)
Do you get a valid answer?
Joshua Scott
Joshua Scott 2022 年 11 月 15 日
It doesn't recognize the function, which seems weird to me.
Matt J
Matt J 2022 年 11 月 15 日
Try calling it within your script, before the optimization is launched.
Joshua Scott
Joshua Scott 2022 年 11 月 15 日
I get "Unrecognized function or variable 'param'."
Which is interesting, since I have ran ML and fmincon while defining 'param0' without 'param'.
Should I define param using the idx structure?
Torsten
Torsten 2022 年 11 月 15 日
You should define "param0" in the same way as you extract the fitting parameters from "param" in "Posterior".
Joshua Scott
Joshua Scott 2022 年 11 月 15 日
編集済み: Joshua Scott 2022 年 11 月 15 日
Thank you! I did have two parameters swapped. Small progress!
Now I get the same error, but can call on the function at param0 and its not a number (NaN).

サインインしてコメントする。

回答 (1 件)

Torsten
Torsten 2022 年 11 月 15 日
移動済み: Matt J 2022 年 11 月 16 日

1 投票

Then set some outputs in the function "Posterior" to see where the NaN operation occurs.

1 件のコメント

Joshua Scott
Joshua Scott 2022 年 11 月 16 日
移動済み: Matt J 2022 年 11 月 16 日
There seemed to be an updating issue with the nested function. I changed param0 to vectors of ones to avoid issues. It runs! Now on to a new set of problems!
Thank you so much!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

リリース

R2020b

質問済み:

2022 年 11 月 14 日

編集済み:

2022 年 11 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by