Below is the example data, We run the main code 1 that follows and it yields ew and vw returns on decile portfolios.
1. Main code
On the output, i want to calculate:
a. mean and sd matrix
b. regression intercept, coefficients, t-stat, r-squared, adj r-squared.
We used following codes for this purpose previously but it was 5x5 bivariate sorts portfolios then and now it is univariate deciles. Request you to amend the following codes (code 2 and code 3) so that they are compatible with new requirement.
2. code for means and sd
2. code for regression coefficients etc.
I hope main code does sorts each month. Instead, if we wish to have:-
i. sorts only once in June month to form decile and weighting of vw returns for each month using june month mcap values observed at start of year.
ii. sorts each month on previous idiovol values to form deciles and weighting of vw returns by lagged mcap values of each rt observation.
What should the main code 1 be in case of i and ii.
Code:
clear input int stock_id str52 stock float(date mdate rt mcap idiovol) 1 "3M India Ltd." 19875 653 . 5078.8 .07464047 1 "3M India Ltd." 19905 654 .08684798 5519.88 .09564243 1 "3M India Ltd." 19936 655 .20035714 6625.83 .1153478 1 "3M India Ltd." 19967 656 .0796447 7153.54 .05842065 1 "3M India Ltd." 19997 657 -.015353846 7043.71 .05778759 1 "3M India Ltd." 20028 658 .004470022 7075.2 .05916266 1 "3M India Ltd." 20058 659 .030267583 7289.35 .07579362 1 "3M India Ltd." 20089 660 .049886 7652.98 .05058851 1 "3M India Ltd." 20120 661 .16551733 8919.68 .08370074 1 "3M India Ltd." 20148 662 .00643473 8977.08 .07558923 1 "3M India Ltd." 20179 663 -.002064286 8958.55 .11817545 1 "3M India Ltd." 20209 664 .005815781 9010.65 .05396339 1 "3M India Ltd." 20240 665 .03876228 9359.92 .12874845 2 "63 Moons Technologies Ltd." 19875 653 . 1162.1 .06185364 2 "63 Moons Technologies Ltd." 19905 654 .26605865 1471.29 .2051615 2 "63 Moons Technologies Ltd." 19936 655 -.20388347 1171.32 .1596595 2 "63 Moons Technologies Ltd." 19967 656 -.12077104 1029.86 .11630887 2 "63 Moons Technologies Ltd." 19997 657 -.2187919 804.53 .2429151 2 "63 Moons Technologies Ltd." 20028 658 .04925539 844.16 .08935963 2 "63 Moons Technologies Ltd." 20058 659 .10807862 935.39 .15593943 2 "63 Moons Technologies Ltd." 20089 660 -.007389163 928.48 .036555734 2 "63 Moons Technologies Ltd." 20120 661 -.11315138 823.42 .08614067 2 "63 Moons Technologies Ltd." 20148 662 .07302744 883.56 .237539 2 "63 Moons Technologies Ltd." 20179 663 -.10169491 793.7 .074082345 2 "63 Moons Technologies Ltd." 20209 664 -.05573298 749.47 .03879507 2 "63 Moons Technologies Ltd." 20240 665 -.0624039 702.7 .15120164 3 "A B B India Ltd." 19875 653 . 23777.18 .07483307 3 "A B B India Ltd." 19905 654 -.072323 22057.54 .07221169 3 "A B B India Ltd." 19936 655 -.05596119 20823.18 .03861913 3 "A B B India Ltd." 19967 656 .16755714 24312.25 .12428077 3 "A B B India Ltd." 19997 657 .016952803 24724.41 .06956107 3 "A B B India Ltd." 20028 658 -.04795368 23538.78 .06761845 3 "A B B India Ltd." 20058 659 .1579041 27255.66 .16287985 3 "A B B India Ltd." 20089 660 -.001088402 27225.99 .04814025 3 "A B B India Ltd." 20120 661 .0859277 29565.46 .10311798 3 "A B B India Ltd." 20148 662 -.09905387 26636.88 .05732526 3 "A B B India Ltd." 20179 663 .05962605 28225.14 .06130007 3 "A B B India Ltd." 20209 664 .006043807 28395.72 .06657626 3 "A B B India Ltd." 20240 665 -.012873135 28030.18 .05802079 4 "A B G Shipyard Ltd." 19875 653 . 1432.68 .09499458 4 "A B G Shipyard Ltd." 19905 654 -.0972099 1293.41 .05455963 4 "A B G Shipyard Ltd." 19936 655 -.0690945 1204.05 .11501205 4 "A B G Shipyard Ltd." 19967 656 -.04715582 1147.27 .12661067 4 "A B G Shipyard Ltd." 19997 657 .009542804 1188.16 .06451628 4 "A B G Shipyard Ltd." 20028 658 -.011870728 1174.06 .09569627 4 "A B G Shipyard Ltd." 20058 659 .02602895 1234.19 .063639 4 "A B G Shipyard Ltd." 20089 660 -.11513444 1092.09 .027290424 4 "A B G Shipyard Ltd." 20120 661 .15731433 1269.39 .2665481 4 "A B G Shipyard Ltd." 20148 662 -.03980519 1220.81 .21235485 4 "A B G Shipyard Ltd." 20179 663 -.09768464 1101.56 .07366299 4 "A B G Shipyard Ltd." 20209 664 -.3022972 769.47 .12029396 4 "A B G Shipyard Ltd." 20240 665 .2980736 998.83 .3044246 5 "A C C Ltd." 19875 653 . 27561.96 .05712568 5 "A C C Ltd." 19905 654 -.0501005 26181.09 .06022657 5 "A C C Ltd." 19936 655 .069702365 28005.97 .05895481 5 "A C C Ltd." 19967 656 -.05869138 26362.26 .04966506 5 "A C C Ltd." 19997 657 .068902895 28178.7 .03969718 5 "A C C Ltd." 20028 658 -.020354487 27605.14 .033498686 5 "A C C Ltd." 20058 659 -.04825379 26273.09 .04008862 5 "A C C Ltd." 20089 660 .11479922 29289.21 .05381659 5 "A C C Ltd." 20120 661 .0789718 31602.24 .05813709 5 "A C C Ltd." 20148 662 -.07286501 29299.54 .05722007 5 "A C C Ltd." 20179 663 -.078111 27010.92 .08348867 5 "A C C Ltd." 20209 664 .05272128 28434.97 .044722 5 "A C C Ltd." 20240 665 -.04879339 27047.53 .05062318 6 "A I A Engineering Ltd." 19875 653 . 7324.45 .08130153 6 "A I A Engineering Ltd." 19905 654 .005601747 7365.48 .08059192 6 "A I A Engineering Ltd." 19936 655 .12549622 8289.82 .08216862 6 "A I A Engineering Ltd." 19967 656 .07094092 8877.9 .09176414 6 "A I A Engineering Ltd." 19997 657 -.001221806 8867.06 .06304555 6 "A I A Engineering Ltd." 20028 658 .2806085 11355.23 .10604648 6 "A I A Engineering Ltd." 20058 659 -.09942693 10226.21 .09121463 6 "A I A Engineering Ltd." 20089 660 .04819222 10719.04 .11355854 6 "A I A Engineering Ltd." 20120 661 -.004223615 10673.76 .07496843 6 "A I A Engineering Ltd." 20148 662 .10087043 11750.43 .09606886 6 "A I A Engineering Ltd." 20179 663 -.07617597 10855.33 .10489568 6 "A I A Engineering Ltd." 20209 664 -.11560522 9600.4 .09647164 6 "A I A Engineering Ltd." 20240 665 -.01709482 9436.28 .07773334 7 "Aarti Drugs Ltd." 19875 653 . 640.36 .12179978 7 "Aarti Drugs Ltd." 19905 654 .1787929 754.91 .08089033 7 "Aarti Drugs Ltd." 19936 655 .19337867 900.88 .16471484 7 "Aarti Drugs Ltd." 19967 656 .036881723 934.11 .08244735 7 "Aarti Drugs Ltd." 19997 657 -.063984215 874.3 .05832432 7 "Aarti Drugs Ltd." 20028 658 .10558385 966.69 .0699918 7 "Aarti Drugs Ltd." 20058 659 .067942664 1032.31 .08391946 7 "Aarti Drugs Ltd." 20089 660 .4081825 1453.75 .10373413 7 "Aarti Drugs Ltd." 20120 661 .0470447 1522.04 .14374593 7 "Aarti Drugs Ltd." 20148 662 .036975738 1578.35 .14562993 7 "Aarti Drugs Ltd." 20179 663 .011476587 1596.51 .20718463 7 "Aarti Drugs Ltd." 20209 664 .012559962 1616.49 .1337734 7 "Aarti Drugs Ltd." 20240 665 -.05447035 1528.46 .1502725 8 "Aarti Industries Ltd." 19875 653 . 1803.28 .11358362 8 "Aarti Industries Ltd." 19905 654 .05777171 1907.38 .0686042 8 "Aarti Industries Ltd." 19936 655 .2983466 2476.58 .10981288 8 "Aarti Industries Ltd." 19967 656 .002432368 2482.78 .08899546 8 "Aarti Industries Ltd." 19997 657 .08121614 2684.33 .09928838 8 "Aarti Industries Ltd." 20028 658 -.05333335 2541.25 .04957134 8 "Aarti Industries Ltd." 20058 659 -.11825407 2240.48 .08447535 8 "Aarti Industries Ltd." 20089 660 .1230429 2516.45 .06511408 8 "Aarti Industries Ltd." 20120 661 .018025612 2561.63 .1415287 end format %td date format %tm mdate
Code:
by mdate, sort: egen idiovol_decile = xtile(idiovol), nq(10) capture program drop one_weighted_return program define one_weighted_return if !missing(idiovol_decile){ egen numerator = total(mcap*rt) egen denominator = total(mcap) gen vw_mean_rt = numerator/denominator } exit end runby one_weighted_return, by(mdate idiovol_decile) by mdate idiovol_decile, sort: egen ew_mean_rt = mean(rt) keep mdate idiovol_decile *_mean_rt drop if missing(idiovol_decile, vw_mean_rt, ew_mean_rt) by mdate *_decile, sort: gen stock_count = _N by mdate idiovol_decile, sort: keep if _n == 1 gen str32 group = "_idiovol_d" + string(idiovol_d) + "_" drop *_decile reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
a. mean and sd matrix
b. regression intercept, coefficients, t-stat, r-squared, adj r-squared.
We used following codes for this purpose previously but it was 5x5 bivariate sorts portfolios then and now it is univariate deciles. Request you to amend the following codes (code 2 and code 3) so that they are compatible with new requirement.
2. code for means and sd
Code:
frame create means_and_sds int (idiovol_group mcap_group) /// float(mean_vw sd_vw mean_unwtd sd_unwtd) forvalues iv = 1/5 { forvalues mc = 1/5 { summ vw_mean_q`mc'_idiovol_q`iv'_rt local mean_vw = r(mean) local sd_vw = r(sd) summ unwtd_mean_q`mc'_idiovol_q`iv'_rt local mean_unwtd = r(mean) local sd_unwtd = r(sd) frame post means_and_sds (`iv') (`mc') (`mean_vw') (`sd_vw') /// (`mean_unwtd') (`sd_unwtd') } } frame change means_and_sds rename (mean* sd*) =_idiovol_ reshape wide *_idiovol_, i(mcap_group) j(idiovol_group)
Code:
frame create regress_results int (idiovol_group mcap_group) /// float(intercept tstat b_rmrf t_rmrf b_smb t_smb b_hml t_hml b_wml t_wml b_himli t_himli r2 adj_r2) forvalues iv = 1/5 { forvalues mc = 1/5 { local group q`mc'_idiovol_q`iv' regress vw_mean_`group'_rt rmrf smb hml wml himli matrix M = r(table) local topost (`iv') (`mc') (M["b", "_cons"]) (M["t", "_cons"]) foreach x in rmrf smb hml wml himli { local topost `topost' (M["b", "`x'"]) (M["t", "`x'"]) } frame post regress_results `topost' (e(r2)) (e(r2_a)) } } frame change regress_results rename (intercept tstat b_* t_* r2 adj_r2) =_idiovol_ reshape wide *_idiovol_, i(mcap_group) j(idiovol_group)
i. sorts only once in June month to form decile and weighting of vw returns for each month using june month mcap values observed at start of year.
ii. sorts each month on previous idiovol values to form deciles and weighting of vw returns by lagged mcap values of each rt observation.
What should the main code 1 be in case of i and ii.
Comment