Previous topic

Currency trading strategies

Next topic

Object detection from fMRI

Simulated time seriesΒΆ

Binder

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import fem
print 'number of threads: %i' % (fem.fortran_module.fortran_module.num_threads(),)
number of threads: 32
In [2]:
n = 10
w = fem.continuous.model_parameters(n)
In [3]:
fig, ax = plt.subplots(1, 2, figsize=(8,4))
ax[0].matshow(w)
w_flat = w.flatten()
hist = ax[1].hist(w_flat, ec='k', alpha=0.5)
plt.show()
../../_images/notebooks_continuous_01_simulated_time_series_3_0.png
In [4]:
x = fem.continuous.time_series(w, l=1e4)
In [5]:
tab = []
for i in range(n):
    tab.append([x[i].min(), x[i].max(), x[i].mean()])
pd.DataFrame(data=tab, columns=['min', 'max', 'average'], index=1+np.arange(n))
Out[5]:
min max average
1 -4.344546 4.229992 0.002489
2 -4.257472 3.915364 -0.007198
3 -4.259568 4.288349 -0.005594
4 -4.779247 4.513722 -0.006781
5 -4.578835 4.204255 0.005520
6 -4.564148 4.626909 -0.005528
7 -4.503005 3.984702 0.021771
8 -5.510507 4.412546 0.015716
9 -4.943588 3.987518 -0.004167
10 -4.438788 5.082597 -0.014730
In [6]:
fig, ax = plt.subplots(n, 1, figsize=(16, 4))
for i in range(n):
    ax[i].plot(x[i, :500], 'k-')
    ax[i].axis('off')
../../_images/notebooks_continuous_01_simulated_time_series_6_0.png
In [7]:
model = fem.continuous.model()
x1, x2 = x[:,:-1], x[:,1:]
model.fit(x1, x2, iters=10)
In [8]:
fig = plt.figure(figsize=(4,4))
ax = plt.gca()
w_fit_flat = model.w.flatten()
ax.scatter(w_flat, w_fit_flat, c='k')
grid = np.linspace(w.min(), w.max())
ax.plot(grid, grid, 'b--')
ax.set_xlabel('true parameters')
ax.set_ylabel('inferred parameters')
plt.show()
../../_images/notebooks_continuous_01_simulated_time_series_8_0.png
In [9]:
x2_prediction = model.predict(x1)
In [10]:
fig, ax = plt.subplots(n, 1, figsize=(16, 4))
for i in range(n):
    ax[i].plot(x2[i, :500], 'k-')
    ax[i].plot(x2_prediction[i, :500], 'r-')
    ax[i].axis('off')
plt.show()
../../_images/notebooks_continuous_01_simulated_time_series_10_0.png
In [ ]: