In [1]:
import time
import numpy as np
import pandas as pd
In [2]:
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
import warnings
warnings.filterwarnings('ignore')
In [4]:
import assign_0

Discrete

In [5]:
ret_dg = assign_0.disc_grid(output=True)
df_dg = pd.DataFrame(ret_dg).set_index('k', drop=False)
0:   0.8023
459: 9.66338e-13
converged
In [6]:
fig, ax = plt.subplots();
(df_dg['kp']-df_dg['k']).plot(ax=ax);
ax.hlines(0, *ax.get_xlim(), linestyle='--', linewidth=1);

Discrete — Interpolation

In [7]:
ret_di = assign_0.disc_interp(output=True)
df_di = pd.DataFrame(ret_di).set_index('k', drop=False)
0:  0.84304
468: 9.88987e-13
converged
In [8]:
fig, ax = plt.subplots();
(df_di['kp']-df_di['k']).plot(ax=ax);
ax.hlines(0, *ax.get_xlim(), linestyle='--', linewidth=1);

Errors

In [9]:
def disc_perf(fun, lN1, lN0_lo, lN0_hi, kind='linear'):
    N1 = 2**lN1

    res = []
    for p in range(lN0_lo, lN0_hi+1):
        N0 = 2**p

        t0 = time.time()
        ret = fun(N=N0)
        dt = time.time() - t0

        k_grid, verr = assign_0.disc_errors(ret['v'], ret['kp'], N0, N1, kind=kind)
        acc = -np.mean(np.log10(np.abs(verr)))

        res.append((N0, dt, acc))
        print(f'{N0}: {dt} {acc}')

    return pd.DataFrame(res, columns=['N', 'dt', 'acc']).set_index('N')
In [10]:
perf_dg = disc_perf(assign_0.disc_grid, lN1=16, lN0_lo=6, lN0_hi=12)
64: 0.028228044509887695 4.962549131114292
128: 0.03855252265930176 5.5744371339277095
256: 0.08524894714355469 6.188979062317487
512: 0.3041269779205322 6.77797289085815
1024: 1.16587495803833 7.379935926597037
2048: 8.92326545715332 7.98390462541128
4096: 38.047409534454346 8.584796937137867
In [11]:
perf_di = disc_perf(assign_0.disc_interp, lN1=16, lN0_lo=6, lN0_hi=14)
64: 3.743008852005005 4.614310392907732
128: 3.88028621673584 5.231517500611821
256: 4.424067497253418 5.828693788888254
512: 5.426894903182983 6.431179461517878
1024: 7.2872114181518555 7.034028420450861
2048: 10.805475950241089 7.6366692853560965
4096: 17.566997528076172 8.237941626975076
8192: 31.373860597610474 8.841007427579823
16384: 59.02369260787964 9.442448003303806
In [12]:
fig, ax = plt.subplots()
ax.plot(perf_dg['acc'], np.log10(perf_dg['dt']), marker='o', label='Grid Search');
ax.plot(perf_di['acc'], np.log10(perf_di['dt']), marker='o', label='Interpolation');
ax.set_xlabel('accuracy');
ax.set_ylabel('log(time)');
ax.legend();

Continuous

Rediscretization

In [40]:
ret_cd = assign_0.cont_redisc(output=True)
df_cd = pd.DataFrame(ret_cd).set_index('k', drop=False)
0:  4.11829
10000: 0.000101322
20000: 1.43698e-06
30000: 1.17936e-07
40000: 9.68369e-09
50000: 7.95135e-10
60000: 6.52896e-11
70000: 5.36104e-12
76719: 9.99867e-13
converged
In [41]:
fig, ax = plt.subplots();
df_cd['dk'].plot(ax=ax);
plt.hlines(0, *ax.get_xlim(), linestyle='--', linewidth=1);

Reverse Shooting

In [42]:
ret_cs = assign_0.cont_reverse(output=True)
df_cs = pd.DataFrame(ret_cs).set_index('k', drop=False)
In [43]:
fig, ax = plt.subplots();
df_cs['dk'].plot(ax=ax);
plt.hlines(0, *ax.get_xlim(), linestyle='--', linewidth=1);

Sparse

In [44]:
ret_sp = assign_0.cont_sparse(output=True)
df_sp = pd.DataFrame(ret_sp).set_index('k', drop=False)
0: 6.5780714209040525e-15 0.170205304060147
37: 5.218048215738236e-15 6.395162177597058e-13
converged
In [45]:
fig, ax = plt.subplots();
df_sp['dk'].plot(ax=ax);
plt.hlines(0, *ax.get_xlim(), linestyle='--', linewidth=1);

Errors

In [46]:
def cont_perf(fun, lN1, lN0_lo, lN0_hi, kind='linear'):
    N1 = 2**lN1

    res = []
    for p in range(lN0_lo, lN0_hi+1):
        N0 = 2**p

        t0 = time.time()
        ret = fun(N=N0)
        dt = time.time() - t0

        k_grid, verr = assign_0.cont_errors(ret['v'], ret['i'], ret['dv'], N0, N1, kind=kind)
        acc = -np.mean(np.log10(np.abs(verr)))

        res.append((N0, dt, acc))
        print(f'{N0}: {dt} {acc}')

    return pd.DataFrame(res, columns=['N', 'dt', 'acc']).set_index('N'), k_grid, verr
In [47]:
perf_cr, k_grid, verr = cont_perf(assign_0.cont_redisc, lN1=16, lN0_lo=5, lN0_hi=10)
32: 8.116379022598267 3.386550865452894
64: 8.892828941345215 3.572240416537926
128: 9.443246841430664 3.727919686855998
256: 10.654242753982544 3.8765324660371094
512: 13.158156871795654 4.002966295137823
1024: 17.189457178115845 4.103418745786425
In [48]:
perf_sp, k_grid, verr = cont_perf(assign_0.cont_sparse, lN1=16, lN0_lo=5, lN0_hi=11)
32: 0.06785750389099121 3.438149786793982
64: 0.06741571426391602 3.745008142841036
128: 0.07169079780578613 4.049385349228878
256: 0.07592153549194336 4.350201051105409
512: 0.07984113693237305 4.652539096451723
1024: 0.09577631950378418 4.953404149823195
2048: 0.12388873100280762 5.25460857011267
In [49]:
acc = np.log10(np.abs(verr))
print(np.mean(-acc))
plt.plot(k_grid, acc);
5.25460857011267
In [50]:
fig, ax = plt.subplots()
ax.plot(perf_cr['acc'], np.log10(perf_cr['dt']), marker='o', label='Rediscretization');
ax.plot(perf_sp['acc'], np.log10(perf_sp['dt']), marker='o', label='Sparse');
ax.set_xlabel('accuracy');
ax.set_ylabel('log(time)');
ax.legend();