Q550 Models in Cognitive Science, Prof. Kruschke

Homework: Hillclimbing Optimization

This homework uses four files for use with MATLAB. The four files are:

  1. ModelError.m. This is a function that takes two parameter values as input, and returns the error, i.e., the misfit between data and model predictions, as output.
  2. SimpleHillClimb.m. This is a simple hill-climbing routine for parameter optimization. It will be described in class.
  3. ModelErrorGraph.m. This program calls the optimization routines and then graphs the results.
  4. GridEvaluation.m. This is used for the graphing program.
Copy the files into your directory. (To do this, it is best to select all the text in your browser window, copy it into the paste buffer, then paste it into a blank M-file in MatLab, then save-as in MatLab.)

For all of the questions below, hand in (on the due date) the relevant print-outs from MATLAB, such as command-window output or graphical output or M-file code.

  1. Explore the behavior of the optimization algorithms. Run the ModelErrorGraph.m program with various values of parInit and parInc, as specified below. This is a script, not a function, so go to the file in the Editor window and press F5 to run it.

    The goal of this exercise is for you to notice that the best fit found by the program depends on (a) the initial values of the parameters, (b) the increments used in the search, and (c) the type of search algorithm used.

  2. Try a different model. Modify the ModelError.m file so that
    error = 10 * -cos( 2* (paramVals(1)^2+paramVals(2)^2)^0.5 ) ...
    * exp( -0.5 * ((paramVals(1)+1)^2+(paramVals(2)-1)^2)^0.5 ) + 5.1 ;
    (The "..." is part of the MatLab code; it means that the command continues on the next line.) The purpose of this exercise is to emphasize that the model program is separate from the fitting algorithm. Changing the function being optimized is like changing the model.

  3. Modify the program code to make it more elegant. As it is now, SimpleHillClimb.m has two copies of the code that increments and decrements parameters - one copy for each parameter. Modify the code so that there is only one increment-decrement section, embedded in a loop that is executed once for each parameter. The loop would probably look like this:
    for parIndex = 1 : length( parInit )
          % insert here: code for increment-decrement corresponding parameter
    end
    The purposes of this exercise are (a) to make sure you really understand what the hill-climbing program is doing, and (b) to get you to really start programming in MATLAB (even though loops are not MATLAB's strong suit).