Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH#220 - Softmax Utils Function #221

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions finrl/env/env_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import matplotlib.pyplot as plt
from stable_baselines3.common.vec_env import DummyVecEnv

from finrl.utils.maths import softmax

class StockPortfolioEnv(gym.Env):
"""A single stock trading environment for OpenAI gym
Expand Down Expand Up @@ -161,7 +162,7 @@ def step(self, actions):
# norm_actions = (np.array(actions) - np.array(actions).min()) / (np.array(actions) - np.array(actions).min()).sum()
# else:
# norm_actions = actions
weights = self.softmax_normalization(actions)
weights = softmax(actions)
# print("Normalized actions: ", weights)
self.actions_memory.append(weights)
last_day_memory = self.data
Expand Down Expand Up @@ -220,12 +221,6 @@ def reset(self):
def render(self, mode="human"):
return self.state

def softmax_normalization(self, actions):
numerator = np.exp(actions)
denominator = np.sum(np.exp(actions))
softmax_output = numerator / denominator
return softmax_output

def save_asset_memory(self):
date_list = self.date_memory
portfolio_return = self.portfolio_return_memory
Expand Down
Empty file added finrl/utils/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions finrl/utils/maths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np


def softmax(x: np.ndarray) -> np.ndarray:
"""
Returns the returns softmax. For numerical stability we subtract the max
which will cancel out in the equation.

See:
* https://en.wikipedia.org/wiki/Softmax_function
* https://github.com/tensorflow/tensorflow/blob/85c8b2a817f95a3e979ecd1ed95bff1dc1335cff/tensorflow/python/keras/activations.py#L78
* https://stackoverflow.com/a/38250088

Parameters
----------
x : pd.Series or np.ndarray

Returns
-------
softmax : array-like
"""

e_x = np.exp(x - np.max(x))

return e_x / e_x.sum(axis=0)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ stable-baselines3[extra]

# testing requirements
pytest
parameterized==0.8.1

# packaging
setuptools>=41.4.0
Expand Down
40 changes: 40 additions & 0 deletions tests/test_utils_maths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest import TestCase

from parameterized import parameterized

from numpy import asarray, float64
from numpy.testing import assert_almost_equal
from pandas import Series

from finrl.utils import maths


class TestMaths(TestCase):
@parameterized.expand([
(
"one",
asarray([1, 2, 3]),
asarray([0.090030573, 0.244728471, 0.665240956])
),
(
"two",
asarray([0.1, 0.1, 0.8]),
asarray([0.249143401, 0.249143401, 0.501713198])
),
(
"three",
asarray([0.0, 0.0, 1.0]),
asarray([0.211941558, 0.211941558, 0.576116885])
),
(
"Should allow for using a Pandas Series",
Series([0.0, 0.0, 1.0]),
asarray([0.211941558, 0.211941558, 0.576116885])
),
])
def test_softmax_normalization(self, name, input, expected):
assert_almost_equal(
maths.softmax(input),
expected,
decimal=9
)