Exemplo n.º 1
0
    def test_generate_sample(self):
        process = ArmaProcess.from_coeffs([0.9])
        np.random.seed(12345)
        sample = process.generate_sample()
        np.random.seed(12345)
        expected = np.random.randn(100)
        for i in range(1, 100):
            expected[i] = 0.9 * expected[i - 1] + expected[i]
        assert_almost_equal(sample, expected)

        process = ArmaProcess.from_coeffs([1.6, -0.9])
        np.random.seed(12345)
        sample = process.generate_sample()
        np.random.seed(12345)
        expected = np.random.randn(100)
        expected[1] = 1.6 * expected[0] + expected[1]
        for i in range(2, 100):
            expected[i] = 1.6 * expected[i - 1] - 0.9 * expected[i - 2] + expected[i]
        assert_almost_equal(sample, expected)

        process = ArmaProcess.from_coeffs([1.6, -0.9])
        np.random.seed(12345)
        sample = process.generate_sample(burnin=100)
        np.random.seed(12345)
        expected = np.random.randn(200)
        expected[1] = 1.6 * expected[0] + expected[1]
        for i in range(2, 200):
            expected[i] = 1.6 * expected[i - 1] - 0.9 * expected[i - 2] + expected[i]
        assert_almost_equal(sample, expected[100:])


        np.random.seed(12345)
        sample = process.generate_sample(nsample=(100,5))
        assert_equal(sample.shape, (100,5))
Exemplo n.º 2
0
    def test_isstationary(self):
        process1 = ArmaProcess.from_coeffs([1.1])
        assert_equal(process1.isstationary, False)

        process1 = ArmaProcess.from_coeffs([1.8, -0.9])
        assert_equal(process1.isstationary, True)

        process1 = ArmaProcess.from_coeffs([1.5, -0.5])
        print(np.abs(process1.arroots))
        assert_equal(process1.isstationary, False)
Exemplo n.º 3
0
    def test_invertroots(self):
        process1 = ArmaProcess.from_coeffs([], [2.5])
        process2 = process1.invertroots(True)
        assert_almost_equal(process2.ma, np.array([1.0, 0.4]))

        process1 = ArmaProcess.from_coeffs([], [0.4])
        process2 = process1.invertroots(True)
        assert_almost_equal(process2.ma, np.array([1.0, 0.4]))

        process1 = ArmaProcess.from_coeffs([], [2.5])
        roots, invertable = process1.invertroots(False)
        assert_equal(invertable, False)
        assert_almost_equal(roots, np.array([1, 0.4]))
Exemplo n.º 4
0
    def test_from_model(self):
        process = ArmaProcess([1, -.8], [1, .3], 1000)
        t = 1000
        rs = np.random.RandomState(12345)
        y = process.generate_sample(t, burnin=100, distrvs=rs.standard_normal)
        res = ARMA(y, (1, 1)).fit(disp=False)
        process_model = ArmaProcess.from_estimation(res)
        process_coef = ArmaProcess.from_coeffs(res.arparams, res.maparams, t)

        assert_equal(process_model.arcoefs, process_coef.arcoefs)
        assert_equal(process_model.macoefs, process_coef.macoefs)
        assert_equal(process_model.nobs, process_coef.nobs)
        assert_equal(process_model.isinvertible, process_coef.isinvertible)
        assert_equal(process_model.isstationary, process_coef.isstationary)
Exemplo n.º 5
0
    def mc_ar1_ARMA(self, phi, std, n, N=1000):
        """ Monte-Carlo AR(1) processes

        input:
        phi .. (estimated) lag-1 autocorrelation
        std .. (estimated) standard deviation of noise
        n   .. length of original time series
        N   .. number of MC simulations 
        """
        AR_object = ArmaProcess(np.array([1, -phi]), np.array([1]), nobs=n)
        mc = AR_object.generate_sample(nsample=(N, n),
                                       scale=std,
                                       axis=1,
                                       burnin=1000)
        return mc
Exemplo n.º 6
0
def do_one_res_bootstrap(n, slope_hat, residuals):
    rand_res = lambda size: np.random.choice(residuals, size)
    ar = np.array([1, -slope_hat])
    ma = np.array([1])
    AR_res = ArmaProcess(ar,ma)
    data = AR_res.generate_sample(nsample = n + 1, scale = 1, distrvs = rand_res)

    x = data[0:(n-1)]
    y = data[1:n]

    slope = solve(x, y)
    stderr = calc_StdErr(slope, x, y)
    T = (slope - slope_hat) / stderr

    return T
