tristanz / OpenIRT (http://people.fas.harvard.edu/~tzajonc/openirt.html)

Bayesian and Maximum Likelihood Estimation of Item Response Theory (IRT) Models

Clone this repository (size: 589.9 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/tristanz/openirt
commit 37: 0d32afb3521b
parent 36: 4e35255dd116
branch: default
tags: tip
Fixed 10.5 OSX compile issue
Tristan Zajonc / tristanz
6 months ago
OpenIRT / src / openirt.h
r37:0d32afb3521b 697 loc 20.4 KB embed / history / annotate / raw /
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// OpenIRT parameter definitions
//
// Copyright (c) 2009 Tristan Zajonc
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "mcmc.h"
#include <limits>

/// Process command line and config file
void StartUp(int argc, char* argv[]) {

  // Load default mcmc_options from command line.
  DefaultStartUp(argc, argv);

  cout << "Loading config file..." << endl;

  // Load config file via SimpleIni library.
  CSimpleIniA ini(false, false, false);
  SI_Error rc = ini.LoadFile(mcmc_options.config_file.c_str());
  if (rc < 0) {
   cout << "Error loading config file:" << mcmc_options.config_file << endl;
   exit(1);
  }

  // Proccess ini file and put data into appropriate structures
  // Uses SimpleIni: http://code.jellycan.com/simpleini/
  //    Args: "section-name",  "key name", "default"
  
  CSimpleIniA::TNamesDepend keys;
  ini.GetAllKeys("model_options", keys);
  
  cout << "Loading model options..." << endl;
  model_options.sampler = ini.GetValue("model_options", "sampler");
  model_options.tune_theta = atof(ini.GetValue("model_options", "tune_theta"));
  model_options.tune_a = atof(ini.GetValue("model_options", "tune_a"));
  model_options.tune_b = atof(ini.GetValue("model_options", "tune_b"));
  model_options.tune_c = atof(ini.GetValue("model_options", "tune_c"));
  model_options.tune_d = atof(ini.GetValue("model_options", "tune_d"));
  
  cout << "Loading priors..." << endl;
  priors.a_mu = atof(ini.GetValue("priors", "a_mu"));
  priors.a_sigma = atof(ini.GetValue("priors", "a_sigma"));
  priors.b_mu = atof(ini.GetValue("priors", "b_mu"));
  priors.b_sigma = atof(ini.GetValue("priors", "b_sigma"));
  
  priors.c_alpha = atof(ini.GetValue("priors", "c_alpha"));
  priors.c_beta = atof(ini.GetValue("priors", "c_beta"));
  
  priors.aGPC_mu = atof(ini.GetValue("priors", "aGPC_mu"));
  priors.aGPC_sigma = atof(ini.GetValue("priors", "aGPC_sigma"));
  priors.bGPC_mu = atof(ini.GetValue("priors", "bGPC_mu"));
  priors.bGPC_sigma = atof(ini.GetValue("priors", "bGPC_sigma"));
  priors.dGPC_mu = atof(ini.GetValue("priors", "dGPC_mu"));
  priors.dGPC_sigma = atof(ini.GetValue("priors", "dGPC_sigma"));
  
  priors.mu_mu = atof(ini.GetValue("priors", "mu_mu"));
  priors.mu_sigma = atof(ini.GetValue("priors", "mu_sigma"));
  priors.sigma_alpha = atof(ini.GetValue("priors", "sigma_alpha"));
  priors.sigma_beta = atof(ini.GetValue("priors", "sigma_alpha"));
  
  cout << "Loading data..." << endl;
  matrix item_data(mcmc_options.test_file.c_str());
  items.id = item_data(_, 0);
  items.type = item_data(_, 1);
  items.num_categories = item_data(_, 2);

  items.a = item_data(_, 3);
  items.b = item_data(_, 4);
  items.c = item_data(_, 5);
  items.d = item_data(0, 6, items.id.rows() - 1, item_data.cols() - 1);
  items.num_items = items.id.rows();
  cout << "Items added: " << items.num_items << endl;

  matrix response_data(mcmc_options.response_file.c_str());
  responses.id = response_data(_, 0);
  responses.group = response_data(_, 1);
  responses.theta = response_data(_, 2);
  responses.x = response_data(0, 3, response_data.rows() - 1, response_data.cols() - 1);
  responses.num_responses = responses.x.rows();
  responses.num_items = responses.x.cols();
  responses.num_groups = max(responses.group);
  cout << "Responses added: " << responses.num_responses << endl;
  std::cout << "Max int limit: " << std::numeric_limits<int>::max() << std::endl;

	//   cout << "Items ID:" << endl << items.id << endl;
	//   cout << "Items Type:" << endl << items.type << endl;
	//   cout << "Items a:" << endl << items.a << endl;
	//   cout << "Items b:" << endl << items.b << endl;
	// cout << "Items c:" << endl << items.c << endl;

}

/// 2PL Item Response Functions
double Irf2PL(double theta, double a, double b) {
  return(1 / (1 + exp(- D * a * (theta - b))));
}

matrix Irf2PL(matrix &theta, double a, double b) {
  return(1 / (1 + exp(- D * a * (theta - b))));
}

double Irf3PL(double theta, double a, double b, double c) {
  return(c + (1 - c) / (1 + exp(- D * a * (theta - b))));
}

matrix Irf3PL(matrix &theta, double a, double b, double c) {
  return(c + (1-c) / (1 + exp(- D * a * (theta - b))));
}

/*
double IrfGPC(int k, double theta, double a, double b, matrix &d, int num_categories) {
  matrix numer(num_categories, 1, false);
  if(num_categories == 2) {
    d = 0.0;
  }
  numer[0] = a * (theta - b); // normalized d0 = 0, not in d.
  for (int i = 1; i < num_categories; ++i) {
      numer[i] = numer[i-1] + D * a * (theta - b + d[i-1]);
  }
  
  numer = exp(numer);
  double p = numer[k-1]/sum(numer);
  
  // Exponents of big numbers turn into NaN.  Fix major cases:
  if(isnan(p)) {
    // cerr << "UNSTABLE: GPC giving NaN due to out of bounds theta or b";
    if(k == 1 && theta > b) {
      return(0);
    } else if (k == 1 && theta < b) {
      return(1);
    } else if (k == num_categories &&  theta > b + d[num_categories - 2]) {
      return(1);
    } else if (k == num_categories && theta < b + d[num_categories - 2]) {
      return(0);
    } else {
      cerr << "FATAL: GPC out of range.";
      exit(1);
    }
  }
  // this can become zero.  need safe log.
  if(p < std::numeric_limits<double>::min()) {
    p = std::numeric_limits<double>::min();
  }
  return(p);
}

template <matrix_order O, matrix_style S>
matrix IrfGPC(const Matrix<int, O, S> &k, matrix &theta, double a, double b, matrix &d, int num_categories) {
  matrix p(theta.rows(),1,false);
  for(int i = 0; i < theta.rows(); ++i) {
    p[i] = IrfGPC(k[i], theta[i], a, b, d, num_categories);
  }
  return(p);
}
*/

// Item density function
double Density2PL(int x, double theta, double a, double b) {
  if(x==1) {
    return(Irf2PL(theta, a, b));
  } else {
    return(1-Irf2PL(theta, a, b));
  }
}

template <matrix_order O, matrix_style S>
matrix Density2PL(const Matrix<int, O, S> &x, matrix &theta, double a, double b) {
  int N = theta.rows();
  matrix p(N, 1, false);
  for(int i = 0; i < theta.rows(); ++i) {
    if(x[i]==1) {
      p[i] = Irf2PL(theta[i], a, b);
    } else {
      p[i] = 1 - Irf2PL(theta[i], a, b);
    }
  }
  return(p);
}

double Density3PL(int x, double theta, double a, double b, double c) {
  if(x==1) {
    return(Irf3PL(theta, a, b, c));
  } else {
    return(1-Irf3PL(theta, a, b, c));
  }
}

template <matrix_order O, matrix_style S>
matrix Density3PL(const Matrix<int, O, S> &x, matrix &theta, const double a, const double b, const double c) {
  int N = theta.rows();
  matrix p(N, 1, false);
  for(int i = 0; i < N; ++i) {
    if(x[i]==1) {
      p[i] = Irf3PL(theta[i], a, b, c);
    } else {
      p[i] = 1 - Irf3PL(theta[i], a, b, c);
    }
  }
  return(p);
}

/*
// the way the IRF is written is is the density
double DensityGPC(int x, double theta, double a, double b, matrix &d, int num_categories) {
  return(IrfGPC(x, theta, a, b, d, num_categories));
}

template <matrix_order O, matrix_style S>
matrix DensityGPC(const Matrix<int, O, S> &x, matrix &theta, double a, double b, matrix &d, int num_categories) {
  return(IrfGPC(x, theta, a, b, d, num_categories));
}
*/

// IRT simulation (augmentation) functions
int Random2PL(const double theta, const double a, const double b) {
  return(myrng.rbern(Irf2PL(theta, a, b)));
}

int Random3PL(const double theta, const double a, const double b, const double c) {
  return(myrng.rbern(Irf3PL(theta, a, b, c))); 
}

/*
int RandomGPC(double theta, double a, double b, matrix &d, int num_categories) {
  double u = myrng.runif();
  double p = 0;
  for(int k = 1; k < num_categories; ++k) {
    p += IrfGPC(k, theta, a, b, d, num_categories);
    if(u < p) {
      return(k);
    } 
  }
  return(num_categories);
}
*/

/// 2PL missing response parameter.
class Missing2PLParameter : public Parameter<int> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  Missing2PLParameter() : Parameter<int>() {}
  Missing2PLParameter(bool track, string name, int i, int j) : Parameter<int>(track, name), i_(i), j_(j) {}

  int RandomPosterior() {
    return Random2PL(responses.theta(i_), items.a(j_), items.b(j_));
  }
  
  /// Draw starting value randomly p = 0.5
  int StartingValue() {
    return myrng.rbern(0.5);
  }
  
  /// Save back to global location
  void Save(int new_value) {
    responses.x(i_,j_) = new_value;
  }
  
  /// Return value from global location.
  int Value() {
    return responses.x(i_,j_);
  }
private:
  int i_;
  int j_;
};


/// 3PL missing response parameter.
class Missing3PLParameter : public Parameter<int> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  Missing3PLParameter() : Parameter<int>() {}
  Missing3PLParameter(bool track, string name, int i, int j) : Parameter<int>(track, name), i_(i), j_(j) {}

  int RandomPosterior() {
    return Random3PL(responses.theta(i_), items.a(j_), items.b(j_), items.c(j_));
  }
  
  // Better option?
  int StartingValue() {
    return myrng.rbern(0.5);
  }
  
  /// Save back to global location
  void Save(int new_value) {
    responses.x(i_,j_) = new_value;
  }
  
  /// Return value from global location.
  int Value() {
    return responses.x(i_,j_);
  }
private:
  int i_;
  int j_;
};

