### Install pandas-datareader Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Installs the pandas-datareader library, redirecting output to /dev/null. ```python ! pip install pandas-datareader > /dev/null ``` -------------------------------- ### Import Libraries and Setup Environment Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Imports necessary libraries and configures the environment for Causal Impact analysis. Sets TensorFlow logging level and warning filters. ```python %matplotlib inline import sys import os import warnings import logging logging.getLogger('tensorflow').setLevel(logging.FATAL) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' warnings.filterwarnings("ignore") sys.path.append(os.path.abspath('../')) import numpy as np import pandas as pd import tensorflow as tf import tensorflow_probability as tfp import matplotlib.pyplot as plt from causalimpact import CausalImpact tfd = tfp.distributions plt.rcParams['figure.figsize'] = [15, 10] ``` -------------------------------- ### Install statsmodels Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Installs the statsmodels library, redirecting output to /dev/null to keep the console clean. ```python ! pip install statsmodels > /dev/null ``` -------------------------------- ### Install tfcausalimpact Source: https://github.com/willianfuks/tfcausalimpact/blob/master/README.md Install the tfcausalimpact package using pip. Ensure you have the required Python version and dependencies like TensorFlow and pandas. ```bash pip install tfcausalimpact ``` -------------------------------- ### Initialize CausalImpact model with pre- and post-intervention periods Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Instantiate the CausalImpact model with the time series data and defined pre-intervention and post-intervention periods. The periods are specified as lists of start and end dates. ```python pre_period=['2018-01-03', '2020-10-14'] post_period=['2020-10-21', '2020-11-25'] ci = CausalImpact(data, pre_period, post_period) ``` -------------------------------- ### Generate Sample Data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb This comment indicates that the following code block is intended to generate sample data, referencing an example from Google's R package for Causal Impact. ```python # This is an example presented in Google's R code. ``` -------------------------------- ### Basic Causal Impact Analysis Source: https://github.com/willianfuks/tfcausalimpact/blob/master/README.md Perform a basic causal impact analysis using sample data. This example loads data, defines pre and post intervention periods, initializes CausalImpact, and prints summary reports. It also generates a plot visualizing the results. ```python import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('https://raw.githubusercontent.com/WillianFuks/tfcausalimpact/master/tests/fixtures/arma_data.csv')[['y', 'X']] data.iloc[70:, 0] += 5 pre_period = [0, 69] post_period = [70, 99] ci = CausalImpact(data, pre_period, post_period) print(ci.summary()) print(ci.summary(output='report')) ci.plot() ``` -------------------------------- ### Model Weekly Seasonality in Daily Data Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt This example demonstrates how to incorporate weekly seasonality into the causal impact model for daily data. It uses `nseasons=7` to model 7 distinct seasonal periods within a week, with each season spanning one day. ```python import pandas as pd from causalimpact import CausalImpact # Daily data with weekly seasonality data = pd.read_csv('daily_data.csv') data = data.set_index(pd.date_range(start='20200101', periods=len(data))) pre_period = ['20200101', '20200311'] post_period = ['20200312', '20200410'] # Model weekly seasonality on daily data (7 seasons, each spanning 1 day) ci = CausalImpact( data, pre_period, post_period, model_args={'nseasons': 7} ) print(ci.summary()) ci.plot() # For hourly data with weekly seasonality: ``` -------------------------------- ### Custom TensorFlow Probability Models Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Provides an example of using a custom TensorFlow Probability StructuralTimeSeries model with CausalImpact for advanced use cases. Data must be standardized when using custom models with standardize=True. ```python import numpy as np import pandas as pd import tensorflow_probability as tfp from causalimpact import CausalImpact from causalimpact.misc import standardize # Load and prepare data data = pd.read_csv('data.csv').astype(np.float32) pre_period = [0, 69] post_period = [70, 99] # Standardize data for custom model (required when using custom models with standardize=True) normed_data, (mu, sig) = standardize(data) obs_data = normed_data.iloc[:70, 0] # Pre-intervention response variable ``` -------------------------------- ### Causal Impact Analysis with DateTime Index Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt This example shows how to use CausalImpact with a pandas DataFrame that has a datetime index. Periods can be specified using date strings, and the library automatically handles the datetime indexing for analysis. ```python import pandas as pd from causalimpact import CausalImpact # Load data and set datetime index data = pd.read_csv('comparison_data.csv', index_col=['DATE']) # Specify periods using date strings pre_period = ['2019-04-16', '2019-07-14'] post_period = ['2019-07-15', '2019-08-01'] # Run analysis - the library automatically handles datetime indexing ci = CausalImpact(data, pre_period, post_period) print(ci.summary()) # Output includes: # Posterior Inference {Causal Impact} # Average Cumulative # Actual 78574.42 1414339.5 # Prediction (s.d.) 79282.92 (727.48) 1427092.62 (13094.72) # 95% CI [77849.5, 80701.18][1401290.94, 1452621.31] # ... ``` -------------------------------- ### Helper Function to Get Parameter Index Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb A utility function to find the index of a parameter by its name within a model's parameter list. Used when accessing samples from HMC fits. ```python def get_param_index(model, name): for i, v in enumerate(model.parameters): if v.name == name: return i ``` -------------------------------- ### Extracting Sparse Linear Regression Weights (HMC) Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Calculates the mean of the weights for the SparseLinearRegression component when the model is fitted using the HMC method. It uses a helper function to get parameter indices. ```python tf.reduce_mean(ci.model.components_by_name['SparseLinearRegression/'].params_to_weights( ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_global_scale_variance')], ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_global_scale_noncentered')], ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_local_scale_variances')], ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_local_scales_noncentered')], ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_weights_noncentered')], ), axis=0) ``` -------------------------------- ### Get Parameter Index for HMC Method Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt A helper function to find the index of a specific parameter within a model's parameter list, used when working with the HMC method in TF Causal Impact. ```python def get_param_index(model, name): for i, v in enumerate(model.parameters): if v.name == name: return i return None ``` -------------------------------- ### Initialize Causal Impact Model Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initialize the CausalImpact model with data and pre/post intervention periods. Ensure data is properly formatted. ```python pre_period = [0, 69] post_period = [70, 99] ci = CausalImpact(data, pre_period, post_period) ``` -------------------------------- ### Initialize CausalImpact with Hourly Data Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Initializes CausalImpact with hourly data, specifying the pre- and post-intervention periods and model arguments for seasonality. ```python import pandas as pd hourly_data = pd.read_csv('hourly_data.csv') hourly_data = hourly_data.set_index(pd.date_range(start='20200101', periods=len(hourly_data), freq='H')) ci_hourly = CausalImpact( hourly_data, ['20200101 00:00:00', '20200311 23:00:00'], ['20200312 00:00:00', '20200410 23:00:00'], model_args={'nseasons': 7, 'season_duration': 24} ) ``` -------------------------------- ### Initialize CausalImpact Model Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initializes the CausalImpact model with specified pre and post-intervention periods. The 'vi' fit method is used for faster fitting. ```python pre_period = [str(np.min(data.index.values)), "2015-09-13"] post_period = ["2015-09-20", str(np.max(data.index.values))] ci = CausalImpact(data.iloc[:, 0], pre_period, post_period, model_args={'nseasons': 52, 'fit_method': 'vi'}) ``` -------------------------------- ### Generate Summary Output Methods Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Demonstrates how to generate summary outputs from a CausalImpact analysis, including a concise table (default) and a detailed report. Control decimal precision using the 'digits' argument. ```python import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('data.csv') ci = CausalImpact(data, [0, 69], [70, 99]) # Concise summary table (default) print(ci.summary()) # Detailed narrative report print(ci.summary(output='report')) # Control decimal precision print(ci.summary(digits=4)) ``` -------------------------------- ### Fetch and preprocess Bitcoin data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Fetches Bitcoin ('BTC-USD') closing prices from Yahoo Finance for a specified date range. It then removes duplicates, sets the 'Date' as the index, resamples to daily frequency, and fills missing values. ```python btc_data = pdr.get_data_yahoo(['BTC-USD'], start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2020, 12, 3))['Close'] btc_data = btc_data.reset_index().drop_duplicates(subset='Date', keep='last').set_index('Date').sort_index() btc_data = btc_data.resample('D').fillna('nearest') ``` -------------------------------- ### Import necessary libraries Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Imports pandas-datareader for fetching financial data and datetime for date manipulation. ```python import pandas_datareader as pdr import datetime ``` -------------------------------- ### Initialize CausalImpact with Date Index Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Set the index of your data to a date range and define pre/post intervention periods for CausalImpact analysis. Ensure periods are specified as strings representing dates. ```python dated_data = data.set_index(pd.date_range(start='20200101', periods=len(data))) pre_period = ['20200101', '20200311'] post_period = ['20200312', '20200409'] figsize = (20, 6) ci = CausalImpact(dated_data, pre_period, post_period) ``` -------------------------------- ### Initialize CausalImpact with different periods Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Re-initialize the CausalImpact model with a different set of pre- and post-intervention periods to analyze alternative scenarios or timeframes. ```python pre_period=['2018-01-03', '2018-12-05'] post_period=['2018-12-12', '2019-02-06'] ci = CausalImpact(data, pre_period, post_period) ``` -------------------------------- ### Print model sample statistics Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Iterate through the model's posterior samples and print the mean values for different components. This provides insights into the model's learned parameters. ```python for name, values in ci.model_samples.items(): print(f'{name}: {values.numpy().mean(axis=0)}') ``` -------------------------------- ### Sample and Plot Seasonal State Space Model Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Generate a sample from a defined seasonal state space model and plot the resulting time series data. ```python plt.plot(s_ssm.sample()); ``` -------------------------------- ### Python: Perform Causal Impact Analysis Source: https://github.com/willianfuks/tfcausalimpact/blob/master/README.md Load data using pandas, define pre- and post-intervention periods, and initialize CausalImpact in Python. Data should be indexed by date. ```python import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('https://raw.githubusercontent.com/WillianFuks/tfcausalimpact/master/tests/fixtures/comparison_data.csv', index_col=['DATE']) pre_period = ['2019-04-16', '2019-07-14'] post_period = ['2019-7-15', '2019-08-01'] ci = CausalImpact(data, pre_period, post_period, model_args={'fit_method': 'hmc'}) ``` -------------------------------- ### Initialize Causal Impact with Real Data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initializes the CausalImpact model using real-world data and specified pre- and post-intervention periods. Dates should be in a format recognized by pandas. ```python pre_period = ['2020-01-01', '2020-03-01'] post_period = ['2020-03-02', '2020-03-31'] ci = CausalImpact(gdata, pre_period, post_period) ``` -------------------------------- ### Run Causal Impact Analysis with Default Settings Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt This snippet demonstrates the basic usage of the CausalImpact class, loading data, defining pre and post intervention periods, and printing a summary of the results. Ensure your data is loaded into a pandas DataFrame with response and covariate columns. ```python import pandas as pd import numpy as np from causalimpact import CausalImpact # Load time series data with response variable (y) in first column and covariates (X) in remaining columns data = pd.read_csv('https://raw.githubusercontent.com/WillianFuks/tfcausalimpact/master/tests/fixtures/arma_data.csv')[['y', 'X']] # Simulate an intervention effect starting at index 70 data.iloc[70:, 0] += 5 # Define pre-intervention and post-intervention periods pre_period = [0, 69] post_period = [70, 99] # Run causal impact analysis with default settings ci = CausalImpact(data, pre_period, post_period) # Access results print(ci.summary()) ``` -------------------------------- ### Initializing Causal Impact with Default Settings Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initializes a CausalImpact object with specified pre and post periods and default model arguments, including standardization. ```python dated_data = data.set_index(pd.date_range(start='20200101', periods=len(data))) pre_period = ['20200101', '20200310'] post_period = ['20200311', '20200409'] ci = CausalImpact(dated_data, pre_period, post_period, model_args={'standardize': False}) ``` -------------------------------- ### Initialize CausalImpact with a Custom Model Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Prepare data by standardizing it and ensuring it has a float32 dtype. Define a custom tfp.sts model, such as a combination of LocalLinearTrend and LinearRegression, and pass it to the CausalImpact constructor. The input data should be the original denormalized data. ```python from causalimpact.misc import standardize normed_data, _ = standardize(data.astype(np.float32)) obs_data = normed_data.iloc[:70, 0] linear_level = tfp.sts.LocalLinearTrend(observed_time_series=obs_data) linear_reg = tfp.sts.LinearRegression(design_matrix=normed_data.iloc[:, 1:].values.reshape(-1, normed_data.shape[1] -1)) model = tfp.sts.Sum([linear_level, linear_reg], observed_time_series=obs_data) pre_period = [0, 69] post_period = [70, 99] ci = CausalImpact(data, pre_period, post_period, model=model) ``` -------------------------------- ### Load and Plot Stock Data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Loads stock data from a CSV file and plots it. Ensure the CSV has 'Date' as the index and is comma-separated. ```python data = pd.read_csv('../tests/fixtures/volks_data.csv', header=0, sep=' ', index_col='Date', parse_dates=True) data.plot(); ``` -------------------------------- ### Configure Causal Impact Model Arguments Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt This snippet illustrates how to customize the structural time series model using the `model_args` parameter. Options include data standardization, prior settings, fitting method ('vi' or 'hmc'), number of posterior samples, and seasonality. ```python import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('data.csv') pre_period = [0, 69] post_period = [70, 99] # Configure model with custom arguments ci = CausalImpact( data, pre_period, post_period, model_args={ 'standardize': True, # Normalize data to zero mean and unit std (default: True) 'prior_level_sd': 0.01, # Prior std for local level; use 0.1 if covariates don't explain y well 'fit_method': 'vi', # 'vi' for speed (default), 'hmc' for precision 'niter': 1000, # Number of posterior samples (default: 1000) 'nseasons': 7, # Add weekly seasonality for daily data 'season_duration': 1 # How many data points each season value spans }, alpha=0.05 # Significance level for credible intervals (default: 0.05) ) print(ci.summary()) ``` -------------------------------- ### Initialize Causal Impact with Covariate Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initializes the CausalImpact model with data that includes a random covariate. 'standardize': False is set to disable data standardization. ```python pre_period = [0, 69] post_period = [70, 99] ci = CausalImpact(data2, pre_period, post_period, model_args={'standardize': False}) ``` -------------------------------- ### Initializing Causal Impact with HMC Fit Method Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initializes a CausalImpact object with specified pre and post periods and sets the fitting method to Hamiltonian Monte Carlo (HMC). Standardization is also disabled. ```python ci = CausalImpact(dated_data, pre_period, post_period, model_args={'standardize': False, 'fit_method':'hmc'}) ``` -------------------------------- ### Data Standardization Utilities Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Utilize `standardize`, `unstandardize`, and `maybe_unstandardize` from `causalimpact.misc` for data preprocessing. `standardize` centers and scales data, while `unstandardize` reverses the process. ```python import pandas as pd import numpy as np from causalimpact.misc import standardize, unstandardize, maybe_unstandardize # Create sample data data = pd.DataFrame({ 'y': np.random.randn(100) * 10 + 50, 'x1': np.random.randn(100) * 5 + 20, 'x2': np.random.randn(100) * 3 + 10 }) # Standardize data (zero mean, unit standard deviation) normed_data, (mu, sig) = standardize(data) print(f"Standardized mean: {normed_data.mean()}") # ~0 print(f"Standardized std: {normed_data.std()}") # ~1 # Reverse standardization original_data = unstandardize(normed_data, (mu, sig)) print(f"Restored mean: {original_data.mean()}") # Back to original # Conditional unstandardization (returns data unchanged if mu_sig is None) result = maybe_unstandardize(normed_data, (mu, sig)) # Returns unstandardized result = maybe_unstandardize(data, None) # Returns data unchanged ``` -------------------------------- ### Simulate Random Walk with TensorFlow Probability Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Uses TensorFlow Probability to define priors and a LocalLevelStateSpaceModel for simulating a random walk. Requires TensorFlow and TensorFlow Probability. ```python observed_stddev, observed_initial = (tf.convert_to_tensor(value=1, dtype=tf.float32), tf.convert_to_tensor(value=0., dtype=tf.float32)) level_scale_prior = tfd.LogNormal(loc=tf.math.log(0.05 * observed_stddev), scale=1, name='level_scale_prior') initial_state_prior = tfd.MultivariateNormalDiag(loc=observed_initial[..., tf.newaxis], scale_diag=(tf.abs(observed_initial) + observed_stddev)[..., tf.newaxis], name='initial_level_prior') ll_ssm = tfp.sts.LocalLevelStateSpaceModel(100, initial_state_prior=initial_state_prior, level_scale=level_scale_prior.sample()) ``` -------------------------------- ### Custom Model with Seasonal Components Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Create a custom CausalImpact model by integrating LocalLinearTrend and Seasonal components for weekly seasonality. Disable internal standardization if data is pre-standardized. ```python import numpy as np import pandas as pd import tensorflow_probability as tfp from causalimpact import CausalImpact from causalimpact.misc import standardize # Prepare data data = pd.read_csv('data.csv').astype(np.float32) pre_period = ['2020-01-01', '2020-04-01'] post_period = ['2020-04-02', '2020-05-01'] normed_data, _ = standardize(data) obs_data = normed_data.loc[:pre_period[1]].iloc[:, 0] # Build model with multiple components local_linear = tfp.sts.LocalLinearTrend(observed_time_series=obs_data) seasonal = tfp.sts.Seasonal(num_seasons=7, observed_time_series=obs_data) # Weekly seasonality # Combine components model = tfp.sts.Sum( [local_linear, seasonal], observed_time_series=obs_data ) # Run analysis with custom model (disable internal standardization since we pre-standardized) ci = CausalImpact( data, pre_period, post_period, model=model, model_args={'standardize': False} ) print(ci.summary()) ``` -------------------------------- ### Initializing Causal Impact Analysis Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initializes the CausalImpact class with the data, pre-intervention period, post-intervention period, and a custom TensorFlow Probability model. ```python ci = CausalImpact(data, pre_period, post_period, model=model) ``` -------------------------------- ### Generate Synthetic Data and Plot Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Generates synthetic data based on the simulated random walk, introduces a structural break, and plots the data using pandas and matplotlib. Ensure pandas and matplotlib are imported. ```python ll_ssm_sample = np.squeeze(ll_ssm.sample().numpy()) x0 = 100 * np.random.rand(100) x1 = 90 * np.random.rand(100) y = 1.2 * x0 + 0.9 * x1 + ll_ssm_sample y[70:] += 10 data = pd.DataFrame({'x0': x0, 'x1': x1, 'y': y}, columns=['y', 'x0', 'x1']) data.plot() plt.axvline(69, linestyle='--', color='k') plt.legend() ``` -------------------------------- ### Run CausalImpact with Seasonal Data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Initialize and run a CausalImpact analysis on data that includes a seasonal component, specifying the number of seasons in the model arguments. ```python ci = CausalImpact(season_data, pre_period, post_period, model_args={'nseasons': 7}) ``` -------------------------------- ### Build Custom Model with LocalLinearTrend and LinearRegression Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Construct a custom CausalImpact model by combining LocalLinearTrend and LinearRegression components. Ensure data is pre-standardized if model standardization is disabled. ```python import numpy as np import pandas as pd import tensorflow_probability as tfp from causalimpact import CausalImpact from causalimpact.misc import standardize # Prepare data data = pd.read_csv('data.csv').astype(np.float32) pre_period = ['2020-01-01', '2020-04-01'] post_period = ['2020-04-02', '2020-05-01'] normed_data, _ = standardize(data) obs_data = normed_data.iloc[:, 0] # Build model with LocalLinearTrend and LinearRegression linear_level = tfp.sts.LocalLinearTrend(observed_time_series=obs_data) linear_reg = tfp.sts.LinearRegression( design_matrix=normed_data.iloc[:, 1:].values.reshape(-1, normed_data.shape[1] - 1) ) model = tfp.sts.Sum([linear_level, linear_reg], observed_time_series=obs_data) # Run causal impact with custom model ci = CausalImpact(data, pre_period, post_period, model=model) print(ci.summary()) ci.plot() ``` -------------------------------- ### Creating Stationary Data with Log Difference Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Transforms time series data to achieve stationarity by applying a logarithmic transformation followed by differencing. This is a common preprocessing step for time series models. ```python stationary_data = np.log(data['BTC-USD'].loc[:'2020-10-14']).diff() ``` -------------------------------- ### Fetch and preprocess stock market data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Fetches closing prices for multiple stock symbols (TWTR, GOOGL, AAPL, MSFT, AMZN, FB, GOLD) from Yahoo Finance. It processes the data similarly to Bitcoin data: removing duplicates, setting 'Date' as index, resampling to daily, and filling missing values. ```python X_data = pdr.get_data_yahoo(['TWTR', 'GOOGL', 'AAPL', 'MSFT', 'AMZN', 'FB', 'GOLD'], start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2020, 12, 2))['Close'] X_data = X_data.reset_index().drop_duplicates(subset='Date', keep='last').set_index('Date').sort_index() X_data = X_data.resample('D').fillna('nearest') ``` -------------------------------- ### Prepare Data with Random Covariate Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Adds a random covariate 'x2' to the existing data for Causal Impact analysis. This is useful for testing the model's sensitivity to unrelated variables. ```python x2 = pd.DataFrame(np.random.randn(100, 1), columns=['x2']) data2 = pd.concat([data, x2], axis=1) ``` -------------------------------- ### Plotting time series data with intervention marker Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Visualize the time series data using matplotlib, highlighting a specific intervention point with a vertical dashed line. This helps in visually assessing the data before and after the intervention. ```python np.log(data).plot(figsize=(15, 12)) plt.axvline('2020-10-14', 0, np.max(data['BTC-USD']), lw=2, ls='--', c='red', label='PayPal Impact') plt.legend(loc='upper left') ``` -------------------------------- ### Concatenate and clean data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Concatenates the Bitcoin data with the other stock market data. It then removes rows with any missing values and resamples the data to a weekly frequency, keeping the last entry of each week and converting to float32. ```python data = pd.concat([btc_data, X_data], axis=1) data.dropna(inplace=True) data = data.resample('W-Wed').last().astype(np.float32) ``` -------------------------------- ### Inspect Data Index Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Displays the index of the loaded DataFrame, which should be a DatetimeIndex. ```python data.index ``` -------------------------------- ### Display the full DataFrame Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Print the entire pandas DataFrame to inspect the loaded time series data for all symbols. This is useful for verifying data integrity and structure. ```python data ``` -------------------------------- ### Extracting Sparse Linear Regression Weights (Variational Inference) Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Calculates the mean of the weights for the SparseLinearRegression component when the model is fitted using Variational Inference. This helps assess the contribution of covariates. ```python tf.reduce_mean(ci.model.components_by_name['SparseLinearRegression/'].params_to_weights( ci.model_samples['SparseLinearRegression/_global_scale_variance'], ci.model_samples['SparseLinearRegression/_global_scale_noncentered'], ci.model_samples['SparseLinearRegression/_local_scale_variances'], ci.model_samples['SparseLinearRegression/_local_scales_noncentered'], ci.model_samples['SparseLinearRegression/_weights_noncentered'], ), axis=0) ``` -------------------------------- ### Analyzing Model Components with TensorFlow Probability Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Decompose the fitted Causal Impact model by component using `tfp.sts.decompose_by_component` and `tfp.sts.decompose_forecast_by_component`. Visualize the mean and standard deviation of each component's contribution. ```python import numpy as np import pandas as pd import tensorflow as tf import tensorflow_probability as tfp import matplotlib.pyplot as plt from causalimpact import CausalImpact # Run causal impact analysis data = pd.read_csv('data.csv') ci = CausalImpact(data, [0, 69], [70, 99], model_args={'standardize': False}) # Decompose fitted time series by component component_dists = tfp.sts.decompose_by_component( ci.model, ci.observed_time_series, ci.model_samples ) # Plot each component's contribution fig, axes = plt.subplots(len(component_dists), 1, figsize=(12, 3 * len(component_dists))) for ax, (component, component_dist) in zip(axes, component_dists.items()): mean = component_dist.mean().numpy() stddev = component_dist.stddev().numpy() ax.plot(mean, label='Mean') ax.fill_between(range(len(mean)), mean - 2*stddev, mean + 2*stddev, alpha=0.3) ax.set_title(component.name) ax.legend() plt.tight_layout() plt.show() # Decompose forecast by component forecast_dists = tfp.sts.decompose_forecast_by_component( ci.model, ci.posterior_dist, ci.model_samples ) ``` -------------------------------- ### Load Real Data for Causal Impact Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Loads time series data from a CSV file for Causal Impact analysis. Ensure the CSV file has a 'date' column for indexing. ```python gdata = pd.read_csv('../tests/fixtures/google_data.csv', index_col='date') ``` -------------------------------- ### Display data types Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Prints the data types of each column in the processed DataFrame. ```python data.dtypes ``` -------------------------------- ### Add Seasonal Component to Data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Incorporate a sampled seasonal component into the original data to simulate a time series with seasonal patterns for CausalImpact analysis. ```python season_data = data.copy() season_data['y'] += np.squeeze(s_ssm.sample().numpy()) ``` -------------------------------- ### R: Perform Causal Impact Analysis Source: https://github.com/willianfuks/tfcausalimpact/blob/master/README.md Load data, define pre- and post-intervention periods, and run the CausalImpact function in R. Ensure data is in zoo format and periods are specified as dates. ```r data = read.csv.zoo('comparison_data.csv', header=TRUE) pre.period <- c(as.Date("2019-04-16"), as.Date("2019-07-14")) post.period <- c(as.Date("2019-07-15"), as.Date("2019-08-01")) ci = CausalImpact(data, pre.period, post.period) ``` -------------------------------- ### Python: Causal Impact with Hamiltonian Monte Carlo Source: https://github.com/willianfuks/tfcausalimpact/blob/master/README.md Switch to the Hamiltonian Monte Carlo (HMC) fitting method for higher precision in causal impact analysis. Note that this can significantly increase computation time. ```python ci = CausalImpact(data, pre_period, post_period, model_args={'fit_method': 'hmc'}) ``` -------------------------------- ### Display processed stock data Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Displays the DataFrame containing the processed stock market data, showing symbols and their daily closing prices. ```python X_data ``` -------------------------------- ### Plot Forecast Components with CausalImpact Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Use this function to visualize the components of a trained CausalImpact model, such as the forecast and its uncertainty. ```python plot_forecast_components(ci) ``` -------------------------------- ### Yule-Walker Estimation for AR Model Coefficients Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Estimates the coefficients (rho) and noise variance (sigma) of an autoregressive model using the Yule-Walker method. Requires stationary data. ```python from statsmodels.regression.linear_model import yule_walker rho, sigma = yule_walker(stationary_data, 1, method='mle') print(rho, sigma) ``` -------------------------------- ### Print Summary of Causal Impact Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Display a concise summary of the causal impact analysis, including actual vs. predicted values, absolute and relative effects, and confidence intervals. This is useful for a quick quantitative overview. ```python print(ci.summary()) ``` -------------------------------- ### Use HMC Fitting Method for Higher Precision Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Applies the Hamiltonian Monte Carlo (HMC) fitting method for more accurate posterior sampling, which is slower but yields higher precision. Ensure data is loaded and periods are defined. ```python import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('comparison_data.csv', index_col=['DATE']) pre_period = ['2019-04-16', '2019-07-14'] post_period = ['2019-07-15', '2019-08-01'] # Use HMC for more precise inference (slower but more accurate) ci = CausalImpact( data, pre_period, post_period, model_args={'fit_method': 'hmc'} ) # Results will be more precise but computation takes longer # Consider using GPU acceleration for complex time series print(ci.summary()) ``` -------------------------------- ### Plot Time Series Components Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Visualizes the decomposition of the time series into its constituent components based on the fitted Causal Impact model. Requires a fitted CausalImpact object. ```python def plot_time_series_components(ci): # https://github.com/tensorflow/probability/blob/v0.16.0/tensorflow_probability/python/sts/decomposition.py#L165-L195 component_dists = tfp.sts.decompose_by_component(ci.model, ci.observed_time_series, ci.model_samples) num_components = len(component_dists) xs = np.arange(len(ci.observed_time_series)) fig = plt.figure(figsize=(12, 3 * num_components)) mu, sig = ci.mu_sig if ci.mu_sig is not None else 0.0, 1.0 for i, (component, component_dist) in enumerate(component_dists.items()): # If in graph mode, replace `.numpy()` with `.eval()` or `sess.run()`. component_mean = component_dist.mean().numpy() component_stddev = component_dist.stddev().numpy() ax = fig.add_subplot(num_components, 1, 1 + i) ax.plot(xs, component_mean, lw=2) ax.fill_between(xs, component_mean - 2 * component_stddev, component_mean + 2 * component_stddev, alpha=0.5) ax.set_title(component.name) ``` -------------------------------- ### Extract Linear Regression Weights (Variational Inference) Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Extracts and prints the mean of covariate weights from a TF Causal Impact model using Variational Inference. Higher absolute values indicate stronger predictive power. Ensure the model has been fitted. ```python import tensorflow as tf import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('data.csv') ci = CausalImpact(data, [0, 69], [70, 99], model_args={'standardize': False}) # For Variational Inference (default), samples are in a dictionary if isinstance(ci.model_samples, dict): weights = tf.reduce_mean( ci.model.components_by_name['SparseLinearRegression/'].params_to_weights( ci.model_samples['SparseLinearRegression/_global_scale_variance'], ci.model_samples['SparseLinearRegression/_global_scale_noncentered'], ci.model_samples['SparseLinearRegression/_local_scale_variances'], ci.model_samples['SparseLinearRegression/_local_scales_noncentered'], ci.model_samples['SparseLinearRegression/_weights_noncentered'], ), axis=0 ) print(f"Covariate weights: {weights.numpy()}") # Higher absolute values indicate stronger predictive power ``` -------------------------------- ### Plot Forecast Components Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Visualizes the decomposition of the forecast into its constituent components. This function is useful for understanding the contribution of each component to the overall forecast. Requires a fitted CausalImpact object. ```python def plot_forecast_components(ci): component_forecasts = tfp.sts.decompose_forecast_by_component(ci.model, ci.posterior_dist, ci.model_samples) num_components = len(component_forecasts) xs = np.arange(len(ci.post_data)) fig = plt.figure(figsize=(12, 3 * num_components)) mu, sig = ci.mu_sig if ci.mu_sig is not None else 0.0, 1.0 for i, (component, component_dist) in enumerate(component_forecasts.items()): # If in graph mode, replace `.numpy()` with `.eval()` or `sess.run()`. component_mean = component_dist.mean().numpy() component_stddev = component_dist.stddev().numpy() ax = fig.add_subplot(num_components, 1, 1 + i) ax.plot(xs, component_mean, lw=2) ax.fill_between(xs, component_mean - 2 * component_stddev, component_mean + 2 * component_stddev, alpha=0.5) ax.set_title(component.name) ``` -------------------------------- ### Model Seasonal Components with TensorFlow Probability Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Define a structural time series model with seasonal components using TensorFlow Probability. This involves setting up priors for drift scale and initial state, and specifying the number of seasons and timesteps. ```python observed_stddev, observed_initial = (5., 0.) num_seasons = 7 drift_scale_prior = tfd.LogNormal(loc=tf.math.log(.01 * observed_stddev), scale=1.) initial_effect_prior = tfd.Normal(loc=observed_initial, scale=tf.abs(observed_initial) + observed_stddev) initial_state_prior = tfd.MultivariateNormalDiag(loc=tf.stack([initial_effect_prior.mean()] * num_seasons, axis=-1), scale_diag=tf.stack([initial_effect_prior.stddev()] * num_seasons, axis=-1)) s_ssm = tfp.sts.SeasonalStateSpaceModel( num_timesteps=100, num_seasons=num_seasons, num_steps_per_season=1, drift_scale=drift_scale_prior.sample(), initial_state_prior=initial_state_prior ) ``` -------------------------------- ### Partial Autocorrelation Plot (PACF) Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Plots the partial autocorrelation function (PACF) for a time series. This is crucial for determining the order of an autoregressive (AR) model. ```python from statsmodels.tsa.stattools import pacf from statsmodels.graphics.tsaplots import plot_pacf plot_pacf(stationary_data); ``` -------------------------------- ### Plot Seasonal Decomposition Results Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Plots the results of the seasonal decomposition, showing trend, seasonal, and residual components. ```python result.plot(); ``` -------------------------------- ### Causal Impact Summary Report Source: https://github.com/willianfuks/tfcausalimpact/blob/master/README.md Generate a detailed report for the Causal Impact analysis. This includes posterior inference on actual, predicted, and effect values, along with confidence intervals and probabilities of causal effect. ```python print(ci.summary(output='report')) ``` -------------------------------- ### Seasonal Decomposition of Time Series Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Performs seasonal decomposition on a time series using statsmodels. Ensure the data is preprocessed and the period is correctly specified. ```python from statsmodels.tsa.seasonal import seasonal_decompose result = seasonal_decompose(btc_data.loc[:'2020-10-14'].iloc[:, 0]) result.plot(); ``` -------------------------------- ### Plotting Causal Impact Results Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Visualizes the causal impact analysis using the plot() method, which can display all three panels (original data, pointwise effects, cumulative effects) by default. Customize panels, figure size, and save plots. ```python import pandas as pd import matplotlib.pyplot as plt from causalimpact import CausalImpact data = pd.read_csv('data.csv') ci = CausalImpact(data, [0, 69], [70, 99]) # Plot all three panels (default) ci.plot() # Plot specific panels only ci.plot(panels=['original', 'pointwise']) # Customize figure size ci.plot(figsize=(15, 12)) # Access figure for further customization (don't show immediately) ci.plot(show=False) ax = plt.gca() ax.set_title('Custom Title') fig = plt.gcf() fig.savefig('causal_impact_results.png', dpi=300) plt.show() ``` -------------------------------- ### Plot CausalImpact Results Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Generate a comprehensive plot of the CausalImpact analysis, typically showing the actual data, the predicted values with confidence intervals, and the estimated effects. ```python ci.plot(figsize=(15, 12)) ``` -------------------------------- ### Plot Time Series Components Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Plots the components of a Causal Impact analysis. Ensure the CausalImpact object is already defined. ```python plot_time_series_components(ci) ``` -------------------------------- ### Plot Causal Impact Results Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Visualize the results of the CausalImpact analysis, including actual data, predictions, and confidence intervals, using the plot method. ```python ci.plot(figsize=(15, 14)) ``` -------------------------------- ### Lag Plot for Time Series Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Generates a lag plot to visualize the correlation between a time series and its lagged values. Useful for identifying patterns and seasonality. ```python pd.plotting.lag_plot(stationary_data) ``` -------------------------------- ### Set Prior for Level Standard Deviation in CausalImpact Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Customize the prior for the standard deviation of the level parameter in CausalImpact. The default is 0.01, suitable for well-behaved data. Increasing this value, like to 0.1, indicates a stronger random walk component not explained by covariates. ```python ci = CausalImpact(data, pre_period, post_period, model_args={'prior_level_sd':0.1}) ``` -------------------------------- ### Access Inferred Results Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Access the raw inference results from the fitted CausalImpact model. The `inferences` attribute provides a DataFrame with detailed predictions and confidence intervals, useful for further custom analysis. ```python ci.inferences.head() ``` -------------------------------- ### Select a specific column from the DataFrame Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Access a single column from a pandas DataFrame to isolate a specific time series for analysis. Ensure the column name matches the data. ```python data['BTC-USD'] ``` -------------------------------- ### Defining a Causal Impact Model with TensorFlow Probability Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Constructs a structural time series model using TensorFlow Probability, including local linear trend, linear regression, and seasonal components. This model is then used for Causal Impact analysis. ```python 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( [normed_data.loc[pre_period[0]: pre_period[1]], normed_data.loc[post_period[0]: post_period[1]]] ).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) ``` -------------------------------- ### Accessing Inference Results Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Retrieves detailed inference results from a CausalImpact object, including predictions, effects, and credible intervals stored in the 'inferences' DataFrame. Also accesses computed attributes like p-value and alpha, and the fitted model. ```python import pandas as pd from causalimpact import CausalImpact data = pd.read_csv('data.csv') ci = CausalImpact(data, [0, 69], [70, 99]) # Access full inference results as DataFrame print(ci.inferences.head()) # Access other computed attributes print(f"P-value: {ci.p_value}") print(f"Alpha: {ci.alpha}") print(f"Pre-period data shape: {ci.pre_data.shape}") print(f"Post-period data shape: {ci.post_data.shape}") # Access the fitted model and samples for advanced analysis model = ci.model samples = ci.model_samples ``` -------------------------------- ### Working with NumPy Arrays Source: https://context7.com/willianfuks/tfcausalimpact/llms.txt Perform Causal Impact analysis using NumPy arrays as input. The library automatically converts arrays to DataFrames internally. Ensure the first column is the response variable. ```python import numpy as np from causalimpact import CausalImpact # Generate synthetic data as numpy array np.random.seed(42) n_points = 100 # Response variable with intervention effect y = np.cumsum(np.random.randn(n_points)) + 100 y[70:] += 10 # Add intervention effect # Covariates x1 = np.cumsum(np.random.randn(n_points)) + 50 x2 = np.cumsum(np.random.randn(n_points)) + 30 # Combine into array (first column is response, rest are covariates) data = np.column_stack([y, x1, x2]) # Run causal impact with numpy array pre_period = [0, 69] post_period = [70, 99] ci = CausalImpact(data, pre_period, post_period) print(ci.summary()) ci.plot() ``` -------------------------------- ### Plot Specific Causal Impact Panels Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Customize the plot to display only specific panels, such as 'original' and 'pointwise' effects. Adjust figsize for better visualization. ```python ci.plot(panels=['original', 'pointwise'], figsize=(15, 12)) ``` -------------------------------- ### Plot Causal Impact with Specific Panels Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Generates a plot for the Causal Impact analysis, displaying only the 'original' data panel. Useful for focusing on the raw time series. ```python ci.plot(figsize=(15, 6), panels=['original']) ``` -------------------------------- ### Causal Impact with Linear Regression and Data Holes Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Performs causal impact analysis on time series data with missing values (holes) by applying regularization and filling NaNs. This approach ensures proper inference when using linear regression models. ```python from causalimpact.misc import standardize pre_period = ['20200101', '20200311'] post_period = ['20200312', '20200409'] dated_data_holes = dated_data[dated_data.index.isin(['20200315', '20200316', '20200404']) == False] reg_dated_data_holes = tfp.sts.regularize_series(dated_data_holes) normed_data = standardize(reg_dated_data_holes.astype(np.float32))[0] obs_data = normed_data.loc[pre_period[0]: pre_period[1]].iloc[:, 0] reg_normed_data = tfp.sts.regularize_series(normed_data) design_matrix_data = (reg_normed_data.iloc[:, 1:]).astype(np.float32).fillna(0) linear_level = tfp.sts.LocalLinearTrend(observed_time_series=obs_data) linear_reg = tfp.sts.LinearRegression(design_matrix=design_matrix_data.values.reshape(-1, normed_data.shape[1] -1)) model = tfp.sts.Sum([linear_level, linear_reg], observed_time_series=obs_data) ci = CausalImpact(dated_data_holes, pre_period, post_period, model=model) ``` -------------------------------- ### Autocorrelation Plot for Time Series Source: https://github.com/willianfuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb Generates an autocorrelation plot (ACF) to visualize the correlation between a time series and its lagged values. Helps in identifying the order of AR or MA models. ```python pd.plotting.autocorrelation_plot(stationary_data); ```