Exemplo n.º 7
0
def make_trend(series_len,
               method='rw',
               arma=[.25, .6],
               rw_loc=0.0,
               rw_scale=0.1,
               seed=1):
    """ Module to generate time-series trend with different methods
    Parameters
    ----------
    series_len: int
        Total length of series
    method: str ['arma', 'rw']
        In case of `'rw'`, a simple random walk process will be used. For `'arma'`, we will use `statsmodels.api` to
        simulate a simple ARMA(1, 1) process
    arma: list
        List [arparams, maparams] of size 2 where used for arma(1) generating process
    rw_loc: float
        Location parameter of random walk generated by `np.random.normal()`
    rw_scale: float
        Scale parameter of random walk generated by `np.random.normal()`
    seed: int
        Seed passed into `np.random.default_rng()`
    Returns
    -------
    np.array-llike
        Simulated trend with length equals `series_len`

    Notes
    -----
        1. ARMA process: https://www.statsmodels.org/stable/generated/statsmodels.tsa.arima_process.ArmaProcess.html
    """
    # make trend
    if method == "rw":
        rw = np.random.default_rng(seed).normal(rw_loc, rw_scale, series_len)
        trend = np.cumsum(rw)
    elif method == "arma":
        arparams = np.array([arma[0]])
        maparams = np.array([arma[1]])
        # add zero-lag and negate
        ar = np.r_[1, -arparams]
        # add zero-lag
        ma = np.r_[1, maparams]
        arma_process = ArmaProcess(ar, ma)
        trend = arma_process.generate_sample(series_len)
    else:
        raise IllegalArgument("Invalid trend method.")

    return trend
Exemplo n.º 8
0
def _generate_random_arparams(ar_order, ma_order, limit_abs_sum=True, maxiter=100):
    is_stationary = False
    iteration = 0
    while not is_stationary:
        iteration += 1
        # print("Iteration", iteration)
        if iteration > maxiter:
            raise RuntimeError("failed to find stationary coefficients")
        # Generate random parameters
        arparams = []
        maparams = []
        for i in range(ar_order):
            arparams.append(2 * np.random.random() - 1)
        for i in range(ma_order):
            maparams.append(2 * np.random.random() - 1)
        # print(arparams)
        arparams = np.array(arparams)
        maparams = np.array(maparams)
        if limit_abs_sum:
            ar_abssum = sum(np.abs(arparams))
            ma_abssum = sum(np.abs(maparams))
            if ar_abssum > 1:
                arparams = arparams / (ar_abssum + 10e-6)
                arparams = arparams * (0.5 + 0.5 * np.random.random())
            if ma_abssum > 1:
                maparams = maparams / (ma_abssum + 10e-6)
                maparams = maparams * (0.5 + 0.5 * np.random.random())

        arparams = arparams - np.mean(arparams)
        maparams = maparams - np.mean(maparams)
        arma_process = ArmaProcess.from_coeffs(arparams, maparams, nobs=100)
        is_stationary = arma_process.isstationary
    return arparams, maparams
Exemplo n.º 9
0
def get_synthetic_ts_data_period(n_steps=1000,
                                 forecast_length=1,
                                 max_window_size=50):
    simulated_data = ArmaProcess().generate_sample(nsample=n_steps)
    x1 = np.arange(0, n_steps)
    x2 = np.arange(0, n_steps) + 1

    simulated_data = simulated_data + x1 * 0.0005 - x2 * 0.0001

    periodicity = np.sin(x1 / 50)

    simulated_data = simulated_data + periodicity

    task = Task(
        TaskTypesEnum.ts_forecasting,
        TsForecastingParams(forecast_length=forecast_length,
                            max_window_size=max_window_size,
                            return_all_steps=False))

    data = InputData(idx=np.arange(0, n_steps),
                     features=np.asarray([x1, x2]).T,
                     target=simulated_data,
                     task=task,
                     data_type=DataTypesEnum.ts)

    return train_test_data_setup(data)
Exemplo n.º 10
0
def test_from_estimation(d, seasonal):
    ar = [0.8] if not seasonal else [0.8, 0, 0, 0.2, -0.16]
    ma = [0.4] if not seasonal else [0.4, 0, 0, 0.2, -0.08]
    ap = ArmaProcess.from_coeffs(ar, ma, 500)
    idx = pd.date_range(dt.datetime(1900, 1, 1), periods=500, freq="Q")
    data = ap.generate_sample(500)
    if d == 1:
        data = np.cumsum(data)
    data = pd.Series(data, index=idx)
    seasonal_order = (1, 0, 1, 4) if seasonal else None
    mod = ARIMA(data, order=(1, d, 1), seasonal_order=seasonal_order)
    res = mod.fit()
    ap_from = ArmaProcess.from_estimation(res)
    shape = (5,) if seasonal else (1,)
    assert ap_from.arcoefs.shape == shape
    assert ap_from.macoefs.shape == shape
Exemplo n.º 11
0
    def test_acf(self):
        process1 = ArmaProcess.from_coeffs([.9])
        acf = process1.acf(10)
        expected = np.array(0.9) ** np.arange(10.0)
        assert_array_almost_equal(acf, expected)

        acf = process1.acf()
        assert_(acf.shape[0] == process1.nobs)
