Esempio n. 1
0
def fox_sample(lagged_values, lagged_times, delay, num, name, as_process=None):
    " Elementary but not completely woeful sampler, used by Malaxable Fox"
    dt = approx_dt(lagged_times)
    lag = max(10, math.ceil(delay / dt))
    print('lag = ' + str(lag))
    is_proc = as_process or ('~' not in name and StatsConventions.is_process(lagged_values))
    if len(lagged_values) < 250 + lag or not is_proc:
        values = exponential_bootstrap(lagged=lagged_values, decay=0.1, num=num, as_process=as_process)
        ret_values = StatsConventions.nudged(project_on_lagged_lattice(values=values, lagged_values=lagged_values))
    else:
        changes = np.diff(list(reversed(lagged_values)), n=lag)
        counter = dict(Counter(changes))
        d = dict(counter)
        num_total = len(changes)
        d1 = dict([(change, round(175 * change_count / num_total)) for change, change_count in d.items()])
        values = list()
        for change, rounded_count in d1.items():
            values.extend([change] * rounded_count)
        change_spray = list(range(-50, 50))
        values.extend(change_spray)
        change_values = values[:num]
        abs_values = [lagged_values[0] + chg for chg in change_values]
        if not len(abs_values) == num:
            # Too many rounded down ... may not be discrete
            abs_values = exponential_bootstrap(lagged=lagged_values, decay=0.1, num=num, as_process=True)
        ret_values = StatsConventions.nudged(project_on_lagged_lattice(values=abs_values, lagged_values=lagged_values))

    return ret_values
Esempio n. 2
0
def independent_bootstrap(lagged, decay, num):
    """ One parameter jiggled bootstrap favouring more recent observations
          lagged  [ float ]     List most recent observation first
          decay    float        Coefficient in exp(-a k) that weights samples
          num      int          Number of scenarios requested
           :returns  [ float ]  Statistical sample
    """
    weights = list(np.exp([-decay * k for k in range(len(lagged))]))
    empirical_sample = _weighted_random_sample(population=lagged, weights=weights, num=num)
    return StatsConventions.nudged(empirical_sample)