/*
/// GPC missing response parameter.
class MissingGPCParameter : public Parameter<int> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  MissingGPCParameter() : Parameter<int>() {}
  MissingGPCParameter(bool track, string name, int i, int j) : Parameter<int>(track, name), i_(i), j_(j) {}

  int RandomPosterior() {
    return RandomGPC(responses.theta(i_), items.a(j_), items.b(j_), items.d(j_,_), items.num_categories(j_));
  }
  
  /// Draw starting value randomly p = 0.5 from 1/2.
  int StartingValue() {
    return myrng.rbern(0.5) + 1;
  }
  
  /// Save back to global location
  void Save(int new_value) {
    responses.x(i_,j_) = new_value;
  }
  
  /// Return value from global location.
  int Value() {
    return responses.x(i_,j_);
  }
private:
  int i_;
  int j_;
};
*/

/// 2PL a parameter
class A2PLParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  A2PLParameter() : Parameter<double>() {}
  A2PLParameter(bool track, string name, int j) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double a) {
    return sum(log(Density2PL(responses.x(_, j_), responses.theta, a, items.b(j_))))
      + log(dlnorm(a, priors.a_mu, priors.a_sigma));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rlnorm(priors.a_mu, priors.a_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.a(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.a(j_);
  }
private:
  int j_;
};

/// 2PL b parameter
class B2PLParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  B2PLParameter() : Parameter<double>() {}
  B2PLParameter(bool track, string name, int j) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double b) {
    return sum(log(Density2PL(responses.x(_, j_), responses.theta, items.a(j_), b)))
      + log(dnorm(b, priors.b_mu, priors.b_sigma));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rnorm(priors.b_mu, priors.b_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.b(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.b(j_);
  }
private:
  int j_;
};

class A3PLParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  A3PLParameter() : Parameter<double>() {}
  A3PLParameter(bool track, string name, int j) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double a) {
    return sum(log(Density3PL(responses.x(_, j_), responses.theta, a, items.b(j_), items.c(j_))))
      + log(dlnorm(a, priors.a_mu, priors.a_sigma));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rlnorm(priors.a_mu, priors.a_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.a(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.a(j_);
  }
private:
  int j_;
};

/// 2PL b parameter
class B3PLParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  B3PLParameter() : Parameter<double>() {}
  B3PLParameter(bool track, string name, int j) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double b) {
    return sum(log(Density3PL(responses.x(_, j_), responses.theta, items.a(j_), b, items.c(j_))))
      + log(dnorm(b, priors.b_mu, priors.b_sigma));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rnorm(priors.b_mu, priors.b_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.b(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.b(j_);
  }
private:
  int j_;
};


/// 2PL b parameter
class C3PLParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  C3PLParameter() : Parameter<double>() {}
  C3PLParameter(bool track, string name, int j) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double c) {
    return sum(log(Density3PL(responses.x(_, j_), responses.theta, items.a(j_), items.b(j_), c)))
      + log(dnorm(c, priors.c_alpha, priors.c_beta));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rbeta(priors.c_alpha, priors.c_beta);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.c(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.c(j_);
  }
private:
  int j_;
};

