Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #46
    Thank you so much
    one more thing, how to modify that code so present the Betas for each portfolio, as a mean estimate of the entire period so that each portfolio will have a table that present a mean estimate of the betas for each portfolio
    that would give one table of 5x5
    and also I want to be able to modify the period, not just taking the entire sample, as in code #34

    Comment


    • #47
      and also I've tried this code, and I have this error msg
      table me_level bv_level if !missing(me_level, bv_level), c(mean(beta_cf)) format(%4.3f)
      mean(beta_cf) invalid or requires argument
      r(198);

      Comment


      • #48
        Sorry, my mistake. the code should be
        Code:
         
        table me_level bv_level if !missing(me_level, bv_level), c(mean beta_cf) format(%4.3f)
        and also I want to be able to modify the period, not just taking the entire sample, as in code #34
        Just follow the example you see in #34: use an -if- clause with the -inrange()- function and appropriate date constants. In your example data, your date variable is daily rather than monthly, so the date constants would look like -td(30mar2020)-.
        Last edited by Clyde Schechter; 30 Mar 2020, 18:37.

        Comment


        • #49
          I have no idea how to thank you, thank you so much, I've been stuck in here for several months to do these two tables, for these two types of portfolios
          I know that I already asked you too much, but can I ask my professor what to do with the RISK portfolios and get back to you
          you saved me a lot of time and your help is extremely appreciated
          this work is part of my Dissertation, I'm a PhD student

          Comment


          • #50
            one question, How can I calculate these betas for each month instead for each year, How to modify code #27,

            gen int year = year(dofm(mdate))
            gen int month = month(dofm(mdate))
            //GO TO LONG LAYOUT

            rename (FFS* RISK*) return=
            reshape long return, i(mdate) j(portfolio) string

            capture program drop betas
            program define betas
            tsset mdate
            gen delta = N_cf - N_dr
            summ delta
            local denom = r(Var)
            corr return N_cf, cov
            local cf1 = r(cov_12)
            corr return L1.N_cf, cov
            local cf2 = r(cov_12)
            gen beta_cf = (`cf1' + `cf2')/`denom'
            corr return N_dr, cov
            local dr1 = r(cov_12)
            corr return L1.N_dr, cov
            local dr2 = r(cov_12)
            gen beta_dr = (`dr1' + `dr2')/`denom'
            drop delta
            gen beta = beta_dr + beta_cf
            exit
            end
            //ssc install runby: run loops over data by groups
            runby betas, by(month portfolio)

            the data just disappeared ,

            Comment


            • #51
              It is impossible to do this by month. The problem is that when you restrict your analysis to a single month, there are no lagged observations, so the attempt to calculate a correlation between return and L1.N_cf throws an error. With -runby-, any of the by-groups that generates an error contributes no data to the final output, and since this lagged correlation fails in every group, you have no output at all.

              Basically, because this problem relies on lagged observations, it cannot be done on single dates.

              Comment


              • #52
                okay there is another specification for the betas :
                rename (FFS* RISK*) return=
                reshape long return, i(mdate) j(portfolio) string

                capture program drop betas
                program define betas
                tsset mdate
                gen delta = R_Me- L1.EtR_Me
                summ delta
                local denom = r(Var)
                corr return N_cf, cov
                local cf1 = r(cov_12)
                gen beta_cf = (`cf1')/`denom'
                corr return N_dr, cov
                local dr1 = r(cov_12)
                gen beta_dr = (`dr1')/`denom'
                drop delta
                gen beta = beta_dr + beta_cf
                exit
                end
                //ssc install runby
                runby betas, by( year portfolio)

                Then this would work? if we ignore the lagged observations ?
                because there are 2 specifications for estimating these two betas. and this is the other specification

                Comment


                • #53
                  It will run on a yearly basis. But it still will not work on a monthly basis because -
                  gen delta = R_Me- L1.EtR_Me- still uses a non-existent lag. Therefore, the -summ delta- command will fail because there are no observations.

                  If you are restricting the analysis to a single month, there can be no references to any time-series operations whatsoever anywhere in program betas.

                  Comment


                  • #54
                    gen int month = month(dofm(mdate))

                    //GO TO LONG LAYOUT
                    rename (FFS* RISK*) return=
                    reshape long return, i(mdate) j(portfolio) string

                    capture program drop betas
                    program define betas
                    tsset mdate, delta(12)
                    gen delta = R_Me- EtR_Me
                    summ delta
                    local denom = r(Var)
                    corr return N_cf, cov
                    local cf1 = r(cov_12)
                    gen beta_cf = (`cf1')/`denom'
                    corr return N_dr, cov
                    local dr1 = r(cov_12)
                    gen beta_dr = (`dr1')/`denom'
                    drop delta
                    gen beta = beta_dr + beta_cf
                    exit
                    end
                    runby betas, by( month portfolio)

                    I have tried this one, however, it is calculating for each January of each year

                    Comment


                    • #55
                      I'm sorry this code calculated the betas for each month only for 1929,

                      Comment


                      • #56
                        When I run the code in #54 using the data from #20, it runs without errors and calculates betas for all observations. I cannot replicate the problem you describe in #55: you must be doing something different, or there is some problem in your data that is not reflected in the code you show and the example data we have been using up to now.

                        And this makes sense because #54 is different from #50 in some crucial ways. First, it does not include any lags, which were how things fell apart previously. But, actually, having specified -delta(12)- in your -tsset- command, you could actually re-introduce the Lag operator into the code, because with -delta(12)-, the Lag refers to the observation from the same calendar month in the preceding year, and you have enough different years' data to leave you with an adequate estimation sample for the correlations and covariances.

                        Comment


                        • #57
                          If you run code #54 and then duplicates drop beta_cf beta_dr, force
                          you will see that it only calculated betas for actually the first 12 months only

                          Comment


                          • #58
                            OK, but in #54 where you raise this issue there is no mention of -duplicates drop beta_cf beta_dr, force-.

                            And, yes, of course that's what happens. When you force drop observations with the same value of beta_cf and beta_dr, then you are telling Stata to only retain one observation for each of the 12 months of the calendar. The way -duplicates drop- chooses which ones to keep that could well result in retaining only the first year's worth of observations. So why are you telling stata to -duplicates drop beta_cf beta_dr, force-? What is it you want to accomplish with that command?

                            Comment


                            • #59
                              hello, I modified the code, that I can calculate the betas for each portfolio for the entire sample period and subsample periods. However, how can I switch between periods without doing that manually,
                              here's the code :
                              //////////////////////////////////////////
                              gen byte period = (mdate <= tm(1963m7))
                              drop if period==1
                              foreach var of varlist FFS1BM1 FFS1BM2 FFS1BM3 FFS1BM4 FFS1BM5 FFS2BM1 FFS2BM2 FFS2BM3 FFS2BM4 FFS2BM5 FFS3BM1 FFS3BM2 FFS3BM3 FFS3BM4 FFS3BM5 FFS4BM1 FFS4BM2 FFS4BM3 FFS4BM4 FFS4BM5 FFS5BM1 FFS5BM2 FFS5BM3 FFS5BM4 FFS5BM5{
                              gen delta = N_cf + N_dr
                              summ delta
                              local denom = r(Var)
                              corr `var' N_cf, covariance
                              local cf1 = r(cov_12)
                              corr `var' L1.N_cf, covariance
                              local cf2 = r(cov_12)
                              scalar beta_cf_`var' = (`cf1' + `cf2')/`denom'
                              corr `var' N_dr, covariance
                              local dr1 = r(cov_12)
                              corr `var' L1.N_dr, covariance
                              local dr2 = r(cov_12)
                              scalar beta_dr_`var' = (`dr1' + `dr2')/`denom'
                              drop delta
                              scalar beta_`var' = beta_dr_`var' + beta_cf_`var'
                              }
                              ///////////////////////////////////////
                              ///////////////////////////////////////
                              matrix BM_DR = J(6, 6, .)
                              matrix colnames BM_DR = "Growth" "2" "3" "4" "Value" "Diff"
                              matrix rownames BM_DR = "Small" "2" "3" "4" "Large" "Diff"
                              ///////////////////////////////////////////////////////
                              matrix BM_DR[1, 1] = beta_dr_FFS1BM1
                              matrix BM_DR[1, 2] = beta_dr_FFS1BM2
                              matrix BM_DR[1, 3] = beta_dr_FFS1BM3
                              matrix BM_DR[1, 4] = beta_dr_FFS1BM4
                              matrix BM_DR[1, 5] = beta_dr_FFS1BM5
                              matrix BM_DR[1, 6] = beta_dr_FFS1BM5 - beta_dr_FFS1BM1
                              matrix BM_DR[6, 1] = beta_dr_FFS5BM1 - beta_dr_FFS1BM1

                              matrix BM_DR[2, 1] = beta_dr_FFS2BM1
                              matrix BM_DR[2, 2] = beta_dr_FFS2BM2
                              matrix BM_DR[2, 3] = beta_dr_FFS2BM3
                              matrix BM_DR[2, 4] = beta_dr_FFS2BM4
                              matrix BM_DR[2, 5] = beta_dr_FFS2BM5
                              matrix BM_DR[2, 6] = beta_dr_FFS2BM5 - beta_dr_FFS2BM1
                              matrix BM_DR[6, 2] = beta_dr_FFS5BM2 - beta_dr_FFS1BM2

                              matrix BM_DR[3, 1] = beta_dr_FFS3BM1
                              matrix BM_DR[3, 2] = beta_dr_FFS3BM2
                              matrix BM_DR[3, 3] = beta_dr_FFS3BM3
                              matrix BM_DR[3, 4] = beta_dr_FFS3BM4
                              matrix BM_DR[3, 5] = beta_dr_FFS3BM5
                              matrix BM_DR[3, 6] = beta_dr_FFS3BM5 - beta_dr_FFS3BM1
                              matrix BM_DR[6, 3] = beta_dr_FFS5BM3 - beta_dr_FFS1BM3

                              matrix BM_DR[4, 1] = beta_dr_FFS4BM1
                              matrix BM_DR[4, 2] = beta_dr_FFS4BM2
                              matrix BM_DR[4, 3] = beta_dr_FFS4BM3
                              matrix BM_DR[4, 4] = beta_dr_FFS4BM4
                              matrix BM_DR[4, 5] = beta_dr_FFS4BM5
                              matrix BM_DR[4, 6] = beta_dr_FFS4BM5 - beta_dr_FFS4BM1
                              matrix BM_DR[6, 4] = beta_dr_FFS5BM4 - beta_dr_FFS1BM4

                              matrix BM_DR[5, 1] = beta_dr_FFS5BM1
                              matrix BM_DR[5, 2] = beta_dr_FFS5BM2
                              matrix BM_DR[5, 3] = beta_dr_FFS5BM3
                              matrix BM_DR[5, 4] = beta_dr_FFS5BM4
                              matrix BM_DR[5, 5] = beta_dr_FFS5BM5
                              matrix BM_DR[5, 6] = beta_dr_FFS5BM5 - beta_dr_FFS5BM1
                              matrix BM_DR[6, 5] = beta_dr_FFS5BM5 - beta_dr_FFS1BM5
                              matrix list BM_DR, format(%4.2f)
                              /////////////////////////////////

                              Comment


                              • #60
                                Hello there, I have a question. I have a panel data set and would like to calculate the beta on a daily basis. I tried different codes but it doesn't work. I defined the data set as a panel and calculated the COV (daily_tot_return, Value_weighted_return)/ Var (Value_weigthed_return). I used the code here in the forum but all my data gets deleted after the runby command and the beta column does not show anything. Does anybody know how to solve this problem?
                                My used code has been:
                                capture program drop one_day
                                program define one_day
                                tsset tic_1 datadate, daily
                                local denom = r(var)
                                corr ValueWeightedReturnincldivi, cov
                                local nom = r(cov_12)
                                corr ValueWeightedReturnincldivi daily_tot_return, cov
                                gen beta = `nom'/`denom'
                                exit
                                runby one_day, by (tic_1, data date)

                                Attached Files

                                Comment

                                Working...
                                X