Пример #1
0
def parade(p: dict, x: [float], y: float):
    """ Process an observation y
    :param p:   state - supply empty dict on first call
    :param y:   incoming observation
    :param x:   term structure of predictions out k steps ahead, made after y received
    returns:  mean, std, of model residuals and the posterior state s'
    """

    # Initialize
    if not p:
        k = len(x)
        p = {
            'predictions': [[] for _ in range(k)],  # Holds the cavalcade
            'moments': [var_init() for _ in range(k)]
        }  # Could use kurtosis_init here for more moment
    else:
        assert len(x) == len(p['predictions'])  # 'k' is immutable

    assessable = p['predictions'].pop(0)
    if assessable:
        for j, xi in assessable:
            p['moments'][j] = var_update(p['moments'][j], y - xi)

    p['predictions'].append(list())
    for j, xj in enumerate(x):
        p['predictions'][j].append((j, xj))

    return parade_mean(p), parade_std(p), p
Пример #2
0
def parade(p:dict, x:Union[List[SupportsFloat],None], y:Union[SupportsFloat,None]):
    """ Process an observation y
    :param p:   state - supply empty dict on first call
    :param y:   incoming observation
    :param x:   term structure of predictions out k steps ahead, made after y received
    returns:  mean, std, of model residuals and the posterior state s'

    A special convention allows the caller to reset the empirical moments. Pass x=None and y=None

    """
    # Initialize
    if not p:
        k = len(x)
        p = {'predictions': [[] for _ in range(k)],  # Holds the cavalcade
                'moments': [var_init() for _ in range(k)]}  # Could use kurtosis_init here for more moment
    else:
        assert len(x) == len(p['predictions']) # 'k' is immutable

    if x is None and y is None:
        # "reset" the running moments but keep prediction parade
        p_mean, p_std = parade_mean(p), parade_std(p)
        p['moments'] = [var_init() for _ in range(k)]
        return p_mean, p_std, p
    else:
        assessable = p['predictions'].pop(0)
        if assessable:
            for j,xi in assessable:
                p['moments'][j] = var_update(p['moments'][j], y - xi)

        p['predictions'].append(list())
        for j, xj in enumerate(x):
            p['predictions'][j].append((j, xj))

        return parade_mean(p), parade_std(p), p
Пример #3
0
                n_train) + '_k_' + str(k) + '_multiple_' + str(
                    round(multiple, 1))
            try:
                os.mkdir(jpg_path)
            except FileExistsError:
                pass
            jpg_file = jpg_path + os.path.sep + 'rel=' + str(
                rel_error) + '_name=' + name.replace('.json', '.jpg')
            plt.savefig(jpg_file)
            plt.close()
            print(jpg_file)

        return err, lv_err, jpg_path


if __name__ == '__main__':
    from momentum import var_init, var_update
    import json
    lv = var_init()
    pr = var_init()
    params = {'multiple': 1.5, 'n_recent': 5, 'n_train': 500, 'k': 20}
    while True:
        err, lv_err, jpg_path = plot_next_optionated_forecast(**params)
        report = params.copy()
        report['simple_average'] = var_update(lv, lv_err)
        report['prophet'] = var_update(pr, err)
        pprint(report)
        report_file = jpg_path + '_summary.json'
        with open(report_file, 'wt') as f:
            json.dump(report, f)