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

Go to the documentation of this file.
00001 /*! \file ThetaLogLikelihood.h
00002  
00003   \brief
00004   Function object for computing loglikelihood for theta parameter
00005   of an IRT model.
00006 
00007   Estimation Toolkit for Item Response Models (ETIRM)
00008   http://www.smallwaters.com/software/cpp/etirm.html
00009 
00010   Author(s): 
00011   Werner Wothke, maintenance (http://www.smallwaters.com)
00012   Brad Hanson (http://www.b-a-h.com/)
00013   See the file LICENSE for information on usage and redistribution.
00014 
00015   Copyright (C) 2008, Werner Wothke
00016   Copyright (c) 2000-2001, Bradley A. Hanson
00017  */
00018 
00019 #ifndef ETIRM_THETALOGLIKELIHOOD_H_
00020 #define ETIRM_THETALOGLIKELIHOOD_H_
00021 
00022 #ifdef ETIRM_NO_DIR_PREFIX
00023 #include "etirmtypes.h"
00024 #else
00025 #include "etirm/etirmtypes.h"
00026 #endif
00027 
00028 #include <cmath> // for log
00029 #ifdef BOOST_NO_STDC_NAMESPACE
00030 // for compilers which do not put C library functions in std namespace
00031 namespace std
00032 { using ::log;}
00033 #endif
00034 
00035 namespace etirm
00036 {
00037 
00038   /*!
00039     \brief
00040     Function object for computing loglikelihood for theta parameter of an IRT model.
00041 
00042     \section template_args Template Parameters
00043    
00044     \param II Iterator to Item object pointer.
00045     \param RI Type iterator over item responses.
00046     \param T  Type of latent variable.
00047    */
00048   template <class II, class RI, class T =Real> class ThetaLogLikelihood
00049   {
00050 
00051 public:
00052 
00053     //! Constructor - Assigns values of data members.
00054     ThetaLogLikelihood(II begini, II endi, RI beginr, bool neg = false)
00055     {
00056       mBeginItems = begini;
00057       mEndItems = endi;
00058       mBeginResp = beginr;
00059       mNeg = neg;
00060     }
00061 
00062     /*!
00063       \brief
00064       Overloads function operator to return loglikelihood for a particular value of theta.
00065 
00066       \section template_args Template Parameters
00067    
00068       \param T  Type of latent variable.
00069       
00070       \section function_args Function Parameters
00071             
00072       \param[in]  &theta  Address of ability parameter theta.
00073      */
00074     Real operator()(const T &theta) const;
00075 
00076     /*!
00077       \brief
00078       Assigns item responses to compute likelihood over.
00079       
00080       \section template_args Template Parameters
00081    
00082       \param RI Type iterator over item responses.
00083 
00084       \section function_args Function Parameters
00085       
00086       \param[in] resp Iterator pointing to first item response of each item in
00087           mBeginItems to mEndItems range.
00088      */
00089     void SetResponses(RI resp)
00090     {
00091       mBeginResp = resp;
00092     }
00093 
00094 private:
00095 
00096     //! Iterator pointing to first item response of each item in mBeginItems to mEndItems range.
00097     RI mBeginResp;
00098     
00099     //! Iterator to pointer to first item for likelihood computation. 
00100     II mBeginItems;
00101     
00102     //! Iterator to pointer to one past last item for likelihood computation. 
00103     II mEndItems;
00104 
00105     /*!
00106       \brief
00107       Flag: If true return the negative of the log likelihood.
00108       
00109       This allows the use of minimization routines rather than maximization routines 
00110       for computing maximum likelihood estimates.
00111      */
00112     bool mNeg;
00113   };
00114 
00115   template <class II, class RI, class T> Real ThetaLogLikelihood<II, RI, T>::operator()(const T &theta) const
00116   {
00117     Real loglikelihood = 0.0;
00118 
00119     for (II ii = mBeginItems; ii != mEndItems; ++ii)
00120     {
00121       RI ir = mBeginResp + (*ii)->Index();
00122       if (*ir != (*ii)->NotPresentedResponse())
00123       {
00124         loglikelihood += std::log((*ii)->ProbResp(*ir, theta));
00125       }
00126     }
00127 
00128     // return negative of loglikelihood to allow use of
00129     // minimization routine rather than maximization routine
00130     return (mNeg ? -loglikelihood : loglikelihood);
00131   }
00132 
00133 } // namespace etirm
00134 
00135 #endif // ETIRM_THETALOGLIKELIHOOD_H_

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