00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef ETIRM_ITEM_H_
00019 #define ETIRM_ITEM_H_
00020
00021 #ifdef ETIRM_NO_DIR_PREFIX
00022 #include "etirmtypes.h"
00023 #include "ItemParamPrior.h"
00024 #else
00025 #include "etirm/etirmtypes.h"
00026 #include "etirm/ItemParamPrior.h"
00027 #endif
00028
00029 #include <string>
00030 #include <iterator>
00031
00032 namespace etirm
00033 {
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 template<class L> class Item
00044 {
00045
00046 public:
00047
00048 typedef L latentvar_type;
00049
00050
00051 typedef RealVector::iterator param_iterator;
00052
00053
00054 typedef PriorVector::iterator prior_iterator;
00055
00056
00057 typedef RealVector param_vector;
00058
00059
00060 typedef PriorVector prior_vector;
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 Item(int nparam, int index, int nRespCat);
00071
00072
00073 virtual ~Item();
00074
00075
00076 RealVector GetParameters() const
00077 {
00078 return mParameterEstimates;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 virtual RealVector GetAllParameters() const
00092 {
00093 return GetParameters();
00094 }
00095
00096 void SetParameters(const RealVector ¶m);
00097
00098 #ifndef BOOST_MSVC6_MEMBER_TEMPLATES
00099 template<class I> void SetParameters(I begin, I end);
00100 #endif
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 virtual void SetAllParameters(const RealVector &allParam)
00111 {
00112 SetParameters(allParam);
00113 }
00114
00115
00116
00117
00118
00119
00120 template<class I> void SetAllParameters(I begin, I end)
00121 {
00122 RealVector allParam(begin, end);
00123 SetAllParameters(allParam);
00124 }
00125
00126 PriorVector::iterator PriorsIterator()
00127 {
00128 return mPriors.begin();
00129 }
00130
00131
00132 RealVector::iterator ParametersIterator()
00133 {
00134 return mParameterEstimates.begin();
00135 }
00136
00137
00138 void SetPriors(PriorVector &priors);
00139
00140
00141
00142 PriorVector GetPriors()
00143 {
00144 return mPriors;
00145 }
00146
00147 void DeletePriors();
00148
00149
00150 virtual int NonZeroPriors(RealVector &p) const;
00151
00152 int NumParameters() const
00153 {
00154 return mNumParameters;
00155 }
00156
00157
00158 int NumRespCat() const
00159 {
00160 return mNRespCat;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 bool ValidResponse(Response r) const
00174 {
00175 return ((r >= mFirstResponse) && (r <= LastResponse())) ? true : false;
00176 }
00177
00178
00179
00180
00181
00182
00183 bool CheckResponse(Response r) const
00184 {
00185 return (ValidResponse(r) || r == notPresentedResponse) ? true : false;
00186 }
00187
00188 virtual int ScaleParameters(Real slope, Real intercept, bool ignorePriors = false) = 0;
00189
00190
00191
00192
00193
00194
00195
00196 virtual Real ICRF(Response r, const RealVector ¶meters, const L &theta) const = 0;
00197
00198
00199 #ifndef BOOST_MSVC6_MEMBER_TEMPLATES
00200
00201
00202
00203
00204
00205
00206
00207
00208 template<class I> void ICRFAll(const RealVector ¶meters, const L &theta, I iprob) const;
00209
00210 #endif
00211
00212
00213
00214
00215
00216
00217 Real ProbResp(Response r, const L &theta) const
00218 {
00219 return ICRF(r, mParameterEstimates, theta);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 template<class I> void ProbRespAll(const L &theta, I iprob) const
00232 {
00233 ICRFAll<I>(mParameterEstimates, theta, iprob);
00234 }
00235
00236
00237
00238
00239
00240
00241 virtual Real NormalizingConstant() const
00242 {
00243 return 1.0;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 int ResponseIndex(const Response r) const
00255 {
00256 return r - mFirstResponse;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266 Response IndexResponse(const int index) const
00267 {
00268 return mFirstResponse + index;
00269 }
00270
00271
00272 int Index() const
00273 {
00274 return mIndex;
00275 }
00276
00277
00278
00279 virtual std::string ModelName() const = 0;
00280
00281
00282 virtual IRTModel Model() const = 0;
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 virtual void SetFirstResponse(Response r)
00293 {
00294 mFirstResponse = r;
00295
00296
00297 if (ValidResponse(notPresentedResponse))
00298 throw RuntimeError("Not presented response is in range of valid responses",
00299 "Item::SetFirstResponse");
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309 Response FirstResponse() const
00310 {
00311 return mFirstResponse;
00312 }
00313
00314 Response LastResponse() const
00315 {
00316 return mFirstResponse + NumRespCat() - 1;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326 virtual Response CorrectResponse()
00327 {
00328
00329 throw RuntimeError("No correct response for item", "Item::CorrectResponse()");
00330 return 0;
00331 }
00332
00333
00334
00335
00336
00337
00338 static Response NotPresentedResponse()
00339 {
00340 return notPresentedResponse;
00341 }
00342
00343
00344
00345
00346
00347
00348 static bool IsNoResponse(Response r)
00349 {
00350 return r == notPresentedResponse;
00351 }
00352
00353
00354
00355
00356
00357
00358 static Response DefaultFirstResponse()
00359 {
00360 return defaultFirstResponse;
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370 static void SetNotPresentedResponse(Response r)
00371 {
00372 notPresentedResponse = r;
00373 }
00374
00375 static void SetDefaultFirstResponse(Response r)
00376 {
00377 defaultFirstResponse = r;
00378 }
00379
00380
00381 protected:
00382
00383 int mIndex;
00384
00385
00386 int mNumParameters;
00387
00388
00389 int mNRespCat;
00390
00391
00392 RealVector mParameterEstimates;
00393
00394
00395 PriorVector mPriors;
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 Response mFirstResponse;
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 static Response notPresentedResponse;
00416
00417
00418 static Response defaultFirstResponse;
00419
00420
00421
00422 #ifdef BOOST_MSVC6_MEMBER_TEMPLATES
00423
00424
00425
00426 public:
00427 template <class I> void ICRFAll(const RealVector ¶meters, const L &theta, I iprob) const
00428 {
00429 Real sum = 0.0;
00430 int n = NumRespCat()-1;
00431
00432 for (int r = 0; r < n; ++r, ++iprob)
00433 {
00434 *iprob = ICRF(IndexResponse(r), parameters, theta);
00435 sum += *iprob;
00436 }
00437
00438 *iprob = 1.0 - sum;
00439
00440 }
00441
00442 template <class I> void Item<L>::SetParameters(I begin, I end)
00443 {
00444 typename std::iterator_traits<I>::difference_type n = end - begin;
00445 if (n != NumParameters())
00446 throw InvalidArgument("Wrong number of parameters", "Item::SetParameters");
00447
00448 param_iterator ip = mParameterEstimates.begin();
00449 while (begin != end)
00450 {
00451 *ip = *begin;
00452 ++begin;
00453 ++ip;
00454 }
00455
00456 }
00457
00458 #endif
00459
00460 };
00461
00462
00463 template<class L> Item<L>::Item(int nparam, int index, int nRespCat) :
00464 mIndex(index), mNumParameters(nparam), mNRespCat(nRespCat), mParameterEstimates(nparam, 0.0),
00465 mPriors(nparam, (ItemParamPrior *) 0), mFirstResponse(defaultFirstResponse)
00466 {
00467
00468
00469 if (ValidResponse(notPresentedResponse))
00470 throw RuntimeError("Not presented response is in range of valid responses",
00471 "Item::SetFirstResponse");
00472
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 #ifndef BOOST_MSVC6_MEMBER_TEMPLATES
00491 template<class L> template<class I> void Item<L>::ICRFAll(const RealVector ¶meters,
00492 const L &theta, I iprob) const
00493 {
00494 Real sum = 0.0;
00495 int n = NumRespCat()-1;
00496
00497 for (int r = 0; r < n; ++r, ++iprob)
00498 {
00499 *iprob = ICRF(IndexResponse(r), parameters, theta);
00500 sum += *iprob;
00501 }
00502
00503 *iprob = 1.0 - sum;
00504
00505 }
00506 #endif
00507
00508
00509 template<class L> Item<L>::~Item()
00510 {
00511 }
00512
00513
00514 template<class L> void Item<L>::SetParameters(const RealVector ¶m)
00515 {
00516 if (param.size() != NumParameters())
00517 throw InvalidArgument("Wrong number of parameters", "Item::SetParameters");
00518
00519 mParameterEstimates = param;
00520
00521 }
00522
00523
00524 #ifndef BOOST_MSVC6_MEMBER_TEMPLATES
00525 template<class L> template<class I> void Item<L>::SetParameters(I begin, I end)
00526 {
00527 typename std::iterator_traits<I>::difference_type n = end - begin;
00528 if (n != NumParameters())
00529 throw InvalidArgument("Wrong number of parameters", "Item::SetParameters");
00530
00531 param_iterator ip = mParameterEstimates.begin();
00532 while (begin != end)
00533 {
00534 *ip = *begin;
00535 ++begin;
00536 ++ip;
00537 }
00538
00539 }
00540 #endif
00541
00542 template<class L> void Item<L>::SetPriors(PriorVector &priors)
00543 {
00544 if ( (int)priors.size() != NumParameters())
00545 throw InvalidArgument("Wrong number of priors", "Item::SetPriors");
00546
00547 mPriors = priors;
00548
00549 }
00550
00551
00552 template<class L> void Item<L>::DeletePriors()
00553 {
00554
00555 int n = mPriors.size();
00556 PriorVector::iterator i = mPriors.begin();
00557
00558 while (n--)
00559 {
00560 if (*i != 0)
00561 {
00562 delete *i;
00563 *i = 0;
00564 }
00565 ++i;
00566 }
00567
00568 }
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580 template<class L> int Item<L>::NonZeroPriors(RealVector ¶m) const
00581 {
00582 int n = 0;
00583
00584
00585
00586 PriorVector::const_iterator iprior = mPriors.begin();
00587 RealVector::iterator iparam = param.begin();
00588 for (int i=NumParameters(); i--; ++iprior, ++iparam)
00589 {
00590 if (*iprior)
00591 {
00592 Real newparam = (*iprior)->NearestNonZero(*iparam);
00593 if (newparam != *iparam)
00594 {
00595 ++n;
00596 *iparam = newparam;
00597 }
00598 }
00599 }
00600
00601 return n;
00602
00603 }
00604
00605 }
00606
00607 #endif // ETIRM_ITEM_H_