Announcement

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

  • ml command with estimated coefficients (rangestat/runby?)

    Dear All, Given the data set
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Date byte(sell_f buy_f) int(sell_e buy_e)
    13667  9 15  16  22
    13668  5  8   6   5
    13669 10 10  12   2
    13670 14  8  14   7
    13671 19  8  43  11
    13674 19  4  22   0
    13675 11 11  47  25
    13676  9  8  11  20
    13677  4 12  27  26
    13678 12 24   4  35
    13681  9 23  15  36
    13682 11 12  19  11
    13683  7 24  21  18
    13684 22  8  22  10
    13685 19 24   9  12
    13688  9  6  22   8
    13689 19  9  10   6
    13690 10 16  19  10
    13691 16  1  33   9
    13692 15  8  12  25
    13695 12  9  20  15
    13696 20 20  24  31
    13697 11  5   9   9
    13698 12 11  12  13
    13699  9 14  24  26
    13702 15 12  28  14
    13703 11  7  25  30
    13704 12 13  21  26
    13705 14 13  16  15
    13706 16 34  29  85
    13709 18 21  17  55
    13710 26 10  19  22
    13711 14 26   8  49
    13712 17 11  33  44
    13713 11 16   7  36
    13717 14 31  78 155
    13719 25 39 116  76
    13720 18 12  74  38
    13723 22 12  69  16
    13724 14 21  28  59
    13725 16 23  32  67
    13726 17 10  22  26
    end
    format %td Date
    and code as
    Code:
    *    [PIN]
    *
    * Program Instructions
    *
    capture program drop PIN
    program define PIN
    version 7.0
    *
    * Assign names to the log-likelihood function and its parameters 
    *
    args lnf esf ebf ese ebe alpha delta mf me
    *
    * Generate three tempvars so as to divide the expression for PROB(BnE,BnF,SnE,SnF) in three smaller terms
    *
    tempvar expr1 expr2 expr3
    *
    * Define the tempvars and the log-likelihood function
    * [$ML_y(i) is the internal label for the i-th dependent variable]
    *
    quietly  {
    
    gen double `expr1' = ((`esf')^$ML_y1*exp(-`esf') / exp(lnfactorial($ML_y1))* ///
    (`ebf')^$ML_y2*exp(-`ebf') / exp(lnfactorial($ML_y2))*(`ese')^$ML_y3*exp(-`ese') / exp(lnfactorial($ML_y3))* ///
    (`ebe')^$ML_y4*exp(-`ebe') / exp(lnfactorial($ML_y4)))
    
    gen double `expr2' =((`esf'+`mf')^$ML_y1*exp(-`esf'-`mf') / exp(lnfactorial($ML_y1))* ///
    (`ebf')^$ML_y2*exp(-`ebf') / exp(lnfactorial($ML_y2))*(`ese'+`me')^$ML_y3*exp(-`ese'-`me') / exp(lnfactorial($ML_y3))* ///
    (`ebe')^$ML_y4*exp(-`ebe') / exp(lnfactorial($ML_y4)))
    
    gen double `expr3' =((`esf')^$ML_y1*exp(-`esf') / exp(lnfactorial($ML_y1))*(`ebf'+`mf')^$ML_y2*exp(-`ebf'-`mf') / exp(lnfactorial($ML_y2))* ///
    (`ese')^$ML_y3*exp(-`ese') / exp(lnfactorial($ML_y3))*(`ebe'+`me')^$ML_y4*exp(-`ebe'-`me') / exp(lnfactorial($ML_y4)))
    
    replace `lnf'= ln((1-(`alpha'))*(`expr1')+(`alpha')*(`delta')*(`expr2')+(`alpha')*(1-(`delta'))* (`expr3'))
    
    }
    *
    * End the program 
    *
    end
    *
    * Access Data, replacing "<DATA FILE PATH>" with the path to access the Ch5_ex4_data file in your PC, e.g. C:\Documents\Data\Ch5_ex4_data.dta
    *
    import excel "Data_Sets_Chapter_5_data_ex4.xls", sheet("Ch5_ex4_data") firstrow clear
    *use "Data_Sets_Chapter_5_data_ex4.dta", clear
    *
    * Generate the log file, replacing "<RESULT FILE PATH>" with the path to create the output file in your PC, e.g. C:\Documents\Ch5_ex4_results
    *
    cap log close
    log using "Data_Sets_Chapter_5_data_ex4.log", replace
    *
    * Define the parameters and the dependent variable
    *
    ren (sell_f buy_f sell_e buy_e) (sellf buyf selle buye)
    ml model lf PIN (esf: sellf=) (ebf: buyf=) (ese: selle=) (ebe: buye=) (alfa:) (delta:) (mf:) (me:)
    *
    * Set initial values
    *
    ml init 20 20 20 20 0.5 0.5 20 20, copy
    *
    * Maximize the log-likelihood function with respect to the eight parameters to be estimated
    *
    ml maximize
    *
    log close
    The estimated results are
    Code:
    . ml maximize
    
    initial:       log likelihood = -1155.1648
    rescale:       log likelihood = -1155.1648
    rescale eq:    log likelihood = -912.23463
    Iteration 0:   log likelihood = -912.23463  
    Iteration 1:   log likelihood = -875.96779  
    Iteration 2:   log likelihood = -875.87954  
    Iteration 3:   log likelihood = -870.40346  
    Iteration 4:   log likelihood = -870.28267  
    Iteration 5:   log likelihood =  -870.2826  
    
                                                    Number of obs     =         42
                                                    Wald chi2(0)      =          .
    Log likelihood =  -870.2826                     Prob > chi2       =          .
    
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    esf          |
           _cons |   13.39787   .5764053    23.24   0.000     12.26814    14.52761
    -------------+----------------------------------------------------------------
    ebf          |
           _cons |   12.72771   .5905815    21.55   0.000     11.57019    13.88522
    -------------+----------------------------------------------------------------
    ese          |
           _cons |   21.51544   .7463844    28.83   0.000     20.05255    22.97833
    -------------+----------------------------------------------------------------
    ebe          |
           _cons |   20.32264    .745875    27.25   0.000     18.86075    21.78453
    -------------+----------------------------------------------------------------
    alfa         |
           _cons |   .2147634   .0634616     3.38   0.001     .0903809    .3391458
    -------------+----------------------------------------------------------------
    delta        |
           _cons |   .3344657   .1574142     2.12   0.034     .0259396    .6429919
    -------------+----------------------------------------------------------------
    mf           |
           _cons |   11.61024   1.722019     6.74   0.000     8.235147    14.98534
    -------------+----------------------------------------------------------------
    me           |
           _cons |    60.1767   3.096163    19.44   0.000     54.10833    66.24506
    ------------------------------------------------------------------------------
    The problem is that:
    1. How can I store the estimated coefficients as variables in the data set.
    2. In case I need to estimate the model by groups (like -statsby-), any suggestions? Thanks a lot.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)
Working...
X