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 requests | |
sess = requests.Session() | |
sess.verify = False # For testing locally | |
r = sess.post('https://localhost:8002/login/', | |
data={'username': 'alice', 'password': 'pass'}) |
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 django.contrib.auth import get_user_model | |
User = get_user_model() | |
user = User.objects.create_user('alice', 'alice@djwto.com', 'pass') | |
user.save() |
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 as tf | |
linear_model = tf.keras.Sequential([ | |
layers.Dense(units=1, use_bias=False) | |
]) | |
linear_model.compile( | |
optimizer=tf.optimizers.Adam(learning_rate=0.1), | |
loss=tf.keras.losses.MeanSquaredError() | |
) |
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
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 tensorflow_probability as tfp | |
tfd = tfp.distributions | |
dist = tfd.JointDistributionNamed(dict( | |
Operation=tfd.LogNormal(1, 0.5), | |
Marketing=lambda Operation: tfd.Normal(tf.abs(Operation) * 1.2, 0.5), | |
Sales=lambda Marketing: tfd.Normal(tf.abs(Marketing) * 1.4, 0.5) | |
)) | |
dist.sample(1000) |