Exemplo n.º 12
0
    def test_acf(self):
        process1 = ArmaProcess.from_coeffs([0.9])
        acf = process1.acf(10)
        expected = np.array(0.9) ** np.arange(10.0)
        assert_array_almost_equal(acf, expected)

        acf = process1.acf()
        assert_(acf.shape[0] == process1.nobs)
Exemplo n.º 13
0
def test_arma_acovf_persistent():
    # Test arma_acovf in case where there is a near-unit root.
    # .999 is high enough to trigger the "while ir[-1] > 5*1e-5:" clause,
    # but not high enough to trigger the "nobs_ir > 50000" clause.
    ar = np.array([1, -0.9995])
    ma = np.array([1])
    process = ArmaProcess(ar, ma)
    res = process.acovf(10)

    # Theoretical variance sig2 given by:
    # sig2 = .9995**2 * sig2 + 1
    sig2 = 1 / (1 - 0.9995 ** 2)

    corrs = 0.9995 ** np.arange(10)
    expected = sig2 * corrs
    assert_equal(res.ndim, 1)
    assert_allclose(res, expected)
Exemplo n.º 14
0
def test_arma_acovf_persistent():
    # Test arma_acovf in case where there is a near-unit root.
    # .999 is high enough to trigger the "while ir[-1] > 5*1e-5:" clause,
    # but not high enough to trigger the "nobs_ir > 50000" clause.
    ar = np.array([1, -.9995])
    ma = np.array([1])
    process = ArmaProcess(ar, ma)
    res = process.acovf(10)

    # Theoretical variance sig2 given by:
    # sig2 = .9995**2 * sig2 + 1
    sig2 = 1/(1-.9995**2)

    corrs = .9995**np.arange(10)
    expected = sig2*corrs
    assert_equal(res.ndim, 1)
    assert_allclose(res, expected, atol=1e-6)
Exemplo n.º 15
0
    def test_pacf(self):
        process1 = ArmaProcess.from_coeffs([0.9])
        pacf = process1.pacf(10)
        expected = np.array([1, 0.9] + [0] * 8)
        assert_array_almost_equal(pacf, expected)

        pacf = process1.pacf()
        assert_(pacf.shape[0] == process1.nobs)
Exemplo n.º 16
0
    def test_pacf(self):
        process1 = ArmaProcess.from_coeffs([.9])
        pacf = process1.pacf(10)
        expected = np.array([1, 0.9] + [0] * 8)
        assert_array_almost_equal(pacf, expected)

        pacf = process1.pacf()
        assert_(pacf.shape[0] == process1.nobs)
Exemplo n.º 17
0
    def test_from_coeff(self):
        ar = [1.8, -0.9]
        ma = [0.3]
        process = ArmaProcess.from_coeffs(np.array(ar), np.array(ma))

        ar.insert(0, -1)
        ma.insert(0, 1)
        ar_p = -1 * np.array(ar)
        ma_p = ma
        process_direct = ArmaProcess(ar_p, ma_p)

        assert_equal(process.arcoefs, process_direct.arcoefs)
        assert_equal(process.macoefs, process_direct.macoefs)
        assert_equal(process.nobs, process_direct.nobs)
        assert_equal(process.maroots, process_direct.maroots)
        assert_equal(process.arroots, process_direct.arroots)
        assert_equal(process.isinvertible, process_direct.isinvertible)
        assert_equal(process.isstationary, process_direct.isstationary)
def estimating_an_ar_model():
    ar = np.array([1, -0.9])
    ma = np.array([1])
    ar_process = ArmaProcess(ar, ma)
    simulated_data = ar_process.generate_sample(nsample=1000)

    # Fit an AR(1) model to the first simulated data
    mod = ARMA(simulated_data, order=(1, 0))
    res = mod.fit()

    # Print out summary information on the fit
    print(res.summary())

    # Print out the estimate for the constant and for phi
    print("When the true phi=0.9, the estimate of phi (and the constant) are:")
    print(res.params)

    return simulated_data, res
Exemplo n.º 19
0
def generate_signal(go_onset=2, ss_onset=12, fs_onset=22,
                    go_pwr=1, ss_pwr=2, fs_pwr=3, noise=0,
                    design_resolution=0.1, duration=40,
                    stim_duration=1, tr=1):
    rho = 0.12
    cond_order = [0, 1, 2]
    betas = np.array([go_pwr, ss_pwr, fs_pwr])
    onsets = np.array([go_onset, ss_onset, fs_onset])
    onsets_res = onsets / design_resolution
    onsets_res = onsets_res.astype(int)
    duration_res = int(duration / design_resolution)
    stim_duration_res = int(stim_duration / design_resolution)
    sampling_rate = int(tr / design_resolution)

    X = np.zeros((duration_res, onsets.shape[0]))
    B = np.zeros((onsets.shape[0], 1))

    for idx, (cond, onset) in enumerate(zip(cond_order, onsets_res)):
        # set the design matrix
        X[onset:onset+stim_duration_res, idx] = 1
        X[:, idx] = np.convolve(
            X[:, idx], hemodynamic_models._gamma_difference_hrf(
                tr, oversampling=sampling_rate))[0:X.shape[0]]
        # set the beta for the trial depending on condition
        B[idx, :] = betas[cond]

    # downsample X so it's back to TR resolution
    X = X[::sampling_rate, :]

    signal = X @ B
    signal = np.squeeze(signal)
    if noise > 0.0:
        np.random.seed(12345)
        # make the noise component
        n_trs = int(duration / tr)
        ar = np.array([1, -rho])  # statmodels says to invert rho
        ap = ArmaProcess(ar)
        err = ap.generate_sample(n_trs, scale=noise, axis=0)

        Y = signal + err
    else:
        Y = signal

    return Y
