Announcement

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

  • Normalize variable as an expanding window

    Hi,

    I would like to normalize the price of the following as an expanding window taking into account all information up to time t (but not after t, to avoid look-ahead bias). So far, I have the code that takes into account the entire dataset, and hence, induces a look-ahead bias. Can someone help me edit the code? Thanks

    Code:
    clear
    local ticker "BTC-USD"
    getsymbols `ticker', fm(1) fd(1) fy(2012) lm(12) frequency(d) price(adjclose) yahoo clear
    keep period p_adjclose_BTC_USD
    
    * Normalization:
    local To_Norm p_adjclose_BTC_USD
    foreach var in `To_Norm' {
        sum `var', meanonly 
        gen n_`var' = (`var' - r(min)) / (r(max) - r(min)) 
    }

  • #2
    This example uses rangestat from SSC.

    Code:
    webuse grunfeld , clear
    keep if company == 1
    rangestat (min) invest (max) invest , int(year . 0)
    gen wanted = (invest - invest_min) / (invest_max - invest_min)
    
    l invest* wanted
    However, the essence here is wanting the maximum and minimum so far. In another terminology, these are records as in sports, and there is an FAQ from 2006 on that.

    Code:
    . search record, faq
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    FAQ     . . . . . . . . . .  Calculating the maximum and minimum of a sequence
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            10/06   How do I calculate the maximum or minimum seen
                    so far in a sequence?
                    http://www.stata.com/support/faqs/data-management/
                    maximum-and-minimum-of-sequence/


    Last edited by Nick Cox; 23 May 2023, 01:10.

    Comment


    • #3
      Thanks a lot Nick!

      Comment

      Working...
      X