# NSCS 344, Week 8

# Assignment: Least-squares model fitting

*** Due date: Start of class in Week 9 ***

## Part 1: Make a function to compute the mean squared error (3 points)

- Follow along with the notes to create a function that computes the mean squared error between the data and the model for a given value of σ

## Part 2: Plot the mean squared error as a function of sigma (3 points)

- Use your function to compute the error for a range of sigma values
- Plot the error as a function of sigma

## Part 3: Use fmincon to automatically find the minimum of the error function (4 points)

- Follow along with the notes to use fmincon to automatically find the minimum of the error function
- Add a star to your error plot at this minimum value

## Part 4: Use fmincon to fit a softmax function with two parameters (2 Extra Credit points)

Extend your model to include two parameters

Use fmincon to fit both σ (which determines β) and θ. To fit more than one parameter you will need to define your likelihood function to have two free parameters

function error = twoParameterSoftmax(rsk, EV_risky, EV_safe, sigma, theta)

and your function handle slightly differently too ...

fHandle = @(x) twoParameterSoftmax(rsk, EV_risky, EV_safe, x(1), x(2));

Then you'll need a vector of starting points, lower bounds and upper bounds, to give a starting point, lower bound and upper bound for each of the parameters (σ and θ)

LB = [0 -inf]; % lower bound 0 for sigma, -inf for theta

UB = [inf inf]; % upper bound inf for sigma, inf for theta

X0 = [1 0]; % initial condition 1 for sigma, 0 for theta

After all of this, you call fmincon in exactly the same way, but instead of giving you a single best fitting parameter, the output is a vector of best fitting parameters.