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

Go to the documentation of this file.
00001 /*! \file DiscreteNormalDist.h
00002   
00003   \brief
00004   Function for computing a discrete approximation to a normal
00005   distribution.
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_DISCRETENORMALDIST_H_
00020 #define ETIRM_DISCRETENORMALDIST_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 exp
00029 
00030 // for compilers which do not put C library functions in std namespace
00031 #ifdef BOOST_NO_STDC_NAMESPACE
00032 namespace std {using ::exp;}
00033 #endif
00034 
00035 namespace etirm
00036 {
00037 
00038 /*!
00039   DiscreteNormalDist
00040   
00041   \brief
00042   Compute points and weights for a discrete distribution
00043   that approximates a normal distribution
00044   
00045   Arguments
00046   
00047   nQuad   Number of discrete points in distribution.
00048   minPoint  Value of smallest point in distribution (must be smaller than the mean).
00049   maxPoint  Value of largest point in distribution (must be larger than the mean).
00050   ipoints   Iterator over nQuad points of distribution.
00051   iweights  Iterator over nQuad probabilities corresponding to the points.
00052   mean    Mean of normal distribution.
00053   sd      Standard deviation of normal distribution.
00054 */
00055 template<class PI, class WI>
00056 void DiscreteNormalDist(int nQuad, double minPoint, double maxPoint, PI ipoints,
00057   WI iweights, double mean = 0.0, double sd = 1.0)
00058 {
00059   int i;
00060   
00061   if ((maxPoint-minPoint) <= 0.0 || minPoint > mean || maxPoint < mean) 
00062     throw InvalidArgument("Invalid minimum and maximum points", "DiscreteNormalDist");
00063   
00064   double incr = (maxPoint-minPoint) / (nQuad-1.0);
00065   
00066   PI points = ipoints;
00067   WI weights = iweights;
00068   double theta = minPoint;
00069   double sum = 0.0;
00070   for (i=nQuad; i--; ++points, ++weights)
00071   {
00072     *points = (theta - mean) / sd;
00073     *weights = std::exp(-0.5 * theta * theta);
00074     sum += *weights;
00075     theta += incr;
00076   }
00077   
00078   /* Standardize weights */
00079   weights = iweights;
00080   for (i=nQuad; i--; ++weights)
00081   {
00082     *weights /= sum;
00083   }
00084   
00085 }
00086 
00087 } // namespace etirm
00088 
00089 #endif // ETIRM_DISCRETENORMALDIST_H_

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