Ejemplo n.º 1
0
def hurst_exponent(x: np.array) -> float:
    """Computes hurst exponent.

    Parameters
    ----------
    x: numpy array
        Time series.

    References
    ----------
    [1] Taken from https://gist.github.com/alexvorndran/aad69fa741e579aad093608ccaab4fe1
    [2] Based on https://codereview.stackexchange.com/questions/224360/hurst-exponent-calculator
    """
    n = x.size  # num timesteps
    t = np.arange(1, n + 1)
    y = x.cumsum()  # marginally more efficient than: np.cumsum(sig)
    mean_t = y / t  # running mean

    s_t = np.sqrt(
        np.array([np.mean((x[:i + 1] - mean_t[i])**2) for i in range(n)]))
    r_t = np.array(
        [np.ptp(y[:i + 1] - t[:i + 1] * mean_t[i]) for i in range(n)])

    with np.errstate(invalid='ignore'):
        r_s = r_t / s_t

    r_s = np.log(r_s)[1:]
    n = np.log(t)[1:]
    a = np.column_stack((n, np.ones(n.size)))
    hurst_exponent, _ = np.linalg.lstsq(a, r_s, rcond=-1)[0]

    return hurst_exponent
Ejemplo n.º 2
0
def _downsample_array(
    col: np.array,
    target: int,
    random_state: Optional[Union[int, RandomState]] = 0,
    replace: bool = True,
    inplace: bool = False,
):
    """\
    Evenly reduce counts in cell to target amount.

    This is an internal function and has some restrictions:

    * `dtype` of col must be an integer (i.e. satisfy issubclass(col.dtype.type, np.integer))
    * total counts in cell must be less than target
    """
    np.random.seed(random_state)
    cumcounts = col.cumsum()
    if inplace:
        col[:] = 0
    else:
        col = np.zeros_like(col)
    total = cumcounts[-1]
    sample = np.random.choice(total, target, replace=replace)
    sample.sort()
    geneptr = 0
    for count in sample:
        while count >= cumcounts[geneptr]:
            geneptr += 1
        col[geneptr] += 1
    return col
Ejemplo n.º 3
0
def _instance_iou_cpu(
    instance_idx,
    instance_offsets,
    gt_instances,
    gt_instance_sizes,
    num_gt_instances: np.array,
    batch: np.array,
):
    num_proposed_instances = len(instance_offsets) - 1
    iou = np.zeros((num_proposed_instances, num_gt_instances.sum()))
    offset_num_gt_instances = np.concatenate(
        (np.array([0]), num_gt_instances.cumsum()))
    for proposed_instance in range(num_proposed_instances):
        instance = instance_idx[instance_offsets[proposed_instance]:
                                instance_offsets[proposed_instance + 1]]
        sample_idx = batch[instance[0]]
        gt_count_offset = offset_num_gt_instances[sample_idx]
        sample_instance_count = num_gt_instances[sample_idx]
        for instance_id in numba.prange(1, sample_instance_count + 1):
            intersection = 0
            for idx in instance:
                if gt_instances[idx] == instance_id:
                    intersection += 1
            iou[proposed_instance,
                gt_count_offset + instance_id - 1] = intersection / float(
                    len(instance) +
                    gt_instance_sizes[gt_count_offset + instance_id - 1] -
                    intersection)
    return iou
Ejemplo n.º 4
0
def draw_cleanup(draws: np.array, x: np.array,
                 df: pd.DataFrame) -> pd.DataFrame:
    # set to linear, add up cumulative, and create dataframe
    draws -= np.var(draws, axis=1, keepdims=True) / 2
    draws = np.exp(draws)
    draws[draws * df['population'].values[0] <
          1e-10] = 1e-10 / df['population'].values[0]
    draws = draws.cumsum(axis=0)

    # store in dataframe
    draw_df = pd.DataFrame({
        'location_id':
        df['location_id'].unique().item(),
        'Date': [df['Date'].min() + pd.Timedelta(days=dx) for dx in x],
        'population':
        df['population'].unique().item()
    })
    draw_df = pd.concat([
        draw_df,
        pd.DataFrame(draws,
                     columns=[f'draw_{d}' for d in range(draws.shape[1])])
    ],
                        axis=1)

    return draw_df
Ejemplo n.º 5
0
def errors(ws: array, e_share=0.0) -> array:
    """
    Returns a stake-weighted population of adverserial members,
    with `e_share` being the *percentage* of the total weights.
    """
    return ws.cumsum() < e_share