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

InvalidArgumentError: Cholesky decomposition was not successful. The input might not be valid. [Op:Cholesky] #32

Closed ppkhairn closed 2 years ago

ppkhairn commented 2 years ago

Hi Will, I got the following error while reproducing your results from the medium post.

import tensorflow_probability as tfp from causalimpact.misc import standardize

normed_data, mu_sig = standardize(data) obs_data = normed_data['BTC-USD'].loc[:'2020-10-14'].astype(np.float32) design_matrix = pd.concat(

).astype(np.float32).iloc[:, 1:] linear_level = tfp.sts.LocalLinearTrend(observed_time_series=obs_data) linear_reg = tfp.sts.LinearRegression(design_matrix=design_matrix) month_season = tfp.sts.Seasonal(num_seasons=4, num_steps_per_season=1, observed_time_series=obs_data, name='Month') year_season = tfp.sts.Seasonal(num_seasons=52, observed_time_series=obs_data, name='Year') model = tfp.sts.Sum([linear_level, linear_reg, month_season, year_season], observed_time_series=obs_data)

pre_period = ['2018-01-03', '2020-10-14'] post_period = ['2020-10-21', '2020-11-25']

ci = CausalImpact(data, pre_period, post_period, model=model)

Output:

WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass.

InvalidArgumentError Traceback (most recent call last)

in () ----> 1 ci = CausalImpact(data, pre_period, post_period, model=model, model_args={'prior_level_sd': 0.1}) 9 frames in __init__(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/six.py in raise_from(value, from_value) **InvalidArgumentError: Cholesky decomposition was not successful. The input might not be valid. [Op:Cholesky]** Interestingly, when I don't use the year-season component in the model (tf.sts.sum) I don't get an error. Can you please help me regarding this issue? Thank you in advance.
WillianFuks commented 2 years ago

Hi @ppkhairn ,

Just tried running the example again but I'm getting the error "RemoteDataError: No data fetched using 'YahooDailyReader'", not sure if their API is down for now.

Maybe it's something wrong in the input data, not sure. I'll try again a bit later to see if works again.

ppkhairn commented 2 years ago

Hi Will,

Thanks for your reply.

I got around "RemoteDataError: No data fetched using 'YahooDailyReader'" error by doing this:

import pandas from pandas_datareader import data as pdr import yfinance as yfin import datetime yfin.pdr_override()

df = pdr.get_data_yahoo("TSLA", start="2018-1-1", end="2020-12-2")

btc_data = pdr.get_data_yahoo(['BTC-USD'], start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2020, 12, 3))['Close']

I also got rid of the 'Cholesky decomposition' error by upgrading tensorflow_probability to 0.14.1 I used - pip install -U tfcausalimpact which solved the issue. I was able to reproduce your results.

Thank you and I will let you know if I have more questions.

WillianFuks commented 2 years ago

That's good to know :)

If you find any issues please let me know.

Best,

Will