Exemplo n.º 20
0
    def test_process_multiplication(self):
        process1 = ArmaProcess.from_coeffs([0.9])
        process2 = ArmaProcess.from_coeffs([0.7])
        process3 = process1 * process2
        assert_equal(process3.arcoefs, np.array([1.6, -0.7 * 0.9]))
        assert_equal(process3.macoefs, np.array([]))

        process1 = ArmaProcess.from_coeffs([0.9], [0.2])
        process2 = ArmaProcess.from_coeffs([0.7])
        process3 = process1 * process2

        assert_equal(process3.arcoefs, np.array([1.6, -0.7 * 0.9]))
        assert_equal(process3.macoefs, np.array([0.2]))

        process1 = ArmaProcess.from_coeffs([0.9], [0.2])
        process2 = process1 * (np.array([1.0, -0.7]), np.array([1.0]))
        assert_equal(process2.arcoefs, np.array([1.6, -0.7 * 0.9]))

        assert_raises(TypeError, process1.__mul__, [3])
Exemplo n.º 21
0
def get_AR(process, iters, params):
    # AR
    if params == None:
        params = {'AR': [-0.999]}

    metadata = {"NAME": process, "number_of_iterations": iters}
    metadata.update(params)

    ar = np.array([1] + params['AR'])
    ar_object = ArmaProcess(ar, [1])
    y = ar_object.generate_sample(nsample=iters)
    true_var = sum([
        y[i:-(len(ar) - i)] * (-1 * ar[len(ar) - i])
        for i in range(1, len(ar))
    ]) + sps.norm.ppf(0.01)
    y = y[len(ar):]

    data = pd.DataFrame(data={'Returns': y, 'True_VAR_0.01': true_var})
    return metadata, data
Exemplo n.º 22
0
def estimate_order_of_model_information_criteria():
    ma = np.array([1])
    ar = np.array([1, -0.6, -0.3])
    AR_object = ArmaProcess(ar, ma)
    simulated_data_2 = AR_object.generate_sample(nsample=5000)

    # Fit the data to an AR(p) for p = 0,...,6 , and save the BIC
    BIC = np.zeros(7)
    for p in range(7):
        mod = ARMA(simulated_data_2, order=(p, 0))
        res = mod.fit()
    # Save BIC for AR(p)
        BIC[p] = res.bic

    # Plot the BIC as a function of p
    plt.plot(range(1, 7), BIC[1:7], marker='o')
    plt.xlabel('Order of AR Model')
    plt.ylabel('Bayesian Information Criterion')
    plt.show()
Exemplo n.º 23
0
    def test_str_repr(self):
        process1 = ArmaProcess.from_coeffs([.9], [.2])
        out = process1.__str__()
        print(out)
        assert_(out.find('AR: [1.0, -0.9]') != -1)
        assert_(out.find('MA: [1.0, 0.2]') != -1)

        out = process1.__repr__()
        assert_(out.find('nobs=100') != -1)
        assert_(out.find('at ' + str(hex(id(process1)))) != -1)
Exemplo n.º 24
0
    def test_str_repr(self):
        process1 = ArmaProcess.from_coeffs([0.9], [0.2])
        out = process1.__str__()
        print(out)
        assert_(out.find("AR: [1.0, -0.9]") != -1)
        assert_(out.find("MA: [1.0, 0.2]") != -1)

        out = process1.__repr__()
        assert_(out.find("nobs=100") != -1)
        assert_(out.find("at " + str(hex(id(process1)))) != -1)
Exemplo n.º 25
0
    def test_process_multiplication(self):
        process1 = ArmaProcess.from_coeffs([.9])
        process2 = ArmaProcess.from_coeffs([.7])
        process3 = process1 * process2
        assert_equal(process3.arcoefs, np.array([1.6, -0.7 * 0.9]))
        assert_equal(process3.macoefs, np.array([]))

        process1 = ArmaProcess.from_coeffs([.9], [.2])
        process2 = ArmaProcess.from_coeffs([.7])
        process3 = process1 * process2

        assert_equal(process3.arcoefs, np.array([1.6, -0.7 * 0.9]))
        assert_equal(process3.macoefs, np.array([0.2]))

        process1 = ArmaProcess.from_coeffs([.9], [.2])
        process2 = process1 * (np.array([1.0, -0.7]), np.array([1.0]))
        assert_equal(process2.arcoefs, np.array([1.6, -0.7 * 0.9]))

        assert_raises(TypeError, process1.__mul__, [3])
