Exemplo n.º 1
0
def case_state_solution_based(rng: np.random.Generator, ):
    """State of a solution-based linear solver."""
    initial_state = linalg.solvers.LinearSolverState(problem=linsys,
                                                     prior=prior)
    initial_state.action = rng.standard_normal(
        size=initial_state.problem.A.shape[1])
    initial_state.observation = rng.standard_normal()

    return initial_state
Exemplo n.º 2
0
def block(gen: np.random.Generator, statistic: str, num: int, trend: str) -> NDArray:
    max_sample = max(SAMPLE_SIZES)
    e = gen.standard_normal((num, max_sample, MAX_STOCHASTIC_TRENDS))
    z = e.cumsum(axis=1)
    columns = DF_Z_COLUMNS if statistic == "a" else DF_P_COLUMNS
    results = np.empty((num, len(columns)))
    loc = 0
    for ss in SAMPLE_SIZES:
        for ns in range(1, MAX_STOCHASTIC_TRENDS + 1):
            omega_dof = ss - 2 * ns - len(trend)
            z_a = z_t = p_u = p_z = np.full(z.shape[0], np.nan)
            if omega_dof >= 20:
                if statistic == "z":
                    z_a, z_t = z_tests_vec(z[:, :ss, :ns], 0, trend=trend)
                elif statistic == "p":
                    p_u, p_z = p_tests_vec(z[:, :ss, :ns], 0, trend=trend)
                else:
                    raise ValueError(f"statistic must be a or p, saw {statistic}")
            if statistic == "z":
                stats = np.column_stack([z_a, z_t])
            else:  # p
                stats = np.column_stack([p_u, p_z])
            stride = stats.shape[1]
            results[:, loc : loc + stride] = stats
            loc += stride
    return results
Exemplo n.º 3
0
def test_real_forward_adjoint(rng: np.random.Generator, method, order):
    shape = ssht.sample_shape(order, Method=method)
    f = rng.standard_normal(shape, dtype="float64")
    flm = ssht.forward(f, order, Reality=True, Method=method)
    f = ssht.inverse(flm, order, Reality=True, Method=method)

    f_prime = rng.standard_normal(shape, dtype="float64")
    flm_prime = ssht.forward(f_prime, order, Reality=True, Method=method)
    f_prime = ssht.forward_adjoint(flm_prime,
                                   order,
                                   Reality=True,
                                   Method=method,
                                   backend="ducc")

    assert flm_prime.conj() @ flm == approx(
        f_prime.flatten().conj() @ f.flatten())
Exemplo n.º 4
0
def case_state(
    rng: np.random.Generator,
):
    """State of a linear solver."""
    state = linalg.solvers.LinearSolverState(problem=linsys, prior=prior, rng=rng)
    state.action = rng.standard_normal(size=state.problem.A.shape[1])

    return state
Exemplo n.º 5
0
def test_forward_adjoint(rng: np.random.Generator, spin, method, order):
    flm = rng.standard_normal(
        (order * order, 2), dtype="float64") @ np.array([1, 1j])
    flm[0:spin * spin] = 0.0
    f = ssht.inverse(flm, order, Spin=spin, Method=method, backend="ducc")

    flm_prime = rng.standard_normal(
        (order * order, 2), dtype="float64") @ np.array([1, 1j])
    flm_prime[0:spin * spin] = 0.0
    f_prime = ssht.forward_adjoint(flm_prime,
                                   order,
                                   Spin=spin,
                                   Method=method,
                                   backend="ducc")

    assert flm_prime.conj() @ flm == approx(
        f_prime.flatten().conj() @ f.flatten())
Exemplo n.º 6
0
def case_state_symmetric_matrix_based(rng: np.random.Generator, ):
    """State of a symmetric matrix-based linear solver."""
    prior = linalg.solvers.beliefs.LinearSystemBelief(
        A=randvars.Normal(
            mean=linops.Matrix(linsys.A),
            cov=linops.SymmetricKronecker(A=linops.Identity(n)),
        ),
        x=(Ainv @ b[:, None]).reshape((n, )),
        Ainv=randvars.Normal(
            mean=linops.Identity(n),
            cov=linops.SymmetricKronecker(A=linops.Identity(n)),
        ),
        b=b,
    )
    state = linalg.solvers.LinearSolverState(problem=linsys, prior=prior)
    state.action = rng.standard_normal(size=state.problem.A.shape[1])
    state.observation = rng.standard_normal(size=state.problem.A.shape[1])

    return state
