# 1 Nonlinear Estimation with Fminunc

Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).

## 1.1 Nonlinear Estimation Inputs LHS and RHS

Estimate this equation: $$\omega_t =\left(\nu_0 +a_0 \right)+a_1 t-\log \left(1-\exp \left(a_0 +a_1 t\right)\right)+\nu_t$$. We have data from multiple $$t$$, and want to estimate the $$a_0$$, and $$a_1$$ coefficients mainly, but also $$\nu_0$$ is good as well. This is an estimation problem with 3 unknowns. $$t$$ ranges from

% LHS outcome variable
ar_w = [-1.7349,-1.5559,-1.4334,-1.3080,-1.1791,-1.0462,-0.9085,-0.7652,-0.6150,-0.4564,-0.2874,-0.1052,0.0943];
% RHS t variable
ar_t = [0,3,5,7,9,11,13,15,16,19,21,23,25];
% actual parameters, estimation checks if gets actual parameters back
ar_a = [-1.8974, 0.0500];

## 1.2 Prediction and MSE Equations

Objective function for prediction and mean-squared-error.

v_0 = 0.5;
obj_ar_predictions = @(a) (v_0 + a(1) + a(2).*ar_t - log(1 - exp(a(1) + a(2).*ar_t)));
obj_fl_mse = @(a) mean((obj_ar_predictions(a) - ar_w).^2);

Evaluate given ar_a vectors.

ar_predict_at_actual = obj_ar_predictions(ar_a);
fl_mse_at_actual = obj_fl_mse(ar_a);
mt_compare = [ar_w', ar_predict_at_actual'];
tb_compare = array2table(mt_compare);
tb_compare.Properties.VariableNames = {'lhs-outcomes', 'evaluate-rhs-at-actual-parameters'};
disp(tb_compare);

lhs-outcomes    evaluate-rhs-at-actual-parameters
____________    _________________________________

-1.7349                    -1.2349
-1.5559                     -1.056
-1.4334                   -0.93353
-1.308                   -0.80813
-1.1791                   -0.67928
-1.0462                   -0.54641
-0.9085                   -0.40877
-0.7652                   -0.26546
-0.615                   -0.19133
-0.4564                   0.043211
-0.2874                    0.21214
-0.1052                    0.39429
0.0943                    0.59369             

## 1.3 Unconstrained Nonlinear Estimation

Estimation to minimize mean-squared-error.

% Estimation options
options = optimset('display','on');
% Starting values
ar_a_init = [-10, -10];
% Optimize
[ar_a_opti, fl_fval] = fminunc(obj_fl_mse, ar_a_init, options);

Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.

<stopping criteria details>

Compare results.

mt_compare = [ar_a_opti', ar_a'];
tb_compare = array2table(mt_compare);
tb_compare.Properties.VariableNames = {'estimated-best-fit', 'actual-parameters'};
disp(tb_compare);

estimated-best-fit    actual-parameters
__________________    _________________

-2.3541              -1.8974
0.057095                 0.05