Exemplo n.º 26
0
    def test_str_repr(self):
        process1 = ArmaProcess.from_coeffs([.9], [.2])
        out = process1.__str__()
        print(out)
        assert_(out.find('AR: [1.0, -0.9]') != -1)
        assert_(out.find('MA: [1.0, 0.2]') != -1)

        out = process1.__repr__()
        assert_(out.find('nobs=100') != -1)
        assert_(out.find('at ' + str(hex(id(process1)))) != -1)
Exemplo n.º 27
0
def simulate_ar_process(phi=0.9, plot=False):
    """
    # 0 lag coefficient of 1
    # sign of other coefficient is opposite from what we are using

    # Example, AR(1) process with phi = 0.9
    # the second element of AR array should be the opposite sign, - 0.9
    # Since ignoring MA at the moment, we just use 1

    :param phi:
    :return:
    """
    ar = np.array([1, -phi])
    ma = np.array([1])
    AR_object = ArmaProcess(ar, ma)
    simulated_data = AR_object.generate_sample(nsample=1000)
    if plot:
        plt.plot(simulated_data)
        plt.show()
    return simulated_data
Exemplo n.º 28
0
def gen_arma(n_probes=20000, n_patients=80, corr=0.2, df=2, scale=0.2):
    # these parameters are taken from the Bump Hunting Paper
    from statsmodels.tsa.arima_process import ArmaProcess
    sigma = 0.5
    rvs = ss.norm(df, loc=0.05 / sigma, scale=scale).rvs
    corr = -abs(corr)
    return np.column_stack([
        sigma * ArmaProcess([1, corr], [1]).generate_sample(n_probes=n_probes,
                                                            distrvs=rvs)
        for i in range(n_patients)
    ])
Exemplo n.º 29
0
def estimating_an_ma_model():
    ar1 = np.array([1])
    ma1 = np.array([1, -0.9])
    MA_object1 = ArmaProcess(ar1, ma1)
    simulated_data_1 = MA_object1.generate_sample(nsample=1000)

    # Fit an MA(1) model to the first simulated data
    mod = ARMA(simulated_data_1, order=(0, 1))
    res = mod.fit()

    # Print out summary information on the fit
    print(res.summary())

    # Print out the estimate for the constant and for theta
    print(
        "When the true theta=-0.9, the estimate of theta (and the constant) are:"
    )
    print(res.params)

    return res
Exemplo n.º 30
0
def plot_MA(maxit = 100, N = 365, T = 1000, save=False, name='img/results/emission.png'):
    """Generate emission model output with transition replaced with MA.
    
    Transition model is defined as
    
        z_t = z_{t-1} + 3*sin(2*pi*t/T)
    
    Args:
        maxit (int): Number of samples
        N (int): Size of time range.
        T (int): Constant test size.
        save (bool, optional): Whether to save the figure, defaultly not.
        name (str, optional): Path to save the plot to.
    """
    # create model    
    MA = ArmaProcess(ma = [.2,-.4,.2,-.7])
    # iterate
    z = np.zeros((maxit, N))
    x = np.zeros((maxit, N))
    for i in range(maxit):
        # create z[t] sample
        z[i,:] = MA.generate_sample(nsample=N) + 3*np.sin(np.array(range(N))/N*2*np.pi)
        z[i,:] = (z[i,:] - z[i,:].min()) / (z[i,:].max() - z[i,:].min())
        # create x[t] sample
        x[i,:] = emission(z[i,:], np.array([T for i in range(N)]), 1, 50)
    def get_mu_ci(ts):
        mu = ts.mean(axis = 0)
        ci = np.quantile(ts, [.025,.975],axis=0)
        return mu,ci
    z_mu,z_ci = get_mu_ci(z)
    x_mu,x_ci = get_mu_ci(x)
    # plot
    fig1, ax1 = plt.subplots()
    ax1.plot(range(N), z_mu, color='red', label='z[t]')
    ax1.fill_between(range(N), z_ci[0,:], z_ci[1,:], color = 'red', alpha = .1)
    ax1.plot(range(N), x_mu, color='blue', label='x[t]')
    ax1.fill_between(range(N), x_ci[0,:], x_ci[1,:], color = 'blue', alpha = .1)
    ax1.set_xlabel('Time')
    ax1.set_ylabel('Value')
    ax1.legend()
    if save: fig1.savefig(name)
