... cvxpy two: time


The code is not listed in the video, but if you want to repeat this yourself, you can use the following code to make the same charts.

Data Prep

from tqdm import tqdm

def make_dataf_generator(dataf, window=90, step=5):
    i = 0
    while i*step+window < len(dataf):
        subset = dataf.iloc[0 + i*step:window+i*step]
        yield subset.index[-1], subset
        i += 1

df = pd.read_csv("stocks.csv").assign(Date = lambda d: pd.to_datetime(d['Date']))

subset_df = (df
            .loc[lambda d: d['Date'] < pd.to_datetime('2010-01-01')]
groups = make_dataf_generator(subset_df)

x_vals = []
profit = []
timestamps = []
for ts, g in tqdm(groups):
    # this is where you can change the risk preference
    p, xs = solve_problem(g, risk_pref=1.5)

Chart One

plt.figure(figsize=(12, 3))
plt.plot(timestamps, profit);

Chart Two

plt.figure(figsize=(12, 3))
x_vals = np.array(x_vals)
for idx, stock in enumerate(tickers):
    plt.plot(timestamps, x_vals[:, idx], label=stock)

Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider signing up for the newsletter.