Skip to content

Commit e8d70fa

Browse files
committed
Added instrument data collection with tests
1 parent 947ef2d commit e8d70fa

53 files changed

Lines changed: 4964 additions & 486 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ You can install the Cannlytics engine from [PyPI](https://pypi.org/project/cannl
1616
pip install cannlytics
1717
```
1818

19+
## Documentation
20+
21+
Please refer to the [Cannlytics Developer Guide](https://docs.cannlytics.com/) for detailed information about the module.
22+
1923
## Authentication, Data, and File Management
2024

2125
Cannlytics leverages Firebase by default for a database, file storage, and authentication.

cannlytics/analytics/var.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
VAR Functions | Cannlytics
3+
4+
Author: Keegan Skeate
5+
Contact: <keegan@cannlytics.com>
6+
Created: Wed Apr 14 07:55:43 2021
7+
License: MIT License <https://opensource.org/licenses/MIT>
8+
9+
Description:
10+
11+
Crude VAR functions.
12+
13+
Resources:
14+
15+
https://www.statsmodels.org/stable/generated/statsmodels.tsa.tsatools.lagmat.html
16+
17+
"""
18+
19+
import numpy as np
20+
import pandas as pd
21+
import statsmodels.api as sm
22+
from statsmodels.tsa import tsatools
23+
24+
25+
def VAR(Vector, lag_order):
26+
"""
27+
Inputs a Vector of dimension N x I, where N is the number of observations
28+
and I is the number of variables, as well as the lag order of the model.
29+
Returns the estimated equations from OLS as a dictionary with names 'Eq#'.
30+
"""
31+
X = np.empty_like(Vector)
32+
for i in range(1 , 1 + lag_order):
33+
X = np.column_stack([X, lag(Vector, i)])
34+
X = np.delete(X, np.s_[0:len(Vector[0])], axis=1)
35+
X = sm.add_constant(X)
36+
VAR_estimates = {}
37+
for i in range(1,len(Vector[0])+1):
38+
VAR_estimates["Eq{0}".format(i)] = sm.OLS(Vector[:,[i-1]][lag_order:],
39+
X[lag_order:]).fit()
40+
return VAR_estimates
41+
42+
43+
def VAR_forecast(Vector, VAR_estimates, lag_order, horizon,shock=None):
44+
"""
45+
Inputs the VAR Vector, VAR estimates, the lag order of the model,
46+
the forecast horizon, and the desired first period shock.
47+
"""
48+
# Inital Period shock for IRF
49+
if shock is None:
50+
shock = np.zeros(len(Vector[0]))
51+
error = np.zeros((len(Vector),len(Vector[0])))
52+
error[0] = shock
53+
# Predictions for Forecast Horizon
54+
for t in np.arange(0,horizon):
55+
X_hat = Vector
56+
for i in range(1 , lag_order):
57+
# X_hat = np.column_stack([X_hat, lag(Vector, i)])
58+
lagged_vector = tsatools.lagmat(Vector, maxlag=1)
59+
X_hat = np.column_stack([X_hat, lagged_vector])
60+
X_hat = sm.add_constant(X_hat)
61+
Y_hat = []
62+
for Eq in VAR_estimates:
63+
Y_hat.append(np.dot(X_hat[-1], VAR_estimates[Eq].params))
64+
Forecast = Y_hat + error[t]
65+
Vector = np.vstack((Vector,Forecast))
66+
return Vector[-horizon:]
67+
68+
69+
def IRF(Vector,VAR_estimates,lag_order,horizon,shock):
70+
baseline = VAR_forecast(Vector,VAR_estimates,lag_order,horizon,shock=None)
71+
impact = VAR_forecast(Vector,VAR_estimates,lag_order,horizon,shock=shock.T)
72+
return (impact-baseline)
73+
74+
75+
def lag_series(series, lag=None):
76+
lagged_series = pd.Series(series).shift(1)
77+
return lagged_series.values
78+
79+
80+
def lag(x,lag=None):
81+
if lag==None: lag=1
82+
lag_values = np.empty_like(x)
83+
for i in np.arange(0,len(x)):
84+
if i>=lag:
85+
lag_values[i] = x[i-lag]
86+
return lag_values
87+
88+
89+
def cov_matrix(u):
90+
k = len(u[0])
91+
matrix = np.ones((k,k))
92+
for i in np.arange(0,k):
93+
for j in np.arange(0,k):
94+
matrix[i][j] = np.cov(u.T[i],u.T[j])[0][1]
95+
return matrix

0 commit comments

Comments
 (0)