Exemplo n.º 31
0
def get_synthetic_ts_data(n_steps=10000) -> InputData:
    simulated_data = ArmaProcess().generate_sample(nsample=n_steps)
    x1 = np.arange(0, n_steps)
    x2 = np.arange(0, n_steps) + 1

    simulated_data = simulated_data + x1 * 0.0005 - x2 * 0.0001

    input_data = InputData(idx=np.arange(0, n_steps),
                           features=np.asarray([x1, x2]).T,
                           target=simulated_data,
                           task_type=MachineLearningTasksEnum.auto_regression)
    return input_data
Exemplo n.º 32
0
def generate_armaprocess_data(samples, ar_order, ma_order, noise_std, params=None):
    if params is not None:
        # use specified params (make sure to sum up to 1 or less)
        arparams, maparams = params
    else:
        # iterate to find random arparams that are stationary
        arparams, maparams = _generate_random_arparams(ar_order, ma_order)
    arma_process = ArmaProcess.from_coeffs(arparams, maparams, nobs=samples)
    # sample output from ARMA Process
    series = arma_process.generate_sample(samples, scale=noise_std)
    # make zero-mean:
    series = series - np.mean(series)
    return series, list(arparams), list(maparams)
Exemplo n.º 33
0
def test_simulated_y_custom_model():
    np.random.seed(1)
    ar = np.r_[1, 0.9]
    ma = np.array([1])
    arma_process = ArmaProcess(ar, ma)
    X = 100 + arma_process.generate_sample(nsample=100)
    y = 1.2 * X + np.random.normal(size=(100))
    data = pd.DataFrame({'y': y, 'X': X}, columns=['y', 'X'])
    intervention_idx = 70
    normed_pre_data, _ = standardize(data.iloc[:intervention_idx])

    model = UnobservedComponents(
        endog=normed_pre_data['y'].iloc[0:intervention_idx],
        level='llevel',
        exog=normed_pre_data['X'].iloc[0:intervention_idx])

    ci = CausalImpact(data, [0, 69], [70, 99], model=model)

    assert ci.simulated_y.shape == (1000, 30)

    lower, upper = np.percentile(ci.simulated_y.mean(axis=1), [5, 95])
    assert lower > 119
    assert upper < 121
Exemplo n.º 34
0
def generate_stoch_ar1(nt, a1):
    """
    Generate AR1 process for strong noisy source
    Input:
    nt - number of timesteps.
    a1 - interior parameter.
    Output:
    np.array with values of size (nt,).
    """
    ar_par = np.array([1.0, a1])
    ma = np.array([1.0])
    simulated_ar1 = ArmaProcess(ar_par, ma).generate_sample(nsample=nt)

    return simulated_ar1 / np.std(simulated_ar1)
Exemplo n.º 35
0
    def test_generate_sample(self):
        process = ArmaProcess.from_coeffs([0.9])
        np.random.seed(12345)
        sample = process.generate_sample()
        np.random.seed(12345)
        expected = np.random.randn(100)
        for i in range(1, 100):
            expected[i] = 0.9 * expected[i - 1] + expected[i]
        assert_almost_equal(sample, expected)

        process = ArmaProcess.from_coeffs([1.6, -0.9])
        np.random.seed(12345)
        sample = process.generate_sample()
        np.random.seed(12345)
        expected = np.random.randn(100)
        expected[1] = 1.6 * expected[0] + expected[1]
        for i in range(2, 100):
            expected[i] = (
                1.6 * expected[i - 1] - 0.9 * expected[i - 2] + expected[i]
            )
        assert_almost_equal(sample, expected)

        process = ArmaProcess.from_coeffs([1.6, -0.9])
        np.random.seed(12345)
        sample = process.generate_sample(burnin=100)
        np.random.seed(12345)
        expected = np.random.randn(200)
        expected[1] = 1.6 * expected[0] + expected[1]
        for i in range(2, 200):
            expected[i] = (
                1.6 * expected[i - 1] - 0.9 * expected[i - 2] + expected[i]
            )
        assert_almost_equal(sample, expected[100:])

        np.random.seed(12345)
        sample = process.generate_sample(nsample=(100, 5))
        assert_equal(sample.shape, (100, 5))
Exemplo n.º 36
0
def get_synthetic_ts_data(n_steps=10000) -> InputData:
    simulated_data = ArmaProcess().generate_sample(nsample=n_steps)
    x1 = np.arange(0, n_steps)
    x2 = np.arange(0, n_steps) + 1

    simulated_data = simulated_data + x1 * 0.0005 - x2 * 0.0001

    task = Task(TaskTypesEnum.ts_forecasting,
                TsForecastingParams(forecast_length=1, max_window_size=2))

    input_data = InputData(idx=np.arange(0, n_steps),
                           features=np.asarray([x1, x2]).T,
                           target=simulated_data,
                           task=task,
                           data_type=DataTypesEnum.ts)
    return input_data
