Python numba.f8() Examples

The following are 30 code examples of numba.f8(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numba , or try the search function .
Example #1
Source File: helpers_numba.py    From timezonefinder with MIT License 6 votes vote down vote up
def all_the_same(pointer, length, id_list):
    """
    :param pointer: starting from that element the list is being checked for equality of its elements
    :param length:
    :param id_list: List mustn't be empty or Null. There has to be at least one element
    :return: returns the first encountered element if starting from the pointer all elements are the same,
     otherwise it returns -1
    """
    element = id_list[pointer]
    pointer += 1
    while pointer < length:
        if element != id_list[pointer]:
            return -1
        pointer += 1
    return element


# @cc.export('cartesian2rad', dtype_2float_tuple(f8, f8, f8)) 
Example #2
Source File: nb.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def rolling_max_1d_nb(a, window, minp=None):
    """Return rolling max.

    Numba equivalent to `pd.Series(a).rolling(window, min_periods=minp).max()`."""
    if minp is None:
        minp = window
    if minp > window:
        raise Exception("minp must be <= window")
    result = np.empty_like(a, dtype=f8)
    for i in range(a.shape[0]):
        maxv = a[i]
        cnt = 0
        for j in range(max(i-window+1, 0), i+1):
            if np.isnan(a[j]):
                continue
            if np.isnan(maxv) or a[j] > maxv:
                maxv = a[j]
            cnt += 1
        if cnt < minp:
            result[i] = np.nan
        else:
            result[i] = maxv
    return result 
Example #3
Source File: nb.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def rolling_min_1d_nb(a, window, minp=None):
    """Return rolling min.

    Numba equivalent to `pd.Series(a).rolling(window, min_periods=minp).min()`."""
    if minp is None:
        minp = window
    if minp > window:
        raise Exception("minp must be <= window")
    result = np.empty_like(a, dtype=f8)
    for i in range(a.shape[0]):
        minv = a[i]
        cnt = 0
        for j in range(max(i-window+1, 0), i+1):
            if np.isnan(a[j]):
                continue
            if np.isnan(minv) or a[j] < minv:
                minv = a[j]
            cnt += 1
        if cnt < minp:
            result[i] = np.nan
        else:
            result[i] = minv
    return result 
Example #4
Source File: nb.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def expanding_min_1d_nb(a, minp=1):
    """Return expanding min.

    Numba equivalent to `pd.Series(a).expanding(min_periods=minp).min()`."""
    result = np.empty_like(a, dtype=f8)
    minv = a[0]
    cnt = 0
    for i in range(a.shape[0]):
        if np.isnan(minv) or a[i] < minv:
            minv = a[i]
        if ~np.isnan(a[i]):
            cnt += 1
        if cnt < minp:
            result[i] = np.nan
        else:
            result[i] = minv
    return result 
Example #5
Source File: nb.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def describe_reduce_nb(col, a, percentiles, ddof):
    """Return descriptive statistics.

    Numba equivalent to `pd.Series(a).describe(percentiles)`."""
    a = a[~np.isnan(a)]
    result = np.empty(5 + len(percentiles), dtype=f8)
    result[0] = len(a)
    if len(a) > 0:
        result[1] = np.mean(a)
        result[2] = nanstd_1d_nb(a, ddof=ddof)
        result[3] = np.min(a)
        result[4:-1] = np.percentile(a, percentiles * 100)
        result[4+len(percentiles)] = np.max(a)
    else:
        result[1:] = np.nan
    return result 
Example #6
Source File: nb.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def reduce_records_nb(records, n_cols, default_val, reduce_func_nb, *args):
    """Reduce records by column.

    Faster than `map_records_to_matrix_nb` and `vbt.tseries.*` used together, and also
    requires less memory. But does not take advantage of caching.

    `reduce_func_nb` must accept an array of records and `*args`, and return a single value."""
    result = np.full(n_cols, default_val, dtype=f8)
    from_r = 0
    col = -1
    for r in range(records.shape[0]):
        record_col = records['col'][r]
        if record_col != col:
            if col != -1:
                # At the beginning of second column do reduce on the first
                result[col] = reduce_func_nb(records[from_r:r], *args)
            from_r = r
            col = record_col
        if r == len(records) - 1:
            result[col] = reduce_func_nb(records[from_r:r + 1], *args)
    return result 
Example #7
Source File: nb.py    From vectorbt with GNU General Public License v3.0 6 votes vote down vote up
def map_reduce_between_two_nb(a, b, map_func_nb, reduce_func_nb, *args):
    """Map using `map_func_nb` and reduce using `reduce_func_nb` each consecutive 
    pair of `True` values between `a` and `b`.

    Iterates over `b`, and for each found `True` value, looks for the preceding `True` value in `a`.

    `map_func_nb` and `reduce_func_nb` are same as for `map_reduce_between_nb`."""
    result = np.full((a.shape[1],), np.nan, dtype=f8)

    for col in range(a.shape[1]):
        a_idxs = np.flatnonzero(a[:, col])
        if a_idxs.shape[0] > 0:
            b_idxs = np.flatnonzero(b[:, col])
            if b_idxs.shape[0] > 0:
                map_res = np.empty(b_idxs.shape)
                k = 0
                for j, to_i in enumerate(b_idxs):
                    valid_a_idxs = a_idxs[a_idxs < to_i]
                    if len(valid_a_idxs) > 0:
                        from_i = valid_a_idxs[-1]  # preceding in a
                        map_res[k] = map_func_nb(col, from_i, to_i, *args)
                        k += 1
                if k > 0:
                    result[col] = reduce_func_nb(col, map_res[:k], *args)
    return result 
Example #8
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def groupby_apply_matrix_nb(a, groups, apply_func_nb, *args):
    """`groupby_apply_nb` with `apply_func_nb` being applied on all columns at once.

    `apply_func_nb` must accept indices of the current group, the 2-dim array, and `*args`. 
    Must return a single value or an array of shape `a.shape[1]`."""
    result = np.empty((len(groups), a.shape[1]), dtype=f8)
    for i, idxs in groups.items():
        result[i, :] = apply_func_nb(idxs, a[idxs, :], *args)
    return result

# ############# Map, filter and reduce ############# # 
Example #9
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def value_at_risk_nb(returns, cutoff_arr):
    """2-dim version of `value_at_risk_1d_nb`.

    `cutoff_arr` should be an array of shape `returns.shape[1]`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = value_at_risk_1d_nb(returns[:, col], cutoff=cutoff_arr[col])
    return result 
Example #10
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def filter_nb(a, filter_func_nb, *args):
    """Filter non-NA elements elementwise using `filter_func_nb`. 
    The filtered out elements will become NA.

    `filter_func_nb` must accept index of the current column, index of the current element, 
    the element itself, and `*args`. Must return a boolean value."""
    result = a.astype(f8)

    for col in range(result.shape[1]):
        idxs = np.flatnonzero(~np.isnan(a[:, col]))
        for i in idxs:
            if ~filter_func_nb(col, i, a[i, col], *args):
                result[i, col] = np.nan
    return result 
Example #11
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def apply_and_reduce_nb(a, apply_func_nb, reduce_func_nb, *args):
    """Apply `apply_func_nb` on each column and reduce into a single value using `reduce_func_nb`.

    `apply_func_nb` must accept index of the current column, the column itself, and `*args`. 
    Must return an array.

    `reduce_func_nb` must accept index of the current column, the array of results from 
    `apply_func_nb` for that column, and `*args`. Must return a single value."""
    result = np.full(a.shape[1], np.nan, dtype=f8)

    for col in range(a.shape[1]):
        mapped = apply_func_nb(col, a[:, col], *args)
        result[col] = reduce_func_nb(col, mapped, *args)
    return result 
Example #12
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def reduce_nb(a, reduce_func_nb, *args):
    """Reduce each column into a single value using `reduce_func_nb`.

    `reduce_func_nb` must accept index of the current column, the array, and `*args`. 
    Must return a single value."""
    result = np.full(a.shape[1], np.nan, dtype=f8)

    for col in range(a.shape[1]):
        result[col] = reduce_func_nb(col, a[:, col], *args)
    return result 
Example #13
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def reduce_to_array_nb(a, reduce_func_nb, *args):
    """Reduce each column into an array of values using `reduce_func_nb`.

    `reduce_func_nb` same as for `reduce_a` but must return an array.

    !!! note
        * Output of `reduce_func_nb` must be strictly homogeneous"""
    result0 = reduce_func_nb(0, a[:, 0], *args)
    result = np.full((result0.shape[0], a.shape[1]), np.nan, dtype=f8)
    for col in range(a.shape[1]):
        result[:, col] = reduce_func_nb(col, a[:, col], *args)
    return result 
Example #14
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def alpha_nb(returns, factor_returns, ann_factor, risk_free_arr):
    """2-dim version of `alpha_1d_nb`.

    `risk_free_arr` should be an array of shape `returns.shape[1]`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = alpha_1d_nb(returns[:, col], factor_returns[:, col], ann_factor, risk_free=risk_free_arr[col])
    return result 
Example #15
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def sharpe_ratio_nb(returns, ann_factor, risk_free_arr):
    """2-dim version of `sharpe_ratio_1d_nb`.

    `risk_free_arr` should be an array of shape `returns.shape[1]`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = sharpe_ratio_1d_nb(returns[:, col], ann_factor, risk_free=risk_free_arr[col])
    return result 
Example #16
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def downside_risk_nb(returns, ann_factor, required_return_arr):
    """2-dim version of `downside_risk_1d_nb`.

    `required_return_arr` should be an array of shape `returns.shape[1]`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = downside_risk_1d_nb(returns[:, col], ann_factor, required_return=required_return_arr[col])
    return result 
Example #17
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def information_ratio_nb(returns, factor_returns):
    """2-dim version of `information_ratio_1d_nb`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = information_ratio_1d_nb(returns[:, col], factor_returns[:, col])
    return result 
Example #18
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def beta_nb(returns, factor_returns):
    """2-dim version of `beta_1d_nb`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = beta_1d_nb(returns[:, col], factor_returns[:, col])
    return result 
Example #19
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def groupby_apply_nb(a, groups, apply_func_nb, *args):
    """Provide group-by calculations.

    `groups` must be a dictionary, where each key is an index that points to an element in the new array 
    where a group-by result will be stored, while the value should be an array of indices in `a`
    to apply `apply_func_nb` on.

    `apply_func_nb` must accept index of the current column, indices of the current group, 
    the array, and `*args`. Must return a single value."""
    result = np.empty((len(groups), a.shape[1]), dtype=f8)
    for col in range(a.shape[1]):
        for i, idxs in groups.items():
            result[i, col] = apply_func_nb(col, idxs, a[idxs, col], *args)
    return result 
Example #20
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def rolling_apply_matrix_nb(a, window, apply_func_nb, *args):
    """`rolling_apply_nb` with `apply_func_nb` being applied on all columns at once.

    `apply_func_nb` must accept index of the current row, the 2-dim array, and `*args`. 
    Must return a single value or an array of shape `a.shape[1]`."""
    result = np.empty_like(a, dtype=f8)
    for i in range(a.shape[0]):
        window_a = a[max(0, i+1-window):i+1, :]
        result[i, :] = apply_func_nb(i, window_a, *args)
    return result 
Example #21
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def rolling_apply_nb(a, window, apply_func_nb, *args):
    """Provide rolling window calculations.

    `apply_func_nb` must accept index of the current column, index of the current row, 
    the array, and `*args`. Must return a single value."""
    result = np.empty_like(a, dtype=f8)
    for col in range(a.shape[1]):
        for i in range(a.shape[0]):
            window_a = a[max(0, i+1-window):i+1, col]
            result[i, col] = apply_func_nb(col, i, window_a, *args)
    return result 
Example #22
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def expanding_max_nb(a, minp=1):
    """2-dim version of `expanding_max_1d_nb`."""
    result = np.empty_like(a, dtype=f8)
    for col in range(a.shape[1]):
        result[:, col] = expanding_max_1d_nb(a[:, col], minp=minp)
    return result 
Example #23
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def expanding_min_nb(a, minp=1):
    """2-dim version of `expanding_min_1d_nb`."""
    result = np.empty_like(a, dtype=f8)
    for col in range(a.shape[1]):
        result[:, col] = expanding_min_1d_nb(a[:, col], minp=minp)
    return result 
Example #24
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def ewm_std_nb(a, span, minp=None):
    """2-dim version of `ewm_std_1d_nb`."""
    result = np.empty_like(a, dtype=f8)
    for col in range(a.shape[1]):
        result[:, col] = ewm_std_1d_nb(a[:, col], span, minp=minp)
    return result

# ############# Expanding functions ############# # 
Example #25
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def map_records_to_matrix_nb(records, target_shape, default_val, map_func_nb, *args):
    """Map each record to a value and store it in a matrix.

    `map_func_nb` must accept a single record and `*args`, and return a single value."""
    result = np.full(target_shape, default_val, dtype=f8)
    for r in range(records.shape[0]):
        result[records['idx'][r], records['col'][r]] = map_func_nb(records[r], *args)
    return result 
Example #26
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def convert_array_to_matrix(a, records, target_shape, default_val):
    """Convert a 1-dim array already mapped by the user."""

    result = np.full(target_shape, default_val, dtype=f8)
    for r in range(a.shape[0]):
        result[records['idx'][r], records['col'][r]] = a[r]
    return result


# ############# Reducing ############# # 
Example #27
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def ewm_mean_nb(a, span, minp=None):
    """2-dim version of `ewm_mean_1d_nb`."""
    result = np.empty_like(a, dtype=f8)
    for col in range(a.shape[1]):
        result[:, col] = ewm_mean_1d_nb(a[:, col], span, minp=minp)
    return result 
Example #28
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def ewm_mean_1d_nb(a, span, minp=None):
    """Return exponential weighted average.

    Numba equivalent to `pd.Series(a).ewm(span=span, min_periods=minp).mean()`.

    Adaptation of `pandas._libs.window.aggregations.window_aggregations.ewma` with default arguments."""
    if minp is None:
        minp = span
    if minp > span:
        raise Exception("minp must be <= span")
    N = len(a)
    result = np.empty(N, dtype=f8)
    if N == 0:
        return result
    com = (span - 1) / 2.0
    alpha = 1. / (1. + com)
    old_wt_factor = 1. - alpha
    new_wt = 1.
    weighted_avg = a[0]
    is_observation = (weighted_avg == weighted_avg)
    nobs = int(is_observation)
    result[0] = weighted_avg if (nobs >= minp) else np.nan
    old_wt = 1.

    for i in range(1, N):
        cur = a[i]
        is_observation = (cur == cur)
        nobs += is_observation
        if weighted_avg == weighted_avg:
            old_wt *= old_wt_factor
            if is_observation:
                # avoid numerical errors on constant series
                if weighted_avg != cur:
                    weighted_avg = ((old_wt * weighted_avg) + (new_wt * cur)) / (old_wt + new_wt)
                old_wt += new_wt
        elif is_observation:
            weighted_avg = cur
        result[i] = weighted_avg if (nobs >= minp) else np.nan
    return result 
Example #29
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def rolling_mean_nb(a, window, minp=None):
    """2-dim version of `rolling_mean_1d_nb`."""
    result = np.empty_like(a, dtype=f8)
    for col in range(a.shape[1]):
        result[:, col] = rolling_mean_1d_nb(a[:, col], window, minp=minp)
    return result 
Example #30
Source File: nb.py    From vectorbt with GNU General Public License v3.0 5 votes vote down vote up
def down_capture_nb(returns, factor_returns, ann_factor):
    """2-dim version of `down_capture_1d_nb`."""
    result = np.empty(returns.shape[1], dtype=f8)
    for col in range(returns.shape[1]):
        result[col] = down_capture_1d_nb(returns[:, col], factor_returns[:, col], ann_factor)
    return result