Hello everyone,
I already have excess return data for ten assets (variables er1, er2, ..., er10) and a market portfolio labeled "vwerm." I am looking to compute the statistic from the following Equation (1). Since this is a just-idenfitied system, indicating that all parameters can be estimated by OLS, and residuals can be obtained accordingly.

I wrote a custom gmmwald.ado script using Claude, but the computed values seem excessively large.
I would greatly appreciate it if any expert could spare some time to help identify and correct the issue. Thank you!
Note that some data can be found here: ado file for gmm wald test? - Statalist 。
I already have excess return data for ten assets (variables er1, er2, ..., er10) and a market portfolio labeled "vwerm." I am looking to compute the statistic from the following Equation (1). Since this is a just-idenfitied system, indicating that all parameters can be estimated by OLS, and residuals can be obtained accordingly.
I wrote a custom gmmwald.ado script using Claude, but the computed values seem excessively large.
Code:
capture program drop gmmwald
program define gmmwald, eclass
version 12
syntax varlist [if], flist(string) [nqui]
marksample touse
local nvar: word count `varlist'
// Store sample size
qui count if `touse'
local T = r(N)
// Store all OLS coefficients (both α and β)
tempname delta
matrix `delta' = J(2*`nvar', 1, 0)
// Generate residuals first
local i = 1
foreach var of varlist `varlist' {
tempvar res_`i'
qui reg `var' `flist' if `touse'
matrix `delta'[2*`i'-1, 1] = e(b)[1,1] // α
matrix `delta'[2*`i', 1] = e(b)[1,2] // β
qui predict double `res_`i'' if `touse', residuals
local ++i
}
// Create DT and ST matrices
tempname DT ST
matrix `DT' = J(2*`nvar', 2*`nvar', 0)
matrix `ST' = J(2*`nvar', 2*`nvar', 0)
tempvar x1 x2
gen double `x1' = 1 if `touse'
gen double `x2' = `flist' if `touse'
// Process each observation to build DT
local xTx11_sum = 0
local xTx12_sum = 0
local xTx22_sum = 0
qui forvalues t = 1/`T' {
local x1t = `x1'[`t']
local x2t = `x2'[`t']
local xTx11_sum = `xTx11_sum' + `x1t'*`x1t'
local xTx12_sum = `xTx12_sum' + `x1t'*`x2t'
local xTx22_sum = `xTx22_sum' + `x2t'*`x2t'
}
// Fill DT matrix (sum of IN⊗xtxt')
forvalues i = 1/`nvar' {
forvalues j = 1/`nvar' {
if `i'==`j' {
matrix `DT'[2*`i'-1,2*`j'-1] = `xTx11_sum'
matrix `DT'[2*`i'-1,2*`j'] = `xTx12_sum'
matrix `DT'[2*`i',2*`j'-1] = `xTx12_sum'
matrix `DT'[2*`i',2*`j'] = `xTx22_sum'
}
}
}
// Fill ST matrix based on equation (22): sum of êtêt'⊗xtxt'
forvalues i = 1/`nvar' {
forvalues j = 1/`nvar' {
tempvar p11 p12 p21 p22
gen double `p11' = `res_`i''*`res_`j''*`x1'*`x1' if `touse'
gen double `p12' = `res_`i''*`res_`j''*`x1'*`x2' if `touse'
gen double `p21' = `res_`i''*`res_`j''*`x2'*`x1' if `touse'
gen double `p22' = `res_`i''*`res_`j''*`x2'*`x2' if `touse'
qui sum `p11' if `touse'
matrix `ST'[2*`i'-1,2*`j'-1] = r(sum)
qui sum `p12' if `touse'
matrix `ST'[2*`i'-1,2*`j'] = r(sum)
qui sum `p21' if `touse'
matrix `ST'[2*`i',2*`j'-1] = r(sum)
qui sum `p22' if `touse'
matrix `ST'[2*`i',2*`j'] = r(sum)
}
}
// Now divide both matrices by T
matrix `DT' = `DT'/`T'
matrix `ST' = `ST'/`T'
// Create selection matrix R = IN ⊗ (1 0)
tempname R
matrix `R' = J(`nvar', 2*`nvar', 0)
forvalues i = 1/`nvar' {
forvalues j = 1/`nvar' {
if `i' == `j' {
matrix `R'[`i', 2*`j'-1] = 1 // 1 in first column
matrix `R'[`i', 2*`j'] = 0 // 0 in second column
}
}
}
// Get alpha vector: α = R𝛿
tempname alpha
matrix `alpha' = `R'*`delta'
// Calculate test statistic
tempname DTST DTD RDTDR phi1
matrix `DTST' = `DT''*syminv(`ST')
matrix `DTD' = `DTST'*`DT'
matrix `RDTDR' = `R'*syminv(`DTD')*`R''
matrix `phi1' = ((`T'-`nvar'-1)/`T')*`alpha''*syminv(`RDTDR')*`alpha'
// Store and display results
ereturn clear
ereturn scalar chi2 = `phi1'[1,1]
ereturn scalar df = `nvar'
ereturn scalar p = chi2tail(`nvar', `phi1'[1,1])
ereturn scalar N = `T'
di as txt _n "GMM test of mean-variance efficiency"
di as txt "Chi2(" as res `nvar' as txt ") = " as res %8.4f `phi1'[1,1]
di as txt "Prob > chi2 = " as res %8.4f chi2tail(`nvar', `phi1'[1,1])
end
Note that some data can be found here: ado file for gmm wald test? - Statalist 。