Exemplo n.º 37
0
    def test_from_coeff(self):
        ar = [1.8, -0.9]
        ma = [0.3]
        process = ArmaProcess.from_coeffs(np.array(ar), np.array(ma))

        ar.insert(0, -1)
        ma.insert(0, 1)
        ar_p = -1 * np.array(ar)
        ma_p = ma
        process_direct = ArmaProcess(ar_p, ma_p)

        assert_equal(process.arcoefs, process_direct.arcoefs)
        assert_equal(process.macoefs, process_direct.macoefs)
        assert_equal(process.nobs, process_direct.nobs)
        assert_equal(process.maroots, process_direct.maroots)
        assert_equal(process.arroots, process_direct.arroots)
        assert_equal(process.isinvertible, process_direct.isinvertible)
        assert_equal(process.isstationary, process_direct.isstationary)
Exemplo n.º 38
0
def test_fit_arima(fn):
    np.random.seed(200)

    ar_params = np.array([1, -0.5])
    ma_params = np.array([1, -0.3])
    ret = ArmaProcess(ar_params, ma_params).generate_sample(nsample=5*252)

    ret = pd.Series(ret)
    drift = 100
    price = pd.Series(np.cumsum(ret)) + drift    

    lret = np.log(price) - np.log(price.shift(1))
    lret = lret[1:]

    # choose autoregression lag of 1
    AR_lag_p = 1
    
    # choose moving average lag of 1
    MA_lag_q = 1
    
    # choose order of integration 1
    order_of_integration_d = 1
    
    # Create a tuple of p,d,q
    order = (AR_lag_p, order_of_integration_d, MA_lag_q)
    
    arima_model = ARIMA(lret.values, order=order)
    arima_result = arima_model.fit()

    fittedvalues = arima_result.fittedvalues
    arparams = arima_result.arparams
    maparams = arima_result.maparams

    fn_inputs = {
        'lret': lret
        }

    fn_correct_outputs = OrderedDict([
        ('fittedvalues',fittedvalues),
        ('arparams',arparams),
        ('maparams',maparams),
        ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Exemplo n.º 39
0
def get_synthetic_ts_data_period(n_steps=1000, forecast_length=5):
    simulated_data = ArmaProcess().generate_sample(nsample=n_steps)
    x1 = np.arange(0, n_steps)
    x2 = np.arange(0, n_steps) + 1

    simulated_data = simulated_data + x1 * 0.0005 - x2 * 0.0001

    periodicity = np.sin(x1 / 50)

    simulated_data = simulated_data + periodicity

    task = Task(TaskTypesEnum.ts_forecasting,
                TsForecastingParams(forecast_length=forecast_length))

    data = InputData(idx=np.arange(0, n_steps),
                     features=simulated_data,
                     target=simulated_data,
                     task=task,
                     data_type=DataTypesEnum.ts)
    a, b = train_test_data_setup(data)
    return train_test_data_setup(data)
Exemplo n.º 40
0
def simulate_ar1_time_series():
    # Plot 1:  AR parameter = +0.9
    plt.subplot(2, 1, 1)
    ar1 = np.array([1, -0.9])
    ma1 = np.array([1])
    AR_object1 = ArmaProcess(ar1, ma1)
    simulated_data_1 = AR_object1.generate_sample(nsample=1000)
    plt.plot(simulated_data_1)

    # Plot 1:  AR parameter = -0.9
    plt.subplot(2, 1, 2)
    ar2 = np.array([1, 0.9])
    ma2 = np.array([1])
    AR_object2 = ArmaProcess(ar2, ma2)
    simulated_data_2 = AR_object2.generate_sample(nsample=1000)
    plt.plot(simulated_data_2)

    plt.show()
Exemplo n.º 41
0
from statsmodels.tsa.arima_process import arma_generate_sample, ArmaProcess

# <codecell>

np.random.seed(1234)
# include zero-th lag
arparams = np.array([1, .75, -.65, -.55, .9])
maparams = np.array([1, .65])

# <markdowncell>

# * Let's make sure this models is estimable.

# <codecell>

arma_t = ArmaProcess(arparams, maparams)

# <codecell>

arma_t.isinvertible()

# <codecell>

arma_t.isstationary()

# <rawcell>

# * What does this mean?

# <codecell>
100XP
Import the class ArmaProcess in the arima_process module.
Plot the simulated AR procesees:
Let ar1 represent an array of the AR parameters [1, −ϕ
−
ϕ
] as explained above. For now, the MA parmater array, ma1, will contain just the lag-zero coefficient of one.
With parameters ar1 and ma1, create an instance of the class ArmaProcess(ar,ma) called AR_object1.
Simulate 1000 data points from the object you just created, AR_object1, using the method .generate_sample(). Plot the simulated data in a subplot.
Repeat for the other AR parameter.
'''
# import the module for simulating data
from statsmodels.tsa.arima_process import ArmaProcess

# Plot 1: AR parameter = +0.9
plt.subplot(2,1,1)
ar1 = np.array([1, -0.9])
ma1 = np.array([1])
AR_object1 = ArmaProcess(ar1, ma1)
simulated_data_1 = AR_object1.generate_sample(nsample=1000)
plt.plot(simulated_data_1)

# Plot 2: AR parameter = -0.9
plt.subplot(2,1,2)
ar2 = np.array([1, 0.9])
ma2 = np.array([1])
AR_object2 = ArmaProcess(ar2, ma2)
simulated_data_2 = AR_object2.generate_sample(nsample=1000)
plt.plot(simulated_data_2)
plt.show()
Exemplo n.º 43
0
def invertibleroots(ma):
    proc = ArmaProcess(ma=ma)
    return proc.invertroots(retnew=False)
Exemplo n.º 44
0
# ### Exercise: Can you obtain a better fit for the Sunspots model? (Hint:
# sm.tsa.AR has a method select_order)

# ### Simulated ARMA(4,1): Model Identification is Difficult

from statsmodels.tsa.arima_process import ArmaProcess

np.random.seed(1234)
# include zero-th lag
arparams = np.array([1, .75, -.65, -.55, .9])
maparams = np.array([1, .65])

# Let's make sure this model is estimable.

arma_t = ArmaProcess(arparams, maparams)

arma_t.isinvertible

arma_t.isstationary

# * What does this mean?

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111)
ax.plot(arma_t.generate_sample(nsample=50))

arparams = np.array([1, .35, -.15, .55, .1])
maparams = np.array([1, .65])
arma_t = ArmaProcess(arparams, maparams)
arma_t.isstationary
,
0.8
,
0.8
2
,
0.8
3
,
…
Simulate 5000 observations of the MA(30) model
Plot the ACF of the simulated series
'''



# import the modules for simulating data and plotting the ACF
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.graphics.tsaplots import plot_acf

# Build a list MA parameters
ma = [0.8**i for i in range(30)]

# Simulate the MA(30) model
ar = np.array([1])
AR_object = ArmaProcess(ar, ma)
simulated_data = AR_object.generate_sample(nsample=5000)

# Plot the ACF
plot_acf(simulated_data, lags=30)
plt.show()
Exemplo n.º 46
0
nobs = 500
ar = [1, -0.6, -0.1]
ma = [1, 0.7]
dist = lambda n: np.random.standard_t(3, size=n)
np.random.seed(8659567)
x = arma_generate_sample(ar, ma, nobs, sigma=1, distrvs=dist,
                         burnin=500)

mod = TArma(x)
order = (2, 1)
res = mod.fit(order=order)
res2 = mod.fit_mle(order=order, start_params=np.r_[res[0], 5, 1], method='nm')

print(res[0])
proc = ArmaProcess.from_coeffs(res[0][:order[0]], res[0][:order[1]])

print(ar, ma)
proc.nobs = nobs
# TODO: bug nobs is None, not needed ?, used in ArmaProcess.__repr__
print(proc.ar, proc.ma)

print(proc.ar_roots(), proc.ma_roots())

from statsmodels.tsa.arma_mle import Arma
modn = Arma(x)
resn = modn.fit_mle(order=order)

moda = ARMA(x, order=order)
resa = moda.fit( trend='nc')
Exemplo n.º 47
0
        self.trend.plot(ax=axes[2], legend=False)
        axes[2].set_ylabel('Trend')
        self.irregular.plot(ax=axes[3], legend=False)
        axes[3].set_ylabel('Irregular')

        fig.tight_layout()
        return fig


if __name__ == "__main__":
    import numpy as np
    from statsmodels.tsa.arima_process import ArmaProcess
    np.random.seed(123)
    ar = [1, .35, .8]
    ma = [1, .8]
    arma = ArmaProcess(ar, ma, nobs=100)
    assert arma.isstationary()
    assert arma.isinvertible()
    y = arma.generate_sample()
    dates = pd.date_range("1/1/1990", periods=len(y), freq='M')
    ts = pd.TimeSeries(y, index=dates)

    xpath = "/home/skipper/src/x12arima/x12a"

    try:
        results = x13_arima_analysis(xpath, ts)
    except:
        print("Caught exception")

    results = x13_arima_analysis(xpath, ts, log=False)
Exemplo n.º 48
0
 def test_arma2ar(self):
     process1 = ArmaProcess.from_coeffs([], [0.8])
     vals = process1.arma2ar(100)
     assert_almost_equal(vals, (-0.8) ** np.arange(100.0))
Exemplo n.º 49
0
 def test_periodogram(self):
     process = ArmaProcess()
     pg = process.periodogram()
     assert_almost_equal(pg[0], np.linspace(0,np.pi,100,False))
     assert_almost_equal(pg[1], np.sqrt(2 / np.pi) / 2 * np.ones(100))
Exemplo n.º 50
0
 def test_impulse_response(self):
     process = ArmaProcess.from_coeffs([0.9])
     ir = process.impulse_response(10)
     assert_almost_equal(ir, 0.9 ** np.arange(10))