<jupyter_start><jupyter_text>05.01 - TIME SERIES PREDICTIONS<jupyter_code>!wget --no-cache -O init.py -q https://raw.githubusercontent.com/rramosp/ai4eng.v1/main/content/init.py
import init; init.init(force_download=False); init.get_weblink()
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import local.lib.timeseries as ts
%matplotlib inline<jupyter_output><empty_output><jupyter_text>The data<jupyter_code>d = pd.read_csv("local/data/eurcop.csv")
d.index = pd.to_datetime(d.Date)
del(d["Date"])
d.head()
d.plot(figsize=(15,3))
d[["Rate"]].plot(figsize=(15,3))
d = d[["Rate"]]
d.head(10)<jupyter_output><empty_output><jupyter_text>A predictive model First create a time series dataset with look back<jupyter_code>dt = ts.timeseries_as_many2one(d, columns=["Rate"], nb_timesteps_in=4, timelag=0)
dt.head()<jupyter_output><empty_output><jupyter_text>Split dataset for trian and for test<jupyter_code>trds = dt[:"2008"]
tsds = dt["2009":]
print (dt.shape, trds.shape, tsds.shape)
plt.figure(figsize=(15,3))
plt.plot(trds.index.values, trds.Rate.values, color="black", lw=2, label="train", alpha=.5)
plt.plot(tsds.index.values, tsds.Rate.values, color="red", lw=2, label="test", alpha=.5)
plt.grid();
plt.legend();<jupyter_output>(5040, 5) (2691, 5) (2349, 5)<jupyter_text>Create `X` and `y` matrices for train and test<jupyter_code>Xtr, ytr = trds[[i for i in trds.columns if i!="Rate"]].values, trds.Rate.values
Xts, yts = tsds[[i for i in tsds.columns if i!="Rate"]].values, tsds.Rate.values
trds[:5]
print (Xtr[:10])
print (ytr[:10])
tsds[:5]
print (Xts[:10])
print (yts[:20])<jupyter_output>[[3079.18017578 3079.18017578 3140.93432617 3193.72021484]
 [3079.18017578 3140.93432617 3193.72021484 3197.49707031]
 [3140.93432617 3193.72021484 3197.49707031 3093.39477539]
 [3193.72021484 3197.49707031 3093.39477539 3029.25683594]
 [3197.49707031 3093.39477539 3029.25683594 3029.25683594]
 [3093.39477539 3029.25683594 3029.25683594 2914.92724609]
 [3029.25683594 3029.25683594 2914.92724609 2969.78344727]
 [3029.25683594 2914.92724609 2969.78344727 2954.19067383]
 [2914.92724609 2969.78344727 2954.19067383 2983.23510742]
 [2969.78344727 2954.19067383 2983.23510742 2923.44677734]]
[3197.49707031 3093.39477539 3029.25683594 3029.25683594 2914.92724609
 2969.78344727 2954.19067383 2983.23510742 2923.44677734 2923.44677734
 2913.42114258 2897.17138672 2885.69604492 2940.19921875 2941.0378418
 2941.0378418  2878.10864258 2848.63378906 2873.06689453 2828.56445312]<jupyter_text>convert target into classification task for TREND PREDICTION (1 up, 0 down)<jupyter_code>yts = (yts>Xts[:,-1]).astype(int)
ytr = (ytr>Xtr[:,-1]).astype(int)
print (ytr[:20])
print (yts[:20])<jupyter_output>[0 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0]
[1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 1 0]<jupyter_text>inspect target class distributions<jupyter_code>print ("1's in train %.2f%s"%(np.mean(ytr)*100, "%"))
print ("1's in test  %.2f%s"%(np.mean(yts)*100, "%"))<jupyter_output>1's in train 45.04%
1's in test  41.72%<jupyter_text>train a predictive model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
estimator = RandomForestClassifier(n_estimators=5, max_depth=30)
#estimator = DecisionTreeClassifier(max_depth=2)
#estimator = LogisticRegression()
#estimator = Pipeline((("pca", PCA(n_components=2)), ("estimator", estimator)))
estimator.fit(Xtr,ytr);<jupyter_output><empty_output><jupyter_text>get predictive accuracy in train and test<jupyter_code>print ("train accuracy %.2f"%estimator.score(Xtr,ytr))
print ("test accuracy  %.2f"%estimator.score(Xts,yts))<jupyter_output>train accuracy 0.92
test accuracy  0.52<jupyter_text>inspect confusion matrix<jupyter_code>from sklearn.metrics import confusion_matrix
import seaborn as sns

cm = confusion_matrix(yts, estimator.predict(Xts))
sns.heatmap(cm,annot=True,cbar=False, fmt="d")
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.title('Confusion Matrix')<jupyter_output><empty_output><jupyter_text>A strategy- if model predicts 1 (price up) we buy 10 EUR today and sell them tomorrow- if model predicts 0 (price down) we sell 10 EUR today and buy them tomorrow<jupyter_code>def trade(d, date_close, op, qty):
    assert op in ["buy", "sell"]
    assert qty>=0
    
    r = (d.loc[:date_close].iloc[-2].Rate-d.loc[date_close].Rate)*qty    
    if op=="buy":
        r = -r
    return r<jupyter_output><empty_output><jupyter_text>example: a **buy** operation on 2011-01-03 closed (with a sell operation) on 2011-01-04<jupyter_code>trade(tsds, "2011-01-04", "buy", 100)
trade(tsds, "2011-01-05", "buy", 100)
tsds["2011-01-02":].iloc[:5]
yts
def compute_pnl(d, y, predictions, qty=10):
    pnl = []
    for date,prediction in zip(d.index[1:], predictions[1:]):
        pnl.append(trade(d, date, "sell" if prediction==0 else "buy", qty))
    pnl = pd.DataFrame(np.r_[[pnl]].T, index=d.index[1:], columns=["pnl"])
    pnl["prediction"]=predictions[1:]
    pnl["y"]=y[1:]
    return pnl
preds = estimator.predict(Xts)
pnl = compute_pnl(tsds, yts, preds)

pnl.pnl.plot()
plt.title("TOTAL PNL %.2f COP"%pnl.pnl.sum())
plt.ylabel("PNL")
plt.grid();
plt.ylim(-5000,5000);
def plot_pnlhist(pnl_series, label=""):
    k = pnl_series.values
    total = np.sum(k); 
    k = k[np.abs(k)<50000]
    plt.hist(k, bins=30);
    plt.title("PNL for %s, total %.2f COP"%(label, total))
plt.figure(figsize=(12,8))
plt.subplot(221); plot_pnlhist(pnl[pnl.y==1].pnl, "REAL = 1 (up)"); plt.grid();
plt.subplot(222); plot_pnlhist(pnl[pnl.y==0].pnl, "REAL = 0 (down)"); plt.grid();
plt.subplot(223); plot_pnlhist(pnl[preds[1:]==1].pnl, "PREDS = 1 (up)"); plt.grid();
plt.subplot(224); plot_pnlhist(pnl[preds[1:]==0].pnl, "PREDS = 0 (down)"); plt.grid();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16026069518716576, 'fraction_numerical': 0.16059491978609625, 'mean_word_length': 6.024647887323944, 'pattern_counts': {'":': 3, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '61885', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2496, 'n_words': 500, 'path': 'content/NOTES 05.01 - A TIME SERIES PREDICTIVE STRATEGY.ipynb', 'repo_name': 'restrepo/ai4eng.v1', 'size': '5984'}