import numpy as np


# --------------------------------------------------------------------------
#            Diagnostics
# --------------------------------------------------------------------------
# Bit of a placeholder for differencing tests

def sign_changes(lagged):
    return np.nansum([abs(d) > 1.5 for d in np.diff(np.sign(list(lagged) + [0., 0.]))])


def is_process(lagged):
    return sign_changes(np.diff(lagged)) > 2 * sign_changes(lagged)


# --------------------------------------------------------------------------
#            Monte Carlo
# --------------------------------------------------------------------------

def inv_cdf_walk(inv_cdf, k: int, x0: float) -> float:
    """ Generate sample from sum of independent increments generated by inverse cdf function
         inv_cdf  func   taking (0,1) --> R
         k        int    Number of steps
         x0       float  Starting value
    """
    # See crawler_examples/decastyle_cat for usage example
    x = x0
    for _ in range(k):
        u = np.random.rand()
        dx = inv_cdf(u)
        x = x + dx
    return x
