WillianFuks / tfcausalimpact

Python Causal Impact Implementation Based on Google's R Package. Built using TensorFlow Probability.
Apache License 2.0
600 stars 72 forks source link

Spike-and-slab prior for regression coefficients #9

Closed IvanUkhov closed 3 years ago

IvanUkhov commented 3 years ago

First off, thank you for the great package!

In the original paper, the authors use a spike-and-slab prior for the regression coefficients. Do I understand correctly that this is currently not the case in TFCausalImpact? If so, are there any plans in this regard? It might be a matter of replacing LinearRegression with SparseLinearRegression:

https://www.tensorflow.org/probability/api_docs/python/tfp/sts/SparseLinearRegression

WillianFuks commented 3 years ago

Hi @IvanUkhov ,

Thanks for this reference :)! I'll research more about it and if the SparseLinearRegression is closer to spike-and-slab I'll replace it soon.

When developing this package I compared several examples from the original R results with TFP using LinearRegression and even though they differed a bit they tended to converge to similar values. After reading about the sparse version it does seem to be more appropriate (maybe it better models some sort of Lasso regression which affects the log-prob after all).

Thanks,

Will

WillianFuks commented 3 years ago

Hi @IvanUkhov ,

Once again thanks for the information about the SparseLinearRegression, it does indeed model the weights in a very similar way as spike-and-slab from R does (this is in fact mentioned in the docstring of the class).

As a side note (for future reference also for those seeing this issue), the original R package does make some assumptions on its SpikeAndSlab implementation, mainly, they've considered that the prior.df (first component of the gamma distribution for the standard deviation of the slab component) is 50, the expected R^2 is 0.8 and model size is set as 3. Internally, those values are manipulated accordingly to set the prior estimation for the sparsity of data and its variance.

For TFP, it uses instead a horseshoe distribution and only parameter we can manipulate is the weights_prior_scale which sets a prior for sparsity estimation as well (it's used when calculating the final values of the weights as a multiplicative term for local and global scales.)

Currently this value has been set as default (0.1). If someone finds a value that better models the assumptions from the original R package please feel free to either let us know or send a PR.

Still, probably final results wouldn't change much so it's more of a side feature nice to have :).

Let me know @IvanUkhov if the newer version of this package solves this issue for you.

Best,

Will

IvanUkhov commented 3 years ago

Thank you! I think it will be beneficial for the package in general. One of the selling points of the paper is that they take care of the variable selection via a prior inducing sparsity, which was not present here in any form until now.