## Revision 1239 trunk/contrib/statslib/dnorm.cpp

dnorm.cpp (revision 1239)
1 1
#include "statsLib.h"

2 2
#include <admodel.h>

3
#include <df1b2fun.h>

4
#include <adrndeff.h>

3 5

4 6
/**

5 7
*

......
11 13
*

12 14
*

13 15
* This file contains the negative loglikelihood

14
* functions for the normal distribution. The function

16
* functions for the normal distribution and is implemented to be consistent

17
* with the statistical program R with log=TRUE. The function

15 18
* dnorm is overloaded to accomodate single variables and vectors.

19
*

20
* There are also overloaded versions where the user can specify the likelihood (i.e., log=FALSE)

16 21
*

17
* The function is implemented as:

22
* The function is implemented as the negative log of the normal density function:

18 23
* \f[

19 24
*  0.5\ln(2 \pi) + \ln(\sigma) + 0.5\frac{(x-\mu)^2}{\sigma^2}

20 25
* \f]

......
51 56
	return 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);

52 57
}

53 58

59
dvariable dnorm( const prevariable& x, const double& mu, const double& std, bool bLog=true )

60
{

61

62
	if( std<=0 )

63
	{

64
		cerr<<"Standard deviation is less than or equal to zero in "

65
		"dnorm(const dvariable& x, const double& mu, const double& std)\n";

66
		return 0;

67
	}

68
	dvariable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);

69
	if(!bLog)  tmp = mfexp(-tmp);

70
	return tmp;

71
}

72

73
df1b2variable dnorm( const df1b2variable& x, const df1b2variable& mu, const df1b2variable& std, bool bLog=true )

74
{

75

76
	if( value(std)<=0 )

77
	{

78
		cerr<<"Standard deviation is less than or equal to zero in "

79
		"dnorm(const dvariable& x, const double& mu, const double& std)\n";

80
		ad_exit(1);

81
		//return df1b2variable(0);

82
	}

83
	df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);

84
	if(!bLog)  	  tmp = mfexp(-tmp);

85
	return tmp;

86
}

87

88
df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std )

89
{

90

91
	if( value(std)<=0 )

92
	{

93
		cerr<<"Standard deviation is less than or equal to zero in "

94
		"dnorm(const df1b2variable& x, const double& mu, const double& std)\n";

95
		ad_exit(1);

96
		//return df1b2variable(0);

97
	}

98
	df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);

99
	return tmp;

100
}

101

102

103
df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std, bool bLog=true )

104
{

105

106
	if( value(std)<=0 )

107
	{

108
		cerr<<"Standard deviation is less than or equal to zero in "

109
		"dnorm(const df1b2variable& x, const double& mu, const double& std)\n";

110
		ad_exit(1);

111
		//return df1b2variable(0);

112
	}

113
	df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);

114
	if(!bLog)  	  tmp = mfexp(-tmp);

115
	return tmp;

116
}

117

118

54 119
/**

55 120
	\author Steven James Dean Martell

56 121
	\date 2011-06-21

......
67 132
	{

68 133
		cerr<<"Standard deviation is less than or equal to zero in "

69 134
		"dnorm( const dvar_vector& x, const double& mu, const double& std )\n";

70
		return 0;

135
		ad_exit(1);

... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff