コード例 #1
0
    def test_single_price(self):
        df1 = pd.DataFrame(
            {
                'Open': [10.2, 12, 32.1, 9.32],
                'Close': [2.3, 3.6, 4.5, 11.11]
            },
            index=pd.to_datetime(
                ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01']))
        expected1 = pd.DataFrame({'Fake_1': [2.3, 3.6, 4.5, 11.11]},
                                 index=pd.to_datetime([
                                     '2020-01-01', '2020-02-01', '2020-03-01',
                                     '2020-04-01'
                                 ]))
        df2 = pd.DataFrame({'FakeSPY': [10.2, 12, 32.1, 9.32]},
                           index=pd.to_datetime([
                               '2020-01-01', '2020-02-01', '2020-03-01',
                               '2020-04-01'
                           ]))
        expected2 = pd.DataFrame({'FakeSPY': [10.2, 12, 32.1, 9.32]},
                                 index=pd.to_datetime([
                                     '2020-01-01', '2020-02-01', '2020-03-01',
                                     '2020-04-01'
                                 ]))
        new_df1 = DataPreprocessing.single_price(df1, 'Fake_1')
        new_df2 = DataPreprocessing.single_price(df2, 'FakeSPY')

        assert_frame_equal(new_df1, expected1)
        assert_frame_equal(new_df2, expected2)
コード例 #2
0
def run_ESRNN():
        
        import torch
        device = 'cuda' if torch.cuda.is_available() else 'cpu'
        path_daily = r'C:\Users\xxxli\Desktop\Daily'
        dic_daily = preprocess.read_file(path_daily)
        series_list = []
        for k, v in dic_daily.items():
            ticker_name = k
            df, cat = v
            df = preprocess.single_price(df, ticker_name) # column = [ticker]
            series_list.append(DataSeries(cat, 'daily', df))
        collect = DataCollection('universe daily', series_list)
        train_dc, test_dc = collect.split(numTest = 24)

        m = ModelESRNN( max_epochs = 15, 
                    batch_size = 32, dilations=[[1,3], [7, 14]],
                    input_size = 12, output_size = 24, 
                    device = device)

        m.train(train_dc)
        
        y_test = m.predict(test_dc)
        
        y_test_df = y_test.to_df()
        y_test_df.to_csv('hyper_ESRNN_1.csv')
コード例 #3
0
ファイル: Main.py プロジェクト: zliu2019/ML-APRP-Forecasting
def dc_generator(path: str, frequency: str):
    dic, recover_list, ticker_list = DataPreprocessing.read_file(path)
    series_list = []
    for k, v in dic.items():
        df, cat = v
        df = DataPreprocessing.single_price(df, k)
        series_list.append(DataSeries(cat, frequency, df))
    collect = DataCollection(frequency + ' Collection', series_list)
    return collect, recover_list, ticker_list
コード例 #4
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     # An example of how to use Telescope model
     path_monthly = os.path.join('test', 'Data', 'Monthly')
     dic_monthly = preprocess.read_file(path_monthly)
     series_list = []
     for k, v in dic_monthly.items():
         df, cat = v
         df = preprocess.single_price(df, k)
         series_list.append(DataSeries(cat, 'monthly', df))
     self.collect = DataCollection('test1', series_list)
コード例 #5
0
    def test_Naive2(self):
        path_monthly = os.path.join('test', 'Data', 'Monthly')
        dic_monthly = preprocess.read_file(path_monthly)
        series_list = []
        for k, v in dic_monthly.items():
            df, cat = v
            df = preprocess.single_price(df, k)
            series_list.append(DataSeries(cat, 'monthly', df))
        collect = DataCollection('test1', series_list)
        train_dc, test_dc = collect.split(numTest=12)
        m = ModelNaive2(12, train_dc, test_dc)
        y_hat_Naive2_dc = m.fit_and_generate_prediction(12, freq='MS')

        y_hat_Naive2_dc.to_df().to_csv('test_Naive2_result.csv')
コード例 #6
0
    def test_ESRNN(self):
        # An example of how to use ESRNN
        import torch
        device = 'cuda' if torch.cuda.is_available() else 'cpu'
        path_daily = os.path.join('test','Data','daily')
        dic_daily = preprocess.read_file(path_daily)
        series_list = []
        for k, v in dic_daily.items():
            df, cat = v
            df = preprocess.single_price(df, k)
            series_list.append(DataSeries(cat, 'daily', df))
        collect = DataCollection('test1', series_list)
        m = ModelESRNN(max_epochs = 5, seasonality = [], batch_size = 64, input_size = 12, output_size = 12, device = device)
        train_dc, test_dc = collect.split(numTest = 12)

        m.train(train_dc)
        y_test = m.predict(test_dc)
        assert(isinstance(y_test, DataCollection))
        y_test_df = y_test.to_df()
        y_test_df.to_csv('predict_result.csv')     
コード例 #7
0
learning_rate = 1e-2
lr_scheduler_step_size = 9
lr_decay = 0.9
noise_std = 0.001
level_variability_penalty = 80
state_hsize = 40
dilation = [[1]]
add_nl_layer = False
seasonality = [5]
# action
path = os.path.join('test', 'Data', 'Daily')
dic = preprocess.read_file(path)
series_list = []
for k, v in dic.items():
    df, cat = v
    df = preprocess.single_price(df, k)
    series_list.append(DataSeries(cat, 'daily', df))
collect = DataCollection('RollingValidation', series_list)
input_dc, _ = collect.split(numTest=2 * numTest)

score, _ = validation_simple(
    input_dc,
    numTest=numTest,
    max_epochs=max_epochs,
    batch_size=batch_size,
    learning_rate=learning_rate,
    lr_scheduler_step_size=lr_scheduler_step_size,
    lr_decay=lr_decay,
    noise_std=noise_std,
    level_variability_penalty=level_variability_penalty,
    state_hsize=state_hsize,