Hello everyone,
I would like to create a gmmwald.ado file to implement Equation (10) from the referenced article (Mackinlay, A. C., & Richardson, M. P. (1991). Using Generalized Method of Moments to Test Mean-Variance Efficiency. The Journal of Finance, 46(2), 511-536. A partial screenshot is attached below).



Essentially, this is a GMM test statistic.
I have attached the first 100 rows of the raw data below, and I have used the following code for preliminary processing (to calculate excess returns).
I have drafted an initial version of the .ado file (indeed from Claude), but the calculated test statistic appears to be incorrect (way too large).
Could anyone assist with this issue?
Thank you!
I would like to create a gmmwald.ado file to implement Equation (10) from the referenced article (Mackinlay, A. C., & Richardson, M. P. (1991). Using Generalized Method of Moments to Test Mean-Variance Efficiency. The Journal of Finance, 46(2), 511-536. A partial screenshot is attached below).
Essentially, this is a GMM test statistic.
I have attached the first 100 rows of the raw data below, and I have used the following code for preliminary processing (to calculate excess returns).
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int ym double(CAP1RET CAP2RET CAP3RET CAP4RET CAP5RET CAP6RET CAP7RET CAP8RET CAP9RET CAP10RET vwretd rf) -12418 .143661 .043982 .003839 .021334 .026445 -.001635 .004253 .002818 .004725 -.003837 .000561 .002951 -12387 -.045718 -.067602 -.045779 -.079699 -.070859 -.053558 -.05208 -.056464 -.035508 -.022392 -.033046 .002768 -12359 -.139863 -.095192 -.125909 -.116952 -.093662 -.088367 -.099882 -.075299 -.06946 -.051874 -.064002 .002778 -12328 .046633 .011215 .010318 .055662 .062817 .021249 .04172 .044396 .034477 .036293 .037029 .003072 -12298 -.004305 -.021611 .002612 .023445 .009262 -.00964 .012968 .01162 .013865 .013097 .012095 .000342 -12267 .001354 .048366 .06317 .036993 .037166 .032904 .055509 .053306 .060762 .053729 .053664 .003459 -12237 .001944 .022262 -.005706 .010793 .007995 .028931 .016036 -.000063 .035584 .039044 .031411 .0022 -12206 .00541 .066822 .031689 .021817 .036003 .024769 .031235 .021855 .00778 .035295 .028647 .0025 -12175 .004253 -.02565 -.009283 .008334 .005375 -.011681 -.014328 .004121 -.002456 .011451 .005787 .0023 -12145 -.022551 -.029155 -.023392 -.051732 -.027948 -.016857 -.044976 -.035516 -.027826 -.02716 -.028996 .0032 -12114 .00651 .026459 .012604 .034276 .034761 .033904 .038977 .030944 .033647 .025669 .028554 .0031 -12084 .018044 .026793 .053952 .020025 .032043 .019592 .011399 .032529 .034021 .029075 .029211 .0028 -12053 .003172 .002805 .014317 .032929 .016315 .000204 .020612 .007512 .007482 -.002207 .002416 .0025 -12022 .092438 .07166 .058662 .053087 .063236 .064697 .076746 .039833 .042902 .041793 .04537 .0026 -11994 -.088693 -.008173 -.028944 -.003188 -.032311 -.033397 -.007091 -.014345 .001849 .012043 .003756 .003 -11963 .039481 .022963 -.002202 .005944 .007973 .0072 .001073 -.006035 .003022 .010453 .007225 .0025 -11933 .075115 .076116 .102096 .090009 .039405 .064919 .081353 .060786 .0606 .053249 .057773 .003 -11902 -.031104 -.046546 -.004492 -.006333 -.017199 -.000806 -.015678 -.026593 -.032439 -.019647 -.021087 .0026 -11872 .072731 .044965 .054679 .030701 .044819 .053533 .063468 .069224 .056557 .086322 .075279 .003 -11841 -.004154 -.01282 -.01179 -.00099 .005784 -.012889 .006845 .013053 .019656 .029562 .02249 .0028 -11810 -.006315 .0299 .018491 .027097 .024686 .059165 .056898 .051292 .046465 .051737 .049924 .0021 -11780 -.05634 -.057721 -.055306 -.017724 -.028274 -.019678 -.032767 -.036301 -.046828 -.042097 -.04077 .0025 -11749 .151983 .089883 .096032 .11086 .067772 .070954 .07673 .073683 .074324 .06364 .068453 .0021 -11719 .061282 .010987 .020371 .024959 .032628 .035111 .03349 .035554 .043869 .015569 .023782 .0022 -11688 .082891 .036933 .050425 .024538 .007223 .032571 .010485 -.005964 .01896 -.013901 -.00334 .0025 -11657 -.017513 -.017698 -.020122 -.029133 -.03966 -.023506 -.02291 -.040592 -.025027 -.005626 -.014478 .0033 -11628 .023658 .064201 .076955 .081474 .076788 .075151 .075976 .076318 .076708 .099223 .090478 .0029 -11597 .224499 .183015 .083906 .057361 .075368 .069206 .068872 .046976 .041375 .036652 .043846 .0022 -11567 .12849 .036144 .052157 -.01328 .017232 .028186 .020126 .012563 .022465 .016 .01769 .0032 -11536 -.104891 -.13488 -.069093 -.061488 -.069361 -.052095 -.056888 -.065891 -.059967 -.034886 -.045245 .0031 -11506 .010951 -.006581 .014548 -.003397 .006102 .009049 .015669 -.004996 .017312 .00916 .0094 .0032 -11475 .015433 .062216 .047886 .036981 .07533 .076558 .087567 .073003 .079106 .067533 .070206 .0032 -11444 .097367 .056287 .045872 .047999 .064136 .059114 .054335 .064383 .028912 .022603 .031498 .0027 -11414 .025601 .051576 .029264 .018854 .019236 .002469 .018249 -.01103 .037063 .01835 .019228 .0041 -11383 .130821 .227316 .088059 .102203 .114307 .124567 .113406 .093084 .127404 .124411 .1215 .0038 -11353 -.042115 -.030924 -.011626 -.016361 .012337 .018998 -.007102 .024673 .026132 -.000647 .005557 .0006 -11322 .03707 .014078 .026836 .018479 .013253 .025703 .024849 .039332 .065507 .059319 .053935 .0034 -11291 -.007803 .000331 .004008 .002708 .012668 .00517 .019399 .007045 -.001674 -.003365 -.000446 .0036 -11263 .003639 -.035392 -.050998 -.044424 -.04787 -.035577 -.043825 -.023619 -.018128 .004389 -.007808 .0034 -11232 .019042 -.004377 .007913 -.001003 .018287 .01909 .03502 -.004106 .012951 .020186 .017179 .0036 -11202 -.166509 -.14534 -.124481 -.117397 -.087943 -.085604 -.082233 -.062092 -.043471 -.056839 -.059923 .0044 -11171 .088769 .059679 .054146 .047624 .084693 .086686 .088231 .098724 .10875 .104573 .101573 .0052 -11141 .007532 .03095 .023216 .009991 .005164 .019716 .047149 .009014 .042647 .055169 .045958 .0033 -11110 -.015796 -.027972 -.004619 .02113 .017976 .037552 .039934 .047308 .077047 .103681 .085343 .004 -11079 -.034762 -.120364 -.021394 -.052491 -.040756 -.036012 -.015793 -.028472 -.043051 -.057889 -.050455 .0035 -11049 -.229883 -.292596 -.198277 -.247534 -.234577 -.212465 -.210601 -.22481 -.196361 -.188684 -.196684 .0046 -11018 -.143028 -.145817 -.116742 -.129928 -.132945 -.115422 -.136501 -.126292 -.10072 -.127686 -.123812 .0037 -10988 -.055723 .007445 -.04372 -.027165 .007628 -.009889 .011206 -.009426 .027596 .02018 .01639 .0037 -10957 .240036 .167626 .119549 .166176 .073301 .09246 .074717 .052793 .05773 .053463 .058407 .0014 -10926 .057641 .048143 .024057 .039716 .037114 .021057 .010029 .019153 .038548 .027676 .028242 .003 -10898 .113523 .148527 .102964 .082762 .111455 .114041 .090223 .069006 .072672 .071364 .074631 .0035 -10867 -.081904 -.034999 -.028679 -.050306 -.041971 -.018092 -.041375 -.055333 -.038932 -.010452 -.020541 .0021 -10837 -.071979 -.078269 -.045166 -.035207 -.023946 -.034191 -.037347 -.025789 -.016607 -.008523 -.01401 .0026 -10806 -.237253 -.226064 -.231609 -.220168 -.195867 -.190038 -.180644 -.165592 -.168717 -.152066 -.15998 .0027 -10776 .039041 .011589 .022182 .05678 .064175 .042877 .036361 .03517 .048418 .041149 .041964 .002 -10745 -.05377 -.012513 -.04208 -.014341 -.018524 .005221 -.020015 -.013425 .000124 .00862 .003663 .0009 -10714 -.123727 -.221217 -.181852 -.155971 -.135511 -.135387 -.137273 -.139872 -.129182 -.119795 -.124805 .0022 -10684 -.112471 -.10566 -.109747 -.11096 -.092347 -.090768 -.09029 -.097451 -.095591 -.082359 -.086579 .0009 -10653 -.056232 -.034625 -.025861 -.035211 -.015081 -.023398 -.031216 -.021481 -.031108 -.028222 -.028057 .0013 -10623 -.207887 -.151164 -.139404 -.169597 -.091007 -.091987 -.09202 -.111207 -.090503 -.06842 -.077739 .0014 -10592 .281433 .208469 .194756 .144317 .123567 .107461 .081096 .083028 .090925 .05144 .06378 .0015 -10561 .290888 .243834 .155307 .127578 .143261 .158058 .125742 .120132 .097909 .107056 .109695 .0004 -10533 -.119358 -.053174 -.078331 -.056364 -.076851 -.065308 -.064719 -.085468 -.081896 -.056536 -.062866 .0013 -10502 -.232438 -.212228 -.16887 -.16653 -.139804 -.172244 -.12102 -.135607 -.118346 -.087841 -.100219 .0008 -10472 -.153046 -.154801 -.129542 -.136654 -.125867 -.131922 -.14404 -.150284 -.161717 -.126868 -.133836 .0009 -10441 .231803 .201013 .178055 .152165 .150139 .151982 .135384 .188425 .158618 .132178 .14034 .0008 -10411 -.068877 -.057442 -.07121 -.063396 -.076305 -.073075 -.07455 -.062892 -.073493 -.064701 -.066438 .0006 -10380 -.063719 -.063264 -.033312 -.011855 -.009152 .000652 -.027168 .013837 .002859 .004637 .003078 .0003 -10349 -.350205 -.329675 -.325695 -.319836 -.333994 -.323497 -.299028 -.325057 -.314467 -.281972 -.291731 .0003 -10319 .079485 .079018 .094134 .095108 .101027 .086504 .110404 .115443 .094808 .074505 .081509 .001 -10288 -.10708 -.098259 -.109429 -.080187 -.044784 -.075909 -.060965 -.119283 -.087815 -.088233 -.088402 .0017 -10258 -.257788 -.225707 -.197984 -.159455 -.140664 -.162724 -.154758 -.12934 -.142861 -.131064 -.134971 .0012 -10227 .237888 .20071 .107753 .065379 .058712 .054104 .020985 .023347 .010074 -.022628 -.010534 .0023 -10196 -.027567 .004308 .033981 -.014774 -.001461 -.000502 .039605 .021817 .043877 .067463 .057663 .0023 -10167 -.130632 -.15277 -.131207 -.157113 -.109051 -.131713 -.128092 -.093739 -.118178 -.108705 -.110599 .0016 -10136 -.186865 -.257934 -.198573 -.192806 -.203727 -.162734 -.16861 -.15384 -.16009 -.184826 -.179417 .0011 -10106 -.097796 -.129686 -.231994 -.221164 -.229135 -.276508 -.229289 -.212066 -.205019 -.203089 -.206198 .0006 -10075 -.075865 -.024914 .003154 .072597 .009763 .044731 .048151 .028748 .009084 -.013098 -.004289 .0002 -10045 .328941 .395982 .442142 .313352 .410286 .364774 .351201 .38162 .352516 .333122 .340643 .0003 -10014 .991855 .865855 .71168 .607937 .605076 .537786 .417814 .388951 .439538 .341635 .371388 .0003 -9983 -.167679 -.148931 -.136505 -.116111 -.036298 -.068791 -.025991 -.023133 -.047939 -.021667 -.028649 .0003 -9953 -.185973 -.212451 -.187554 -.16882 -.205495 -.185901 -.165128 -.15302 -.145665 -.12308 -.132553 .0002 -9922 -.111214 -.088589 -.123144 -.109206 -.072353 -.076962 -.054712 -.05441 -.079275 -.053226 -.05826 .0002 -9892 -.103806 -.1146 -.089808 -.079018 -.03897 -.055487 .017689 .01166 .017825 .060233 .045227 .0001 -9861 .065295 .028455 .015775 .065966 .05467 .061578 -.013129 .018919 .022471 .00551 .009813 .0001 -9830 -.187573 -.104406 -.173641 -.184832 -.192467 -.168167 -.145526 -.162917 -.145836 -.152732 -.153204 -.0003 -9802 .09608 .104395 .113697 .130567 .067223 .074416 .111348 .083871 .057825 .018694 .033105 .0004 -9771 .474258 .576137 .399093 .518625 .535615 .494002 .538021 .514179 .429632 .365025 .394143 .001 -9741 1.112312 .848976 .8947 .641794 .511894 .494916 .387598 .316837 .230937 .168143 .213261 .0004 -9710 .385212 .244089 .256677 .152239 .213811 .175284 .168778 .118867 .155304 .122968 .133364 .0002 -9680 -.070338 -.113216 -.0667 -.142317 -.153948 -.133889 -.094344 -.123161 -.120808 -.085587 -.096341 .0002 -9649 .021679 .09291 .052621 .115779 .147433 .167943 .183826 .162919 .150755 .106655 .121285 .0003 -9618 -.185685 -.207425 -.196723 -.167403 -.169759 -.142638 -.109393 -.128616 -.098707 -.099262 -.105836 .0002 -9588 -.122392 -.134562 -.140349 -.130963 -.136433 -.105948 -.115943 -.095858 -.078051 -.078009 -.08365 .0001 -9557 .014808 .082916 .043321 .055798 .064293 .062588 .093196 .094486 .085421 .107255 .099896 .0002 -9527 .020201 -.001696 -.002754 .042504 .066279 .040655 -.002091 .030674 .022983 .014988 .017876 .0002 -9496 .638277 .455986 .430647 .35377 .271084 .288628 .264314 .152341 .157102 .096053 .126779 .0005 -9465 .079372 .044094 .023861 .006601 .002744 .01468 -.030013 -.022518 -.028144 -.026788 -.024287 .0002 -9437 -.01814 .005627 .004384 .007987 .008029 -.009335 .017071 .019419 .014378 .000299 .004425 .0002 -9406 .022085 .017246 .008348 -.009492 -.024848 -.009761 -.019872 -.018976 -.016014 -.019596 -.018345 .0001 end format %tdnn/dd/CCYY ym
Code:
gen vwerm = vwretd-rf
forvalues i=1/10 {
gen er`i' = CAP`i'RET-rf
}
gmmwald er1-er10, flist(vwerm)
Code:
capture program drop gmmwald
program define gmmwald, eclass
version 12
syntax varlist [if], flist(string) [nqui]
marksample touse
local nvar: word count `varlist'
qui count if `touse'
local T = r(N)
// 1. OLS regression and create residuals
local i = 1
foreach var of varlist `varlist' {
qui reg `var' `flist' if `touse'
matrix A`i' = e(b)[1,1] // Store alpha
qui predict double res`i' if `touse', residuals
qui gen double resm`i' = res`i' * `flist' if `touse'
local ++i
}
// Combine alphas into column vector
matrix A = A1
forvalues i = 2/`nvar' {
matrix A = A \ A`i'
}
// 2. Calculate sample moment conditions ft(δ)
tempname ft
matrix `ft' = J(2*`nvar', `T', .)
forvalues t = 1/`T' {
forvalues i = 1/`nvar' {
qui sum res`i' if _n==`t'
matrix `ft'[2*`i'-1,`t'] = r(mean) // εit
qui sum resm`i' if _n==`t'
matrix `ft'[2*`i',`t'] = r(mean) // εit*rpt
}
}
// 3. Calculate S0 matrix (equivalent to ST when no serial correlation)
tempname S0
matrix `S0' = J(2*`nvar', 2*`nvar', 0)
forvalues t = 1/`T' {
matrix `S0' = `S0' + `ft'[.,`t']*`ft'[.,`t']'
}
matrix `S0' = `S0'/`T'
// 4. Calculate D0 matrix
// From paper: D0 = E[∂gT(δ)/∂δ']
tempname D0
matrix `D0' = J(2*`nvar', 2*`nvar', 0)
qui sum `flist' if `touse'
local rp_mean = r(mean)
local rp_var = r(Var)*(`T'-1)/`T'
forvalues i = 1/`nvar' {
matrix `D0'[2*`i'-1,2*`i'-1] = 1
matrix `D0'[2*`i'-1,2*`i'] = `rp_mean'
matrix `D0'[2*`i',2*`i'-1] = `rp_mean'
matrix `D0'[2*`i',2*`i'] = `rp_mean'^2 + `rp_var'
}
// 5. Create R matrix as in equation (10)
tempname R phi1
matrix `R' = J(`nvar', 2*`nvar', 0)
forvalues i = 1/`nvar' {
matrix `R'[`i',2*`i'-1] = 1
}
// 6. Calculate test statistic according to equation (10)
matrix `phi1' = `T'*A'*invsym(`R'*invsym(`D0''*invsym(`S0')*`D0')*`R'')*A
// 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])
// Clean up
forvalues i = 1/`nvar' {
cap drop res`i' resm`i'
}
end
Could anyone assist with this issue?
Thank you!