/*
/// GPC a parameter
class AGPCParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  AGPCParameter() : Parameter<double>() {}
  AGPCParameter(bool track, string name, int j, int category) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double a) {
    return sum(log(DensityGPC(responses.x(_, j_), responses.theta, a, items.b(j_), items.d(j_,_), items.num_categories(j_))))
      + log(dlnorm(a, priors.aGPC_mu, priors.aGPC_sigma));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rlnorm(priors.aGPC_mu, priors.aGPC_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.a(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.a(j_);
  }
private:
  int j_;
};

/// GPC b parameter
class BGPCParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  B2PLParameter() : Parameter<double>() {}
  B2PLParameter(bool track, string name, int j) : Parameter<double>(track, name), j_(j) {}

  double LogDensity(double b) {
    return sum(log(DensityGPC(responses.x(_, j_), responses.theta, items.a(j_), b, items.d(j_,_), items.num_categories(j_))))
      + log(dnorm(b, priors.bGPC_mu, priors.bGPC_sigma));
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rnorm(priors.bGPC_mu, priors.bGPC_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.b(j_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.b(j_);
  }
private:
  int j_;
};

/// GPC d parameter
class DGPCParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  DGPCParameter() : Parameter<double>() {}
  DGPCParameter(bool track, string name, int j, int d_index) : Parameter<double>(track, name), j_(j) d_index_(d_index) {}

  double LogDensity(double d) {
    matrix d_new = items.d(j_,_);
    d_new(d_index_) = d
    return sum(log(DensityGPC(responses.x(_, j_), responses.theta, items.a(j_), items.b(j_), d_new, items.num_categories(j_))))
      + log(dnorm(d, priors.dGPC_mu, priors.dGPC_sigma));
  }
  
  /// Draw starting from prior
  /// TODO: ordering?
  double StartingValue() {
    return myrng.rnorm(priors.dGPC_mu, priors.dGPC_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    items.d(j_, d_index_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return items.d(j_, d_index_);
  }
private:
  int j_;
  int d_index_;
};
*/

