This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python3.8 -m venv .env | |
source .env/bin/activate | |
pip install django django-sslserver djwto requests | |
django-admin startproject djwto_project . | |
python manage.py makemigrations | |
python manage.py migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./djwto_project/urls.py | |
from django.urls import path, include | |
urlpatterns = [ | |
path('', include('djwto.urls')), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict | |
import tensorflow_probability as tfp | |
import matplotlib.pyplot as plt | |
import numpy as np | |
def plot_components(index, one_step_dists: Dict[str, tfp.distributions.Distribution], | |
forecast_dists: Dict[str, tfp.distributions.Distribution], | |
mu_sig=None): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import pandas_datareader as pdr | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
btc_data = pdr.get_data_yahoo(['BTC-USD'], | |
start=datetime.datetime(2018, 1, 1), | |
end=datetime.datetime(2020, 12, 3))['Close'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
ll_ssm_sample = np.squeeze(ll_ssm.sample().numpy()) | |
x0 = 100 * np.random.rand(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:77, 0] += np.arange(7, 0, -1) | |
pre_period = [0, 69] | |
post_period = [70, 99] | |
ci = CausalImpact(data, pre_period, post_period) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import tensorflow_probability as tfp | |
x = np.random.rand(30) | |
y = 2.2 * x + np.random.rand(30) | |
data = pd.DataFrame({'X': x, 'y': y}, dtype=np.float32) | |
obs_data = data['y'].iloc[:20] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dist = tfd.JointDistributionNamed(dict( | |
Operation=tfd.LogNormal(1, 0.5), | |
Marketing=tfd.LogNormal(1, 0.5), | |
Sales=lambda Operation, Marketing: ( | |
tfd.Normal(tf.abs(Operation) * 1.2 + tf.abs(Marketing) * 1.4, 0.5) | |
) | |
)) | |
df = pd.DataFrame(dist.sample(1000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Operation | Marketing | Sales | |
---|---|---|---|
2.0623722 | 3.7629867 | 7.3618684 | |
2.678936 | 4.359399 | 8.935453 | |
3.4398987 | 2.6111245 | 7.8845096 | |
2.497365 | 1.0219359 | 4.478344 | |
5.3146906 | 3.7889757 | 12.521039 | |
1.3658007 | 2.4592152 | 4.381847 | |
5.7464175 | 2.567613 | 10.313836 | |
2.1926215 | 2.9856534 | 6.6152453 | |
2.8200917 | 1.9160229 | 5.9984937 |