class FunctionConfigs:
    class FuncType(Enum):
        DFT = 1
        DFT_REAL = 2,
        DFT_HERMITE = 3,
        POWER_SPECTRUM = 4,
        ACF = 5,
        PACF = 6

    lambdaContainer = {
        FuncType.DFT: lambda p: np.fft.fftn(**p),
        FuncType.DFT_REAL: lambda p: np.fft.rfftn(**p),
        FuncType.DFT_HERMITE: lambda p: np.fft.hfft(**p),
        FuncType.POWER_SPECTRUM: lambda p: periodogram(**p),
        FuncType.ACF: lambda p: acf(**p),
        FuncType.PACF: lambda p: pacf(**p)
    }

    constraintTo1D = {
        FuncType.POWER_SPECTRUM: True,
        FuncType.ACF: True,
        FuncType.PACF: True
    }

    indexReturn = {FuncType.POWER_SPECTRUM: 0}

    def __init__(self):
        pass
Beispiel #2
0
def data_periodogram(dados):
    freq = np.arange(0, 0.5, 1/len(dados))
    periodo = 1/freq

    pyplot.plot(dados)
    pyplot.show()

    pgram = periodogram(dados)
    if len(pgram) % 2 == 0:
        pgram = pgram[0:int(len(dados)/2)]
        indexes = sorted(range(len(pgram)), key=lambda i: pgram[i])[-3:]
    else:
        pgram = pgram[0:int((len(dados))/2)+1]
        indexes = sorted(range(len(pgram)), key=lambda i: pgram[i])[-3:]

    print('marilia', indexes)
    indexes = list(indexes)
    freq_max = [freq[index] for index in indexes]
    T = [len(dados)/index for index in indexes]

    df_output = pd.DataFrame({'Frequencia': freq,
                              'Periodograma': pgram})
    return pgram, freq, df_output, indexes, freq_max, T
def test_periodogram_future_warning(reset_randomstate):
    with pytest.warns(FutureWarning):
        periodogram(np.random.standard_normal(100))
Beispiel #4
0
def proxiesStats(proxy_ids):
    ### GET TIME SERIES
    logs_df_file = os.path.join(output_dir, 'logs_df')
    if os.path.isfile(logs_df_file):
        logs_df = pd.read_pickle(logs_df_file)
    else:
        pool = multiprocessing.Pool(
            processes=len(proxy_ids),
            initializer=start_process,
        )
        pool_outputs = pool.map(helper, proxy_ids)
        pool.close()
        pool.join()
        print 'Joined'
        logs_df = pd.concat(pool_outputs, ignore_index=True)
        logs_df.to_pickle(logs_df_file)

    bytes_ts = pd.Series(logs_df['bytes'].set_index('ts').bytes.resample(
        '60Min', how='sum'))
    requests_ts = pd.Series(logs_df['bytes'].set_index('ts').bytes.resample(
        '60Min', how='resampler_count'))
    bytes_df = pd.DataFrame(bytes_ts)
    requests_df = pd.DataFrame(requests_ts)
    pdb.set_trace()

    ### PLOT GENERAL TIME SERIES GRAPHS
    bytes_df.plot(logy=True)
    plt.show()
    raw_input('End')

    periodogram = stattools.periodogram(bytes_df.bytes)
    plotTwin(bytes_df, 'Bytes', periodogram, 'Frequency', 'Periodogram')

    acf = stattools.acf(bytes_df)
    plotTwin(bytes_df, 'Bytes', 'acf', 'Correlation Coefficient',
             'Autocorrelation', bytes_df)

    pacf = stattools.pacf(bytes_df)
    plotTwin(bytes_df, 'Bytes', 'pacf', 'Partial Correlation Coefficient',
             'Partial Autocorrelation', bytes_df)

    decomposition = seasonal_decompose(bytes_df.values, freq=24)
    decomposition.plot()
    plt.show()
    raw_input('End')

    du_wa = stat1.durbin_watson(np.nan_to_num(decomposition.resid))
    print 'Durbin-Watson test for residuals: %s' % du_wa

    ### EVEN/ODD ANALYSIS
    #odd_mask = bytes_df.index.map(lambda x: x.day%2 ) == 1
    odd_mask = bytes_df.index.map(lambda x: x.day in [15, 17, 19, 21]) == True
    bytes_df_odd = bytes_df[odd_mask].shift(-7, 'D')

    #even_mask = bytes_df.index.map(lambda x: x.day%2) == 0
    even_mask = bytes_df.index.map(lambda x: x.day in [8, 10, 12, 14]) == True
    bytes_df_even = bytes_df[even_mask]

    fig, ax1 = plt.subplots()
    lns1 = ax1.plot(bytes_df_odd, 'b--', label='Odd', visible=True)
    lns2 = ax1.plot(bytes_df_even, 'b:', label='Even', visible=True)
    ax1.set_ylabel('Bytes', color='b', fontsize=18)
    ax1.set_yscale('log')
    for tl in ax1.get_yticklabels():
        tl.set_color('b')

    #corr = pd.rolling_corr(arg1=bytes_df_odd['bytes'],arg2=bytes_df_even['bytes'].shift(-24,'H'),window=24)
    #corr = pd.rolling_corr(arg1=bytes_df_odd['bytes'],arg2=bytes_df_even['bytes'],window=24)
    corr = bytes_df_odd['bytes'].rolling(window=24).corr(
        other=bytes_df_even['bytes'])

    ax2 = ax1.twinx()
    lns3 = ax2.plot(corr, 'r-', label='Correlation', visible=True)
    lns = lns1 + lns2 + lns3
    labs = [l.get_label() for l in lns]
    ax1.legend(lns, labs, loc=0)
    ax2.set_ylabel('Correlation Coefficient', color='r', fontsize=18)
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    plt.show()
    raw_input('End')

    print 'Even/Odd correlation: %s' % bytes_df_odd.corrwith(
        bytes_df_even).bytes
Beispiel #5
0
bitcoin_arima = results.forecast(steps=31)[0][1:]
bitcoin_future = pd.read_csv('bitcoin_future.csv')
np.sqrt((bitcoin_future['Closing'] - bitcoin_arima)**2).sum()

# In[41]:

bitcoin_arima

# ### q12 - Periodogram

# In[26]:

# https://www.statsmodels.org/dev/generated/statsmodels.tsa.stattools.periodogram.html
from statsmodels.tsa.stattools import periodogram
plt.plot(periodogram(bprice))
plt.show()
plt.plot(periodogram(bprice_1d))

# ### There is no seasonality

# #### Q13

# In[27]:

final_1d = final_date_2017.diff().dropna()
final_1d
from statsmodels.tsa.vector_ar import var_model
var = var_model.VAR(final_1d)
np.argmin(var.select_order().ics['aic'])
var_results = var.fit(maxlags=1)
Beispiel #6
0
def periodogram(timeSeries):
    # Returns the periodogram for the natural frequency of X
    values_periodograk = stattools.periodogram(timeSeries)