Hi Team,
I’d like to model relationships between stocks by using Dynamic Linear Models.
Let’s assume we have historical price data for stock X and Y and the relationship between daily returns of the stocks is defined by:
Y_returns = alpha(t) + beta(t) * X_returns + noise
Average alpha and beta over whole period can be easily calculated with simple linear regression, but I’d like to see how it changes dynamically (assuming that it changes relatively slow).
Can you advise how to construct such a model and retrieve dynamic alpha and beta from it?
Regards,
Vladimir
That’s awesome! Many thanks Junpeng!
It looks like the model perfectly estimates alpha and beta at the beginning and starts to deviate at the end of the curve.
I replaced alpha and beta with deterministic functions and got similar results (https://colab.research.google.com/drive/1oJrLJkNlQD95C1co_cg5pxkQMC48b0oR?usp=sharing).
Do you have an idea what can possible lead to this deviation?
Regards,
Vladimir
Hi Junpeng,
Fantastic! The new model is much better! Thank you very much for your help!
As far as I understand in both examples you are using forward+backward smoothing (with tfd.MultivariateNormalFullCovariance).
Can you advise how to implement forward only smoothing for the model?
BTW with forward only smoothing would it be correct to say that for any estimation of alpha(t) and beta(t) the model using only information from the past (e.g. can be used for the back test)?
Regards,
Vladimir
From: Junpeng Lao <junpe...@gmail.com>
Sent: Sunday, November 14, 2021 5:29 PM
To: TensorFlow Probability <tfprob...@tensorflow.org>
Cc: vol...@gmail.com <vol...@gmail.com>; Junpeng Lao <junpe...@gmail.com>
Subject: Re: Dynamic Linear Models for modeling Alpha and Beta of the Stock
I think the model is slightly unidentifiable, which is why the part that alpha is underestimate beta is over-estimate, and vice versa:
> Can you advise how to implement forward only smoothing for the model?
There are some example and explanation in the docstring you can check out:
> BTW with forward only smoothing would it be correct to say that for any estimation of alpha(t) and beta(t) the model using only information from the past (e.g. can be used for the back test)?
Exactly - posterior marginal is basically running the backward filter after you get the result from forward filter: https://github.com/tensorflow/probability/blob/76eaa7e1fd949c4796d2da70143f35c82f37c465/tensorflow_probability/python/distributions/linear_gaussian_ssm.py#L1055-L1061
Hi Junpeng,
Thank you again this is exciting!
Basically I am trying to implement the example with Capital Asset Pricing Model (CAPM), described in the book of Petris et al. (section 3.3.3; p. 101)which is available for download from:
https://www.researchgate.net/publication/226410454_Dynamic_Linear_Models_with_R (2009)
The result would be to get a graph for alpha and beta like the one shown in the book:
And the next steps would be to go a bit further. The example in the book makes an assumption that the alpha is time-invariant. So I am trying to see if it works for varying alpha as well.
In your approach you feed the model with absolute values of X and Y but the example in the book operates with the monthly returns (from the table https://raw.githubusercontent.com/rstudio/ai-blog/main/_posts/2019-06-25-dynamic_linear_models_tfprobability/data/capm.txt).
I’ve found a couple of implementations of the model - one is in R and another is in TF but it’s not complete:
https://blogs.rstudio.com/ai/posts/2019-06-25-dynamic_linear_models_tfprobability/
https://github.com/tensorflow/probability/issues/1068
I am using the following code to feed your model:
capm_data = pd.read_fwf(capm_path)
dep_var_ts = (capm_data["IBM"] - capm_data["RKFREE"]).values
ind_var_ts = (capm_data["MARKET"] - capm_data["RKFREE"]).values
num_steps = len( ind_var_ts )
x = tf.constant( np.float32( np.reshape(ind_var_ts, (-1,1)) ) )
y = tf.constant( np.float32( np.reshape(dep_var_ts, (-1,1)) ) )
Can you advise what needs to be change in your model to operate with the monthly returns to get a graph like in the book (ideally for both alpha and beta)?
Hi Junpeng,
I agree with you that using monthly vs. daily data should not make any significant difference for the purpose of the exercise.
The main difference in the example from the book is that they are using returns (relative changes) (x(t)/x(t-1) – 1) instead of prices of x and (y(t)/y(t-1) – 1) instead of prices of y in the model.
I believe some changes in your model are still needed to accommodate the new data.
With the CAPM data, without the changes the model produces very different results:
https://colab.research.google.com/drive/1hZC8FqwHpkwRaFCtYH0Ragy6UToQJS98?usp=sharing
compare to the ones shown in the book (see Beta for IBM):