GMM 程序评估器不喜欢我的临时变量
GMM program evaluator does not like my temporary variable
据我所知,下面代码中的两个程序是相同的。在第一个程序中,我只是将参数分配给一个标量。在第二个程序中,我将每个观察的这个标量存储在一个临时变量中。
从数学上讲,这应该是相同的,但第二个程序产生 "numerical derivatives are approximate" 和 "flat or discontinuous region encountered"。
为什么第二种方法不能正确计算导数?
clear
set obs 10000
set seed 42
gen x = runiform() * 10
gen eps = rnormal()
gen y = 2 + .3 * x + eps
capture program drop testScalar
program testScalar
syntax varlist [if], at(name)
scalar b0 = `at'[1,1]
scalar b1 = `at'[1,2]
replace `varlist' = y - b0 - b1* x
end
capture program drop testTempvar
program testTempvar
syntax varlist [if], at(name)
tempvar tmp
scalar b0 = `at'[1,1]
scalar b1 = `at'[1,2]
gen `tmp' = b1
replace `varlist' = y - b0 - `tmp'* x
end
gmm testScalar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
gmm testTempvar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
输出:
. gmm testScalar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
(10,000 real changes made)
Step 1
Iteration 0: GMM criterion Q(b) = 417.93313
Iteration 1: GMM criterion Q(b) = 1.690e-23
Iteration 2: GMM criterion Q(b) = 3.568e-30
note: model is exactly identified
GMM estimation
Number of parameters = 2
Number of moments = 2
Initial weight matrix: Identity Number of obs = 10,000
------------------------------------------------------------------------------
| Robust
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
/b1 | 2.022865 .0200156 101.06 0.000 1.983635 2.062095
/b2 | .2981147 .003465 86.04 0.000 .2913235 .3049059
------------------------------------------------------------------------------
Instruments for equation 1: x _cons
. gmm testTempvar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
(10,000 real changes made)
Step 1
Iteration 0: GMM criterion Q(b) = 417.93313
numerical derivatives are approximate
flat or discontinuous region encountered
Iteration 1: GMM criterion Q(b) = 8.073e-17
numerical derivatives are approximate
flat or discontinuous region encountered
Iteration 2: GMM criterion Q(b) = 8.073e-17 (backed up)
note: model is exactly identified
GMM estimation
Number of parameters = 2
Number of moments = 2
Initial weight matrix: Identity Number of obs = 10,000
------------------------------------------------------------------------------
| Robust
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
/b1 | 2.022865 .0201346 100.47 0.000 1.983402 2.062328
/b2 | .2981147 .0034933 85.34 0.000 .291268 .3049613
------------------------------------------------------------------------------
Instruments for equation 1: x _cons
.
在程序 testTempvar
中,您需要生成临时变量 tmp
作为 double 类型:
generate double `tmp' = b1
换句话说,这是一个精度问题。
据我所知,下面代码中的两个程序是相同的。在第一个程序中,我只是将参数分配给一个标量。在第二个程序中,我将每个观察的这个标量存储在一个临时变量中。
从数学上讲,这应该是相同的,但第二个程序产生 "numerical derivatives are approximate" 和 "flat or discontinuous region encountered"。
为什么第二种方法不能正确计算导数?
clear
set obs 10000
set seed 42
gen x = runiform() * 10
gen eps = rnormal()
gen y = 2 + .3 * x + eps
capture program drop testScalar
program testScalar
syntax varlist [if], at(name)
scalar b0 = `at'[1,1]
scalar b1 = `at'[1,2]
replace `varlist' = y - b0 - b1* x
end
capture program drop testTempvar
program testTempvar
syntax varlist [if], at(name)
tempvar tmp
scalar b0 = `at'[1,1]
scalar b1 = `at'[1,2]
gen `tmp' = b1
replace `varlist' = y - b0 - `tmp'* x
end
gmm testScalar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
gmm testTempvar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
输出:
. gmm testScalar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
(10,000 real changes made)
Step 1
Iteration 0: GMM criterion Q(b) = 417.93313
Iteration 1: GMM criterion Q(b) = 1.690e-23
Iteration 2: GMM criterion Q(b) = 3.568e-30
note: model is exactly identified
GMM estimation
Number of parameters = 2
Number of moments = 2
Initial weight matrix: Identity Number of obs = 10,000
------------------------------------------------------------------------------
| Robust
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
/b1 | 2.022865 .0200156 101.06 0.000 1.983635 2.062095
/b2 | .2981147 .003465 86.04 0.000 .2913235 .3049059
------------------------------------------------------------------------------
Instruments for equation 1: x _cons
. gmm testTempvar, nequations(1) nparameters(2) instr(x) winitial(identity) onestep
(10,000 real changes made)
Step 1
Iteration 0: GMM criterion Q(b) = 417.93313
numerical derivatives are approximate
flat or discontinuous region encountered
Iteration 1: GMM criterion Q(b) = 8.073e-17
numerical derivatives are approximate
flat or discontinuous region encountered
Iteration 2: GMM criterion Q(b) = 8.073e-17 (backed up)
note: model is exactly identified
GMM estimation
Number of parameters = 2
Number of moments = 2
Initial weight matrix: Identity Number of obs = 10,000
------------------------------------------------------------------------------
| Robust
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
/b1 | 2.022865 .0201346 100.47 0.000 1.983402 2.062328
/b2 | .2981147 .0034933 85.34 0.000 .291268 .3049613
------------------------------------------------------------------------------
Instruments for equation 1: x _cons
.
在程序 testTempvar
中,您需要生成临时变量 tmp
作为 double 类型:
generate double `tmp' = b1
换句话说,这是一个精度问题。