class AbilityParameter : public Parameter<double> {
public:
  /// Default constructors that call base constructors. (REQUIRED)
  AbilityParameter() : Parameter<double>() {}
  AbilityParameter(bool track, string name, int i) : Parameter<double>(track, name), i_(i) {}

  double LogDensity(double theta) {
		double ldens = 0;
		for(int j = 0; j < items.num_items; ++j) {
			if(items.type(j) == TYPE_2PL) {
				ldens += log(Density2PL(responses.x(i_, j), theta, items.a(j), items.b(j)));
			} else {
				ldens += log(Density3PL(responses.x(i_, j), theta, items.a(j), items.b(j), items.c(j)));
			}
		}
    ldens += log(dnorm(theta, priors.mu_mu, priors.mu_sigma));		
		return ldens;
  }
  
  /// Draw starting from prior
  double StartingValue() {
    return myrng.rnorm(priors.mu_mu, priors.mu_sigma);
  }
  
  /// Save back to global location
  void Save(double new_value) {
    responses.theta(i_) = new_value;
  }
  
  /// Return value from global location.
  double Value() {
    return responses.theta(i_);
  }
private:
  int i_;
};



/* Maximum likelihood estimates for theta functor.
 */
class DensityTheta {
public:
	DensityTheta(int i, int pv) : i_(i), pv_(pv) {}
	// Density
	double operator()(double theta) const {
		double ldens = 0;
		for(int j = 0; j < items.num_items; ++j) {
			if(responses.x(i_, j) != MISSING) {
				if(items.type(j) == TYPE_2PL) {
					ldens += log(Density2PL(responses.x(i_, j), theta, results.a_pv(j,pv_), results.b_pv(j,pv_)));
				} else {
					ldens += log(Density3PL(responses.x(i_, j), theta, results.a_pv(j,pv_), results.b_pv(j,pv_), results.c_pv(j,pv_)));
				}
			}
		}
		// Return negative log density so as to minimize.
		return -ldens;
	}
private:
	int i_;
	int pv_;
};

