I'm getting acquainted with Stata's integ command (for numerical integration, not to be confused with the intreg command for interval regression.)
I'm getting some unexpected results. For example, as a test I used the command to estimate the mean of a standard lognormal variable. According to the theoretical formula, the correct answer is exp(1/2) ~= 1.65. Yet at the end of the following code, integ estimates that the mean is 7.38. What am I doing wrong?
local obs 100
clear
range x -5 5 `obs'
generate f = normalden(x)
twoway line f x /* If x is the density of x, then x is standard normal. */
gen fx = f*x
integ fx x
/* The expectation of x is zero. Correct. */
gen fx2 = f*x^2
integ fx2 x
/* The expected square of x is 1. Correct again. */
/* We got good results for the normal density. Now let's try the log normal. */
gen y = exp(x)
gen fy = f*y
twoway line f y /* If f is the density of y, then y is standard lognormal. */
integ fy y
/* The expectation of y should be 1.65. But it's evaluating as 7.38. */
I'm getting some unexpected results. For example, as a test I used the command to estimate the mean of a standard lognormal variable. According to the theoretical formula, the correct answer is exp(1/2) ~= 1.65. Yet at the end of the following code, integ estimates that the mean is 7.38. What am I doing wrong?
local obs 100
clear
range x -5 5 `obs'
generate f = normalden(x)
twoway line f x /* If x is the density of x, then x is standard normal. */
gen fx = f*x
integ fx x
/* The expectation of x is zero. Correct. */
gen fx2 = f*x^2
integ fx2 x
/* The expected square of x is 1. Correct again. */
/* We got good results for the normal density. Now let's try the log normal. */
gen y = exp(x)
gen fy = f*y
twoway line f y /* If f is the density of y, then y is standard lognormal. */
integ fy y
/* The expectation of y should be 1.65. But it's evaluating as 7.38. */
Comment