Exemplo n.º 7
0
def add_noise_column(X: pd.DataFrame,
                     rng: np.random.Generator,
                     noise_columns: List[str] = None,
                     count: int = 1) -> Tuple[pd.DataFrame, List[str]]:
    """
    Create a copy of dataset X with extra synthetic columns generated from standard normal distribution.
    """
    X = X.copy()
    if noise_columns is None:
        noise_columns = [str(uuid.uuid4()) for _ in range(1, count + 1)]
    for col_name in noise_columns:
        noise = rng.standard_normal(len(X))
        X[col_name] = noise
    return X, noise_columns
Exemplo n.º 8
0
def sample_wiener(
    t: np.ndarray,
    init_x: np.ndarray,
    mu: np.ndarray = None,
    ome: np.random.Generator = np.random.default_rng()
) -> np.ndarray:
    """Sample from a multivariate Wiener process at given times.

    :param t:
    :param init_x:
    :param mu:
    :param ome:
    :return:

    >>> # generate fixture
    >>> n = int(1e5)
    >>> init_x = np.random.normal(n)
    >>> mu = np.random.normal(n)
    >>> t = np.sort(np.random.uniform(size=10))

    >>> # draw iid samples
    >>> xt = sample_wiener(t, np.repeat(init_x, n), np.repeat(mu, n))

    >>> # test mahalanobis distance sampling distribution
    >>> from scipy.stats import kstest, norm
    >>> alpha = 1e-2
    >>> true_mean, true_cov = moments.moments_wiener(t, init_x, mu)
    >>> sample = np.linalg.solve(np.linalg.cholesky(true_cov), xt - true_mean[:, np.newaxis]).flatten()
    >>> alpha < kstest(sample, norm().cdf)[1]
    True
    """

    assert 0 < np.min(t)

    dt = np.ediff1d(t, to_begin=(t[0], ))
    dxt = np.sqrt(dt)[:, np.newaxis] * ome.standard_normal(
        (len(t), len(init_x)))
    if mu is not None:
        dxt += np.outer(dt, mu)
    xt = np.cumsum(dxt, axis=0) + init_x

    return xt
Exemplo n.º 9
0
def euler_simulate(
    ndraws: int,
    fin_t: float = 1,
    init_x: float = 0,
    dfunc: Callable[[float], float] = (lambda x: 0),
    vfunc: Callable[[float], float] = (lambda x: 1),
    res: float = 1e-3,
    ome: np.random.Generator = np.random.default_rng()
) -> (np.ndarray, np.ndarray):
    """Simulate from a diffusion with given mu and volatility function by way of discrete approximation.

    :param ndraws:
    :param fin_t:
    :param init_x:
    :param dfunc:
    :param vfunc:
    :param res:
    :param ome:
    :return:

    >>> # simulate Bessel-3 on [0, 1] from x0=1
    >>> nsamples = int(1e4)
    >>> t, y = euler_simulate(nsamples, 1, 1, lambda x: 1 / x, lambda x: 1)

    >>> # test
    >>> from scipy.stats import kstest, ncx2
    >>> alpha = 1e-2
    >>> alpha < kstest(y[:, -1] ** 2 / t[-1], ncx2(3, 1 / t[-1]).cdf)[1]
    True
    """

    noise = np.sqrt(res) * ome.standard_normal((int(fin_t / res), ndraws))
    trajectory = [np.array(ndraws * [init_x])]

    for e in noise:
        trajectory.append(trajectory[-1] + dfunc(trajectory[-1]) * res +
                          vfunc(trajectory[-1]) * e)

    return np.arange(0, fin_t, res), np.array(trajectory).T