C:/programs/etirm/src/SimulateResponses.h

Go to the documentation of this file.
00001 /*! \file SimulateResponses.h
00002  
00003   \brief 
00004   Simulate responses to items.
00005 
00006   Estimation Toolkit for Item Response Models (ETIRM)
00007   http://www.smallwaters.com/software/cpp/etirm.html
00008 
00009   Author(s): 
00010   Werner Wothke, maintenance (http://www.smallwaters.com)
00011   Brad Hanson (http://www.b-a-h.com/)
00012   See the file LICENSE for information on usage and redistribution.
00013 
00014   Copyright (C) 2008, Werner Wothke
00015   Copyright (c) 2000-2001, Bradley A. Hanson
00016  */
00017 
00018 #ifndef ETIRM_SIMULATERESPONSES_H_
00019 #define ETIRM_SIMULATERESPONSES_H_
00020 
00021 #ifdef ETIRM_NO_DIR_PREFIX
00022 #include "etirmtypes.h"
00023 #else
00024 #include "etirm/etirmtypes.h"
00025 #endif
00026 
00027 namespace etirm
00028 {
00029 
00030   /*!
00031     \brief
00032     Simulate responses to a set of items for a specific latent variable value.
00033    
00034     \section template_args Template Parameter
00035    
00036     \param  II  Type of iterator over items (iterator over pointers to Item objects)
00037     \param  T Type of latent variable.
00038     \param  R Type of function object for generating random numbers in the interval (0,1).
00039     \param  RI  Type of iterator over responses.
00040 
00041     \section function_args Function Parameters
00042    
00043     \param[in]  item_begin  Iterator pointing to first item.
00044     \param[in]  item_end  Iterator pointing to one past last item.
00045     \param[in]  &theta  Value of latent variable for which response will be generated.
00046     \param[in]  &rand Uniform (0,1) random number generator.
00047     \param[out] iresp Iterator pointing to first response to be generated.
00048     \param[in]  resp_indices - If true generate integer response indices, rather than
00049        responses of type Response, The integer index for the first response
00050        category is zero, the integer index for the second response category
00051        is is one, etc. If true then the type RI must be an interator over a
00052        container of integers, otherwise RI should be an interator over a
00053        container of type Response. 
00054    */
00055   template <class II, class T, class R, class RI> void SimulateResponses(II item_begin,
00056       II item_end, T &theta, R &rand, RI iresp, bool resp_indices=false)
00057   {
00058     std::vector<Real> prob;
00059     for (; item_begin != item_end; ++item_begin, ++iresp)
00060     {
00061       int ncat = (*item_begin)->NumRespCat();
00062       prob.resize(ncat);
00063       (*item_begin)->ProbRespAll(theta, prob.begin());
00064 
00065       // compute cumulative probabilities
00066       std::vector<Real>::iterator ip = prob.begin()+1;
00067       int i = ncat - 2;
00068       while (i--)
00069       {
00070         *ip += ip[-1];
00071         ++ip;
00072       }
00073       *ip = 1.0; // last cumulative probability should be 1
00074 
00075       // Generate response by finding first cumulative probability
00076       // greater than a random number
00077       Real r = rand();
00078       i = 0;
00079       ip = prob.begin();
00080       while (*ip <= r)
00081       {
00082         ++ip;
00083         ++i;
00084       }
00085 
00086       *iresp = (resp_indices) ? i : (*item_begin)->IndexResponse(i);
00087     }
00088   }
00089 
00090 } // namespace etirm
00091 
00092 #endif // ETIRM_SIMULATERESPONSES_H_

Generated on Sat Mar 1 21:40:16 2008 for ETIRM by  doxygen 1.5.4