Saturday, November 26, 2005

Nonlinear minimization in javascript

For certain experiments in psychology, especially those with tasks in which parameters are changed in response to the behavior of the subject ('adaptive testing'), it is useful to be able to minimize a function. DHTML is very useful for implementing such tasks (and an extemely cheap alternative for expensive experimentation software such as E-Prime), and so it is useful to have a javascript algorithm for function minimization. I found a useful C algorithm that was straightforward enough to translate into JavaScript (click the title of this post to download). It is an implementation of the Hooke and Jeeves algorithm (with improvements; read this for a conceptual explanation and comparison to other algorithms). Good things of this algorithm are: light weight and simple code, reasonably robust, multidimensional minimization, no gradients required, and open source. This javascript implementation is easy to use as the example below demonstrates:
  <script type=text/javascript src="hooke.js"><script/>
  <script>
  function g(x, n) // x contains the parameters, n is the # of params
  {
      c = -Math.exp(-(4-x[0])*(4-x[0])) + (x[1]*x[1]); 
      return (c);
  } 

  var console = document.getElementById("console")
  var a = new Hooke(g);  // new minimizer object
  a.startpt = [5, 1.9];  // starting point
  a.setConsole(console); // give a div element for diagnostic output
  a.main();              // start searching
  var result = a.endpt;  // array containing the coordinates found
  <script>
  <div id=console></div>

console

The full documentation is provided in the source.

Sunday, November 20, 2005

The EZ-Diffusion Model

We—that is, Eric-Jan Wagenmakers, Han van der Maas and me—have recently submited a paper which introduces the "EZ diffusion model" for two-choice reaction times, based on the diffusion model by Ratcliff (1978). To make application of the model easier we made a simple JavaScript program. Here's a batch-processing version. You only have to paste a table with the means, variances and proportions correct, and click a button to compute the EZ-diffusion parameters. The values are nicely formatted in a table so that you can copy easily copy and paste them back into your data analysis program. The original program can be found on E-J's site.

Update: An updated version of the EZ diffusion model, EZ2, is now available which also estimates the starting point/bias parameter. See EZ2

Saturday, November 12, 2005

Simple Questionnaire Wizard (SQW): example 2

1. Als ik 's ochtends wakker wordt heb ik altijd moeite om meteen op te staan.
  helemaal waar 1 2 3 4 5          helemaal onwaar
2. Als ik 's ochtends wakker wordt heb ik altijd honger.
  helemaal waar 1 2 3 4 5          helemaal onwaar
3. Als ik 's ochtends wakker wordt ga ik meteen douchen.
  helemaal waar 1 2 3 4 5          helemaal onwaar
4. Als ik 's ochtends wakker wordt wil ik graag weten wat voor weer het is.
  helemaal waar 1 2 3 4 5          helemaal onwaar
5. Als ik 's ochtends wakker wordt kijk ik als eerste op de klok.
  helemaal waar 1 2 3 4 5          helemaal onwaar