1
$\begingroup$

I have a whole set of data on [0,T] with an observation variable y(t), and a feature x(t), the two being univariates with no missing data.

For a given period [t, t+h], I am applying a dynamic linear regression:

y(t) = a(t) + b(t) * x(t) + w(t)   
a(t) = a(t-1) + w_a(t)
b(t) = b(t-1) + w_b(t)

Where w, w_a, w_b are the variance of the last term on these 3 lines (following a centered normal distribution).

What matters is that for window [0, h], I normalize my data (x, y) - aka zero mean and 1 std - and then will have a series of estimated parameters a and b after maximum likehood estimation:

time   a     b
   0   0     0
   1   0.41  0.72
   ...
   h-1 0.432 0.55
   h   0.435 0.567

I am interested in the last couple a(h), b(h) giving the intercept and the slope.

Then I take as training set x(1), x(2) ... x(h+1) and y(1), y(2), .., y(h+1), normalize thse two series, and have as well some best estimations for the parameters a and b. I take parameters a(h+1) and b(h+1).

And so on. By rolling the window, I thus have a set of slopes b(h), b(h+1), ..., b(T) and intercept a(h), a(h+1), ..., a(T)

However the normalization differs between each rolling windows. So that I am not sure at all it is a good way to deduce some behaviour about the time series a and b of the "last estimation".

What would be the best method:

  • still perform some analysis on a and b timeseries despite (x,y) normalisation is different on each window?

  • apply the above by normalizing the data on each window [t, t+h] with the means/std of y and x on [0, T]?

  • applying the above without normalizing the data?
$\endgroup$
  • $\begingroup$ What do you mean by "normalize"? Are a(t) and b(t) OLS estimates based on $\{ (y_i,x_i), i \le t \}$, or something else? What's the purpose of this whole exercise? $\endgroup$ – StasK Jul 24 '17 at 18:30
  • $\begingroup$ By normalize I mean that I take each dataset on the window [t, t+h], where t goes over [0,T-h] and I retrieve the mean/divide per the std on each window. I edited the question since you're right, it's unclear. $\endgroup$ – Colonel Beauvel Jul 25 '17 at 8:14
  • $\begingroup$ Why do you want/need to normalize? For situations like these, where you need to change the sample, it seems really counterproductive. (May be you can go back and edit again :) ). $\endgroup$ – StasK Jul 25 '17 at 19:05
  • $\begingroup$ I should have mentionned that if I do not normalize, there are some numerical errors in optim in R dlm package. Indeed Y and X have different magnitude (Y around 100000 and X around 300). $\endgroup$ – Colonel Beauvel Jul 26 '17 at 7:46
  • $\begingroup$ Normalize by a fixed constant, then. Divide Y by 100000 and X by 300. $\endgroup$ – StasK Aug 2 '17 at 2:35
0
$\begingroup$
  zscore <- function(stock){
      # 30 day moving average 
      mavg_30 = (filter(stock, rep(1/30, 30), sides=1))
      # current value
      current = tail(stock, 1)
      # 30 day standard deviation
      std_30 = rollapply(stock, width = 30, FUN = sd)
      if(std_30 > 0){
        return((current - mavg_30)/std_30 )
        }
      }

this should do it

    zscore <- function(stock){
     # 30 day moving average
     mavg_30 = rollapply(stock, width=30, FUN=mean)
     # current value
     current = head(stock, 1)
     # 30 day standard deviation
     std_30 = rollapply(stock, width = 30, FUN = sd)
       if(std_30 > 0){
         return((current - mavg_30)/std_30 )
     }
    }

this would be another way

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.