Example #1
0
def plot(sample, t, title, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(r"$x_t$")
    axis.set_xlabel(r"t")
    axis.set_title(title)
    axis.plot(t, sample, lw=1.0)
    config.save_post_asset(figure, "regression", plot_name)
Example #2
0
def pdf_samples(pdf,
                samples,
                title,
                ylabel,
                xlabel,
                plot,
                xrange=None,
                ylimit=None,
                nbins=50):
    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_ylabel(ylabel)
    axis.set_xlabel(xlabel)
    axis.set_title(title)
    axis.set_prop_cycle(config.distribution_sample_cycler)
    _, bins, _ = axis.hist(samples,
                           nbins,
                           rwidth=0.8,
                           density=True,
                           label=f"Samples",
                           zorder=5)
    if xrange is None:
        delta = (bins[-1] - bins[0]) / 500.0
        xrange = numpy.arange(bins[0], bins[-1], delta)
    sample_distribution = [pdf(val) for val in xrange]
    axis.set_xlim([xrange[0], xrange[-1]])
    if ylimit is not None:
        axis.set_ylim(ylimit)
    axis.plot(xrange, sample_distribution, label=f"Target PDF", zorder=6)
    axis.legend(bbox_to_anchor=(0.75, 0.9))
    config.save_post_asset(figure, "regression", plot)
Example #3
0
def timeseries_plot(samples, φ, δ, tmax, title, plot_name):
    p = numpy.array2string(φ, precision=2, separator=',')
    d = numpy.array2string(δ, precision=2, separator=',')
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_title(title)
    axis.set_xlabel(r"$t$")
    time = numpy.linspace(0, tmax - 1, tmax)
    axis.set_ylabel(r"$x_t$")
    axis.set_xlim([0.0, tmax])
    stats = f"Simulation Stats\n\nμ={format(numpy.mean(samples), '2.2f')}\nσ={format(numpy.std(samples), '2.2f')}"
    bbox = dict(boxstyle='square,pad=1',
                facecolor="#FEFCEC",
                edgecolor="#FEFCEC",
                alpha=0.75)
    axis.text(0.1,
              0.8,
              stats,
              fontsize=15,
              bbox=bbox,
              transform=axis.transAxes)
    params = f"Estimated Parameters\n\n" + r"$\hat{\phi}=$" + f"{p}\n" + r"$\hat{\delta}=$" + d
    axis.text(0.7,
              0.75,
              params,
              fontsize=15,
              bbox=bbox,
              transform=axis.transAxes)
    axis.plot(time, samples[:tmax], lw=1.0)
    config.save_post_asset(figure, "regression", plot_name)
Example #4
0
def loglogplot(sample, t, title, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(r"$F(\omega)$")
    axis.set_xlabel(r"$\omega$")
    axis.set_title(title)
    axis.loglog(t, sample, lw=1.0)
    config.save_post_asset(figure, "regression", plot_name)
def regression_plot(xt, yt, params, err, β_r_squared, legend_anchor, title, plot_name, lim=None):
    nsample = len(xt)
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(r"$y_{t}$")
    axis.set_xlabel(r"$x_{t}$")
    if lim is not None:
        axis.set_xlim(lim)
        axis.set_ylim(lim)
        x = numpy.linspace(lim[0], lim[1], 100)
    else:
        x = numpy.linspace(numpy.min(xt), numpy.max(xt), 100)
    y_hat = x * params[1] + params[0]
    axis.set_title(title)
    axis.plot(xt, yt, marker='o', markersize=5.0, linestyle="None", markeredgewidth=1.0, alpha=0.75, zorder=5, label="Simulation")
    axis.plot(x, y_hat, lw=3.0, color="#000000", zorder=6, label=r"$y_{t}=\hat{\alpha}+\hat{\beta}x_{t}$")
    bbox = dict(boxstyle='square,pad=1', facecolor="#f7f6e8", edgecolor="#f7f6e8", alpha=0.5)
    axis.text(x[0], y_hat[80],
              r"$\hat{\beta}=$" + f"{format(params[1], '2.4f')}, " +
              r"$\sigma_{\hat{\beta}}=$" + f"{format(err[1], '2.4f')}\n"
              r"$\hat{\alpha}=$" + f"{format(params[0], '2.4f')}, " +
              r"$\sigma_{\hat{\alpha}}=$" + f"{format(err[0], '2.4f')}\n"
              r"$R^2=$"+f"{format(β_r_squared, '2.4f')}\n",
              bbox=bbox, fontsize=14.0, zorder=7)
    axis.legend(bbox_to_anchor=legend_anchor).set_zorder(7)
    config.save_post_asset(figure, "mean_reversion", plot_name)
Example #6
0
def noise_plot(samples, time, plot_name):
    nsamples = len(samples)
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel("Time")
    axis.set_title(f"Scaled Brownian Noise, nsamples = {nsamples}")
    axis.plot(time, samples, lw=1)
    config.save_post_asset(figure, "regression", plot_name)
Example #7
0
def plot(samples, time, title, plot_name):
    nplot = len(samples)
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel("Time")
    axis.set_title(title)
    axis.plot(time, samples, lw=1)
    config.save_post_asset(figure, "brownian_motion", plot_name)
Example #8
0
def training_plot(title, df, var, box_pos, plot):
    post_fix = ["_prediction", "_lower_bound", "_upper_bound"]
    test = df[var].to_numpy()
    pred = df[var + post_fix[0]].to_numpy()
    lower = df[var + post_fix[1]].to_numpy()
    upper = df[var + post_fix[2]].to_numpy()
    time = df.index.to_numpy()
    n = len(test)

    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_title(title)

    axis.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%y'))
    axis.xaxis.set_major_locator(mdates.DayLocator())


    metrics = f"Bias = {format(bias(test, pred), '2.2f')}\nMAE  = {format(mae(test, pred), '2.2f')}\nRMSE = {format(rmse(test, pred), '2.2f')}"
    bbox = dict(boxstyle='square,pad=1', facecolor="#FEFCEC", edgecolor="#FEFCEC", alpha=0.75)
    font = {"family":"monospace"}
    axis.text(box_pos[0], box_pos[1], metrics, fontsize=15, bbox=bbox, transform=axis.transAxes, fontdict=font)

    for i in range(n):
        axis.plot([time[i], time[i]], [lower[i], upper[i]], color='#8C35FF', marker='o', markersize=7.5)

    axis.plot(time, test, label="Observations")
    axis.plot(time, pred, label="Predictions")

    figure.autofmt_xdate()
    axis.legend(fontsize=16)
    config.save_post_asset(figure, "mean_reversion", plot)
Example #9
0
def comparison_plot(title, df, α, β, labels, box_pos, plot):
    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_title(title)
    axis.set_xlabel(r"$t$ (Days)")

    axis.xaxis.set_mactivajor_formatter(mdates.DateFormatter('%m/%d/%y'))

    params = []
    d = ", "
    nα, _ = α.shape
    nβ, _ = β.shape
    for i in range(nα):
        params.append(f"$α_{{{i+1}}}$=[{d.join([format(elem, '2.2f') for elem in numpy.array(α[i]).flatten()])}]")
    for i in range(nβ):
        params.append(f"$β_{{{i+1}}}$=[{d.join([format(elem, '2.2f') for elem in numpy.array(β[i]).flatten()])}]")

    params_string = "\n".join(params)
    bbox = dict(boxstyle='square,pad=1', facecolor="#FEFCEC", edgecolor="#FEFCEC", alpha=0.75)
    axis.text(box_pos[0], box_pos[1], params_string, fontsize=15, bbox=bbox, transform=axis.transAxes)

    vars = df.columns
    for i in range(len(vars)):
        axis.plot(df.index, df[vars[i]], label=labels[i], lw=1)

    figure.autofmt_xdate()
    axis.legend(fontsize=16)
    config.save_post_asset(figure, "mean_reversion", plot)
Example #10
0
def prediction_plot(title, df, pred, lag, var, plot):
    post_fix = ["_prediction", "_lower_bound", "_upper_bound"]

    obs = df[-lag:][var].to_numpy()
    obs_time = df.index[-lag:].to_numpy()

    forecast = pred[var + post_fix[0]].to_numpy()
    forecast_time = pred.index.to_numpy()
    lower = pred[var + post_fix[1]].to_numpy()
    upper = pred[var + post_fix[2]].to_numpy()

    n = len(forecast)

    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_title(title)

    axis.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%y'))

    for i in range(n):
        axis.plot([forecast_time[i], forecast_time[i]], [lower[i], upper[i]], color='#8C35FF', marker='o', markersize=7.5)

    axis.plot(obs_time, obs, label="Observation")
    axis.plot([obs_time[-1], forecast_time[0]], [obs[-1], forecast[0]], color='#FF9500')
    axis.plot(forecast_time, forecast, label="Prediction")

    figure.autofmt_xdate()
    axis.legend(fontsize=16)
    config.save_post_asset(figure, "mean_reversion", plot)
def plot(f, x, title, xlabel, ylabel, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(ylabel)
    axis.set_xlabel(xlabel)
    axis.set_title(title)
    axis.plot(x, f, lw=1)
    config.save_post_asset(figure, "mean_reversion", plot_name)
Example #12
0
def timeseries_comparison_plot(samples, params, tmax, title, plot_name):
    nplot, nsample = samples.shape
    ymin = numpy.amin(samples)
    ymax = numpy.amax(samples)
    figure, axis = pyplot.subplots(nplot, sharex=True, figsize=(12, 9))
    axis[0].set_title(title)
    axis[nplot - 1].set_xlabel(r"$t$")
    time = numpy.linspace(0, tmax - 1, tmax)
    for i in range(nplot):
        stats = f"μ={format(numpy.mean(samples[i]), '2.2f')}\nσ={format(numpy.std(samples[i]), '2.2f')}"
        bbox = dict(boxstyle='square,pad=1',
                    facecolor="#FEFCEC",
                    edgecolor="#FEFCEC",
                    alpha=0.75)
        axis[i].text(0.05,
                     0.75,
                     stats,
                     fontsize=15,
                     bbox=bbox,
                     transform=axis[i].transAxes)
        axis[i].text(0.7,
                     0.75,
                     params[i],
                     fontsize=15,
                     bbox=bbox,
                     transform=axis[i].transAxes)
        axis[i].set_ylabel(r"$x_t$")
        axis[i].set_ylim([ymin, ymax])
        axis[i].set_xlim([0.0, tmax])
        axis[i].plot(time, samples[i, :tmax], lw=1.0)
    config.save_post_asset(figure, "mean_reversion", plot_name)
Example #13
0
def adf_time_series_plot(f, title, plot_name):
    time = numpy.linspace(0.0, len(f)-1, len(f))
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(r"$x_t$")
    axis.set_xlabel(r"$t$")
    axis.set_title(title)
    axis.plot(time, f, lw=1)
    config.save_post_asset(figure, "regression", plot_name)
Example #14
0
def distribution_plot(fx, x, title, ylabel, xlabel, plot_name):
    nplot = len(fx)
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(ylabel)
    axis.set_xlabel(xlabel)
    axis.set_title(title)
    axis.plot(x, fx)
    config.save_post_asset(figure, "regression", plot_name)
Example #15
0
def ensemble_average_plot(mean, title, ylab, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    npts = len(mean)
    time = numpy.linspace(0, npts-1, npts)
    axis.set_xlabel("Time")
    axis.set_ylabel(ylab)
    axis.set_title(title, pad=10)
    axis.plot(time, mean)
    config.save_post_asset(figure, "mean_reversion", plot_name)
Example #16
0
def histogram_plot(x, f, title, ylabel, plot, title_offset=1.0):
    width = 0.9*(x[1]-x[0])
    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_ylabel(ylabel)
    axis.set_xlabel(r"t")
    axis.set_title(title, y=title_offset)
    axis.set_prop_cycle(config.distribution_sample_cycler)
    axis.bar(x, f, align='center', width=width, zorder=10)
    config.save_post_asset(figure, "regression", plot)
Example #17
0
def ensemble_comparison_plot(ensemble_prop, ensemble_time, analytic_prop, analytic_time, title, ylab, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel("Time")
    axis.set_ylabel(ylab)
    axis.set_title(title, pad=10)
    axis.plot(ensemble_time, ensemble_prop, label="Ensemble Average")
    axis.plot(analytic_time, analytic_prop, label="Analytic", marker='o', linestyle="None", markeredgewidth=1.0, markersize=15.0)
    axis.legend()
    config.save_post_asset(figure, "mean_reversion", plot_name)
def ensemble_std_plot(H, std, time, lengend_location, title, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    step = int(len(time) / 10)
    axis.set_xlabel("Time")
    axis.set_title(title)
    axis.plot(time, std, label="Ensemble Average")
    axis.plot(time[::step], time[::step]**H, label=r"$t^{H}$", marker='o', linestyle="None", markeredgewidth=1.0, markersize=15.0)
    axis.legend(bbox_to_anchor=lengend_location)
    config.save_post_asset(figure, "brownian_motion", plot_name)
Example #19
0
def comparison_multiplot(samples, time, labels, lengend_location, title,
                         plot_name):
    nplot = len(samples)
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel("Time")
    axis.set_title(title)
    for i in range(nplot):
        axis.plot(time, samples[i], lw=1, label=labels[i])
    axis.legend(ncol=2, bbox_to_anchor=lengend_location)
    config.save_post_asset(figure, "brownian_motion", plot_name)
def comparison_plot(title, samples, labels, plot):
    nplot, nsamples = samples.shape
    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_title(title)
    axis.set_xlabel(r"$t$")
    axis.set_xlim([0, nsamples-1])
    for i in range(nplot):
        axis.plot(range(nsamples), samples[i], label=labels[i], lw=1)
    axis.legend(fontsize=16)
    config.save_post_asset(figure, "mean_reversion", plot)
Example #21
0
def autocor(title, samples, Δt, max_lag, plot):
    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_title(title)
    axis.set_ylabel(r"$\gamma_{\tau}$")
    axis.set_xlabel("Time Lag (τ)")
    axis.set_xlim([0, Δt * max_lag])
    axis.set_ylim([-1.05, 1.0])
    ac = stats.autocorrelate(samples)
    axis.plot(Δt * numpy.array(range(max_lag)), numpy.real(ac[:max_lag]))
    config.save_post_asset(figure, "brownian_motion", plot)
Example #22
0
def autocorrelation_plot(samples, max_lag, title, ylim, plot):
    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_title(title)
    axis.set_ylabel(r"$\gamma_{\tau}$")
    axis.set_xlabel("Time Lag (τ)")
    axis.set_xlim([-1.0, max_lag])
    axis.set_ylim(ylim)
    ac = autocorrelate(samples)
    axis.plot(range(max_lag), numpy.real(ac[:max_lag]))
    config.save_post_asset(figure, "regression", plot)
def vr_plot(s_var, s, h, title, plot_name):
    var = s**(2.0*h)
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_ylabel(r"Variance")
    axis.set_xlabel(r"$s$")
    axis.set_title(title)
    axis.loglog(s, s_var, label=r"$\sigma^2(s)$", marker='o', markersize=5.0, zorder=10, linestyle="None", markeredgewidth=1.0, alpha=0.75)
    axis.loglog(s, var, label=r"$s^{2H}$", zorder=5)
    axis.legend(bbox_to_anchor=[0.8, 0.8])
    config.save_post_asset(figure, "regression", plot_name)
Example #24
0
def plot(df, title, plot_name):
    figure, axis = pyplot.subplots(figsize=(10, 8))
    figure.autofmt_xdate()
    data = df[df.columns[0]]
    axis.plot(data)
    axis.set_title(title)
    axis.set_xlabel(df.index.name, labelpad=10)
    axis.set_ylabel(df.columns[0], labelpad=10)
    pyplot.tight_layout(pad=1.0)
    config.save_post_asset(figure, "mean_reversion", plot_name)
Example #25
0
def pcf_plot(title, samples, max_lag, plot):
    figure, axis = pyplot.subplots(figsize=(10, 7))
    pacf_values = pacf(samples, max_lag)
    axis.set_title(title)
    axis.set_xlabel("Time Lag (τ)")
    axis.set_xlim([-0.1, max_lag])
    axis.set_ylim([-1.1, 1.1])
    axis.plot(range(max_lag + 1), pacf_values)
    axis.plot([0.0, max_lag], [0.0, 0.0], lw=4.0, color='black')
    config.save_post_asset(figure, "mean_reversion", plot)
def time_series_plot(samples, time, text_pos, title, ylabel, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    stats=f"Simulation Stats\n\nμ={format(numpy.mean(samples), '2.2f')}\nσ={format(numpy.var(samples), '2.2f')}"
    bbox = dict(boxstyle='square,pad=1', facecolor="#FEFCEC", edgecolor="#FEFCEC", alpha=0.75)
    axis.text(text_pos[0], text_pos[1], stats, fontsize=15, bbox=bbox)
    axis.set_ylabel(ylabel)
    axis.set_xlabel(r"$t$")
    axis.set_title(title)
    axis.plot(time, samples, lw=1)
    config.save_post_asset(figure, "mean_reversion", plot_name)
def ensemble_autocorrelation_plot(H, ac, time, lengend_location, title, plot_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    step = int(len(time) / 10)
    axis.set_ylim([-1.0, 1.0])
    axis.set_xlabel("Time")
    axis.set_title(title)
    label = r"$\frac{1}{2}[(t-1)^{2H} + (t+1)^{2H} - 2t^{2H})]$"
    axis.plot(time, ac, label="Ensemble Average")
    axis.plot(time[1::step], bm.fbn_autocorrelation(H, time[1::step]), label=label, marker='o', linestyle="None", markeredgewidth=1.0, markersize=15.0)
    axis.legend(bbox_to_anchor=lengend_location)
    config.save_post_asset(figure, "brownian_motion", plot_name)
def ensemble_plot(samples, time, text_pos, title, plot_name):
    nsim, npts = samples.shape
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel("Time")
    axis.set_title(title)
    stats=f"Simulation Stats\n\nμ={format(numpy.mean(samples[:,-1]), '2.2f')}\nσ={format(numpy.std(samples[:,-1]), '2.2f')}"
    bbox = dict(boxstyle='square,pad=1', facecolor="#FEFCEC", edgecolor="#FEFCEC", alpha=0.75)
    axis.text(text_pos[0], text_pos[1], stats, fontsize=15, bbox=bbox)
    for i in range(nsim):
        axis.plot(time, samples[i], lw=1)
    config.save_post_asset(figure, "brownian_motion", plot_name)
Example #29
0
def fbn_autocorrelation_limit(H_vals, time, lengend_location, file_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel(r"$n$")
    axis.set_ylabel(r"$r_{n}^{H}$")
    axis.set_title(r"Fraction Brownian Motion Autocorrelation for $n\gg1$, $r^{H}_{n}=H(2H-1)n^{2H-2}$")

    for H in H_vals:
        axis.plot(time, brownian_motion.fbn_autocorrelation_large_n(H, time), label=f"H={format(H, '1.2f')}")

    axis.legend(ncol=2, bbox_to_anchor=lengend_location)
    config.save_post_asset(figure, "brownian_motion", file_name)
Example #30
0
def fbn_autocorrelation_plot(H_vals, time, lengend_location, file_name):
    figure, axis = pyplot.subplots(figsize=(12, 8))
    axis.set_xlabel(r"$n$")
    axis.set_ylabel(r"$r_{n}^{H}$")
    axis.set_title(r"Fraction Brownian Motion Autocovariance, $r^{H}_{n}=\frac{1}{2}[(n-1)^{2H} + (n+1)^{2H} - 2n^{2H}]$")

    for H in H_vals:
        axis.plot(time, brownian_motion.fbn_autocorrelation(H, time), label=f"H={format(H, '1.2f')}")

    axis.legend(ncol=2, bbox_to_anchor=lengend_location)
    config.save_post_asset(figure, "brownian_motion", file_name)