void MLETheta() {
	// Load raw response data without imputed
	cout << endl << "Calculating MLE ability estimates..." << endl;
  boost::progress_display show_progress(responses.num_responses);
	matrix response_data(mcmc_options.response_file.c_str());
  responses.x = response_data(0, 3, response_data.rows() - 1, response_data.cols() - 1);
  responses.theta = response_data(_, 2);

	matrix theta(5, 1, false);
	matrix theta_se(5, 1, false);
	double I;
	for(int i = 0; i < responses.num_responses; ++i) {
		if(approx_equal(responses.theta(i), MISSING)) {
			// Propagate parameter uncertainity using Rubin (1987) formula.
			double item_count = 0;
			for(int j = 0; j < items.num_items; ++j) {
				if(responses.x(i, j) != MISSING) {
					item_count++;
				}
			}
			for(int pv = 0; pv < 5; ++pv) {
				DensityTheta f(i, pv);
				// Note: Gridded olden section search to deal with multi modes.
				theta(pv) = GoldenSectionSearch<DensityTheta>(f, -5, 5, 0.001);
				// Standard errors based on inverse information matrix, calculated using Hessian.
				I = SecondDerivative<DensityTheta>(f, theta(pv), 0.00001) / item_count;
				theta_se(pv) = sqrt((1/I) * (1/item_count));
			}
			results.theta_mle(i) = mean(theta);
			// See Rubin (1987)
			matrix e = theta - mean(theta);
			results.theta_mle_se(i) = sqrt(mean(theta_se % theta_se) + 1.2 * (1/4) * sum(e % e));
	    ++show_progress;
		} else {
			results.theta_mle(i) = responses.theta(i);
			results.theta_mle_se(i) = 0;
		}
	}
}