AI4Finance-Foundation / FinRL

FinRL: Financial Reinforcement Learning. 🔥
https://ai4finance.org
MIT License
9.36k stars 2.27k forks source link

Add GroupBy Scaler #1212

Closed C4i0kun closed 2 months ago

C4i0kun commented 2 months ago

Hello,

This pull request adds a useful sklearn-like scaler that allows the user to apply a StandardScaler or MinMaxScaler to a dataframe with multiple groups. So consider the following dataframe:

            date        high            low             close           tic
        0   2020-12-23  0.157414        0.127420        0.136394        ADA-USD
        1   2020-12-23  34.381519       30.074295       31.097898       BNB-USD
        2   2020-12-23  24024.490234    22802.646484    23241.345703    BTC-USD
        3   2020-12-23  0.004735        0.003640        0.003768        DOGE-USD
        4   2020-12-23  637.122803      560.364258      583.714600      ETH-USD
        ... ...         ...             ...             ...             ...

By using the groupby scaler like below:

from finrl.meta.preprocessor.preprocessors import GroupByScaler

scaler = GroupByScaler(by="tic")
df = scaler.fit_transform(df)

You can easily normalize the time series of each ticker individually, as if different scalers were applied to each one of them. A few unit tests were added to confirm that the fit(), transform() and fit_transform() functions are working as intended.

This pull request also adds a new parameter to GPM architecture so that the user can choose the number of graph convolutional layers to implement.

zhumingpassional commented 2 months ago

have you tested it? does it work well now?

C4i0kun commented 2 months ago

Yes! With this current normalization method, the convolutional neural networks are not overffiting like before (as can be seen in the updated example). It looks like the old normalization method was pretty bad.

zhumingpassional commented 2 months ago

merged. thanks for your valuable codes.