Due to its increasing popularity within the Machine Learning community, we dedicate a chapter to reinforcement learning (RL). In 2022 only, more than 148 papers dedicated to RL have been submitted to (or updated on) arXiv under the q:fin (quantitative finance) classification. Moreover, an early survey of RL-based portfolios is compiled in Sato (2019) (see also Zhang, Zohren, and Roberts (2020)) and general financial applications are discussed in Kolm and Ritter (2019b), Meng and Khushi (2019), Charpentier, Elie, and Remlinger (2020) and Mosavi et al. (2020). This shows that RL has recently gained traction among the quantitative finance community.
While RL is a framework much more than a particular algorithm, its efficient application in portfolio management is not straightforward, as we will show.
In this section, we introduce the core concepts of RL and follow relatively closely the notations (and layout) of Sutton and Barto (2018), which is widely considered as a solid reference in the field, along with Bertsekas (2017). One central tool in the field is called the Markov Decision Process (MDP, see Chapter 3 in Sutton and Barto (2018)).
MDPs, like all RL frameworks, involve the interaction between an agent (e.g., a trader or portfolio manager) and an environment (e.g., a financial market). The agent performs actions that may alter the state of environment and gets a reward (possibly negative) for each action. This short sequence can be repeated an arbitrary number of times, as is shown in Figure 16.1.
FIGURE 16.1: Scheme of Markov Decision Process. R, S and A stand for reward, state and action, respectively.
Given initialized values for the state of the environment ($S_0$) and reward (usually $R_0=0$), the agent performs an action (e.g., invests in some assets). This generates a reward $R_1$ (e.g., returns, profits, Sharpe ratio) and also a future state of the environment ($S_1$). Based on that, the agent performs a new action and the sequence continues. When the sets of states, actions and rewards are finite, the MDP is logically called finite. In a financial framework, this is somewhat unrealistic and we discuss this issue later on. It nevertheless is not hard to think of simplified and discretized financial problems. For instance, the reward can be binary: win money versus lose money. In the case of only one asset, the action can also be dual: investing versus not investing. When the number of assets is sufficiently small, it is possible to set fixed proportions that lead to a reasonable number of combinations of portfolio choices, etc.
We pursue our exposé with finite MDPs; they are the most common in the literature and their formal treatment is simpler. The relative simplicity of MDPs helps grasp the concepts that are common to other RL techniques. As is often the case with Markovian objects, the key notion is that of transition probability:
which is the probability of reaching state $s′$ and reward $r$ at time $t$, conditionally on being in state $s$ and performing action $a$ at time $t-1$. The finite sets of states and actions will be denoted with
$\mathcal{S}$ and $\mathcal{A}$ henceforth. Sometimes, this probability is averaged over the set of rewards which gives the following decomposition:
i.e., it is a discounted version of the reward, where the discount factor is $\gamma \in (0,1]$. The horizon $T$ may be infinite, which is why $\gamma$ was originally introduced. Assuming the rewards are bounded, the infinite sum may diverge for $\gamma=1$. That is the case if rewards don’t decrease with time and there is no reason why they should. When $\gamma <1$ and rewards are bounded, convergence is assured. When $T$ is finite, the task is called episodic and, otherwise, it is said to be continuous.
In RL, the focal unknown to be optimized or learned is the policy $\pi$, which drives the actions of the agent. More precisely, $\pi(a,s)=\mathbb{P}[A_t=a|S_t=s]$, that is, $\pi$ equals the probability of taking action $a$ if the state of the environment is $s$. This means that actions are subject to randomness, just like for mixed strategies in game theory. While this may seem disappointing because an investor would want to be sure to take the best action, it is also a good reminder that the best way to face random outcomes may well be to randomize actions as well.
Finally, in order to try to determine the best policy, one key indicator is the so-called value function:
where the time index $t$ is not very relevant and omitted in the notation of the function. The index $\pi$ under the expectation operator $\mathbb{E}[\cdot]$ simply indicates that the average is taken when the policy $\pi$ is enforced. The value function is simply equal to the average gain conditionally on the state being equal to $s$. In financial terms, this is equivalent to the average profit if the agent takes actions driven by $\pi$ when the market environment is $s$. More generally, it is also possible to condition not only on the state, but also on the action taken. We thus introduce the $q_\pi$ action-value function:
The $q_\pi$ function is highly important because it gives the average gain when the state and action are fixed. Hence, if the current state is known, then one obvious choice is to select the action for which $q_\pi(s,\cdot)$ is the highest. Of course, this is the best solution if the optimal value of $q_\pi$ is known, which is not always the case in practice. The value function can easily be accessed via $q_\pi$: $v_\pi(s)=\sum_a \pi(a,s)q_\pi(s,a)$
The optimal $v_\pi$ and $q_\pi$ are straightforwardly defined as
$v_*(s)=\underset{\pi}{\max} \, v_\pi(s), \ \forall s\in \mathcal{S}, \quad \text{ and } \quad q_*(s,a) =\underset{\pi}{\max} \, q_\pi(s,a), \ \forall (s,a)\in \mathcal{S}\times \mathcal{A}.$
If only $v_∗(s)$ is known, then the agent must span the set of actions and find those that yield the maximum value for any given state s.
Finding these optimal values is a very complicated task and many articles are dedicated to solving this challenge. One reason why finding the best $q_\pi(s,a)$ is difficult is because it depends on two elements ($s$ and $a$) on one side and $π$ on the other. Usually, for a fixed policy $π$, it can be time consuming to evaluate $q_\pi(s,a)$ for a given stream of actions, states and rewards. Once $q_\pi(s,a)$ is estimated, then a new policy $π′$ must be tested and evaluated to determine if it is better than the original one. Thus, this iterative search for a good policy can take long. For more details on policy improvement and value function updating, we recommend chapter 4 of Sutton and Barto (2018) which is dedicated to dynamic programming.
An interesting shortcut to the problem of finding $v_*(s)$ and $q_*(s,a)$ is to remove the dependence on the policy. Consequently, there is then of course no need to iteratively improve it. The central relationship that is required to do this is the so-called Bellman equation that is satisfied by $q_\pi(s,a)$. We detail its derivation below. First of all, we recall that
where the second equality stems from (16.3). The expression $\mathbb{E}_\pi[R_{t+1}|S_t=s,A_t=a]$ can be further decomposed. Since the expectation runs over $π$, we need to sum over all possible actions $a′$ and states $s′$ and resort to $\pi(a',s')$. In addition, the sum on the $s′$ and $r$ arguments of the probability $p(s',r|s,a)=\mathbb{P}\left[S_{t+1}=s',R_{t+1}=r | S_t=s,A_t=a \right]$ gives access to the distribution of the random couple $(S_{t+1},R_{t+1})$ so that in the end $\mathbb{E}_\pi[R_{t+1}|S_t=s,A_t=a]=\sum_{a', r,s'}\pi(a',s')p(s',r|s,a) r$. A similar reasoning applies to the second portion of $q_\pi$ and:
This equation links $q_\pi(s,a)$ to the future $q_\pi(s',a')$ from the states and actions $(s',a')$ that are accessible from $(s,a)$.
Notably, Equation (16.6) is also true for the optimal action-value function $q_*=\underset{\pi}{\max} \, q_\pi(s,a)$:
because one optimal policy is one that maximizes $q_\pi(s,a)$, for a given state $s$ and over all possible actions $a$. This expression is central to a cornerstone algorithm in reinforcement learning called $Q$-learning (the formal proof of convergence is outlined in Watkins and Dayan (1992)). In $Q$-learning, the state-action function no longer depends on policy and is written with capital $Q$. The process is the following:
Initialize values $Q(s,a)$ for all states $s$ and actions $a$. For each episode:
$(\textbf{QL}) \quad \left\{ \begin{array}{l} \text{0. Initialize state } S_0 \text{ and for each iteration } i \text{ until the end of the episode;} \\ \text{1. observe state } s_i; \\ \text{2. perform action } a_i \text{(depending on } Q); \\ \text{3. receive reward }r_{i+1} \text{ and observe state } s_{i+1}; \\ \text{4. Update } Q \text{ as follows: } \end{array} \right.$
The underlying reason this update rule works can be linked to fixed point theorems of contraction mappings. If a function $f$ satisfies $|f(x)-f(y)|< \delta |x-y|$ (Lipshitz continuity), then a fixed point $z$ satisfying $f(z)=z$ can be iteratively obtained via $z \leftarrow f(z)$. This updating rule converges to the fixed point. Equation (16.7) can be solved using a similar principle, except that a learning rate $\eta$ slows the learning process but also technically ensures convergence under technical assumptions.
More generally, (16.8) has a form that is widespread in reinforcement learning that is summarized in Equation (2.4) of Sutton and Barto (2018):
where the last part can be viewed as an error term. Starting from the old estimate, the new estimate therefore goes in the ‘right’ (or sought) direction, modulo a discount term that makes sure that the magnitude of this direction is not too large. The update rule in (16.8) is often referred to as ‘temporal difference’ learning because it is driven by the improvement yielded by estimates that are known at time $t+1$ (target) versus those known at time $t$.
One important step of the Q-learning sequence (QL) is the second one where the action $a_i$ is picked. In RL, the best algorithms combine two features: exploitation and exploration. Exploitation is when the machine uses the current information at its disposal to choose the next action. In this case, for a given state $s_i$, it chooses the action $a_i$ that maximizes the expected reward $Q_i(s_i,a_i)$. While obvious, this choice is not optimal if the current function $Q_i$ is relatively far from the true $Q$. Repeating the locally optimal strategy is likely to favor a limited number of actions, which will narrowly improve the accuracy of the $Q$ function.
In order to gather new information stemming from actions that have not been tested much (but that can potentially generate higher rewards), exploration is needed. This is when an action $a_i$ is chosen randomly. The most common way to combine these two concepts is called $\epsilon$-greedy exploration. The action $a_i$ is assigned according to:
$\begin{equation} \tag{16.10} a_i=\left\{ \begin{array}{c l} \underset{a}{\text{argmax}} \ Q_i(s_i,a) & \text{ with probability } 1-\epsilon \\ \text{randomly (uniformly) over } \mathcal{A} & \text{ with probability } \epsilon \end{array}\right. . \end{equation}$
Thus, with probability $ϵ$, the algorithm explores and with probability $1−ϵ$, it exploits the current knowledge of the expected reward and picks the best action. Because all actions have a non-zero probability of being chosen, the policy is called “soft”. Indeed, then best action has a probability of selection equal to $1-\epsilon(1-\text{card}(\mathcal{A})^{-1})$, while all other actions are picked with probability $\epsilon/\text{card}(\mathcal{A})$.
In $Q$-learning, the algorithm seeks to find the action-value function of the optimal policy. Thus, the policy that is followed to pick actions is different from the one that is learned (via $Q$). Such algorithms are called off-policy. On-policy algorithms seek to improve the estimation of the action-value function $q_π$ by continuously acting according to the policy $π$. One canonical example of on-policy learning is the SARSA method which requires two consecutive states and actions SARSA. The way the quintuple $(S_t,A_t,R_{t+1}, S_{t+1}, A_{t+1})$ is processed is presented below.
The main difference between $Q$ learning and SARSA is the update rule. In SARSA, it is given by
The improvement comes only from the local point $Q_i(s_{i+1},a_{i+1})$ that is based on the new states and actions ($s_{i+1},a_{i+1}$), whereas in $Q$-learning, it comes from all possible actions of which only the best is retained $\underset{a}{\max} \, Q_i(s_{i+1},a)$.
A more robust but also more computationally demanding version of SARSA is expected SARSA in which the target $Q$ function is averaged over all actions:
Expected SARSA is less volatile than SARSA because the latter is strongly impacted by the random choice of $a_{i+1}$. In expected SARSA, the average smoothes the learning process.
Let us first recall that reinforcement learning is a framework that is not linked to a particular algorithm. In fact, different tools can very well co-exist in a RL task (AlphaGo combined both tree methods and neural networks, see Silver et al. (2016)). Nonetheless, any RL attempt will always rely on the three key concepts: the states, actions and rewards. In factor investing, they are fairly easy to identify, though there is always room for interpretation. Actions are evidently defined by portfolio compositions. The states can be viewed as the current values that describe the economy: as a first-order approximation, it can be assumed that the feature levels fulfill this role (possibly conditioned or complemented with macro-economic data). The rewards are even more straightforward. Returns or any relevant performance metric can account for rewards.
A major problem lies in the dimensionality of both states and actions. Assuming an absence of leverage (no negative weights), the actions take values on the simplex
and assuming that all features have been uniformized, their space is $[0,1]^{NK}$. Needless to say, the dimensions of both spaces are numerically impractical.
A simple solution to this problem is discretization: each space is divided into a small number of categories. Some authors do take this route. In Yang, Yu, and Almahdi (2018), the state space is discretized into three values depending on volatility, and actions are also split into three categories. Bertoluzzo and Corazza (2012) and Xiong et al. (2018) also choose three possible actions (buy, hold, sell). In Almahdi and Yang (2019), the learner is expected to yield binary signals for buying or shorting. Garcı́a-Galicia, Carsteanu, and Clempner (2019) consider a larger state space (8 elements) but restrict the action set to 3 options. In terms of the state space, all articles assume that the state of the economy is determined by prices (or returns).
One strong limitation of these approaches is the marked simplification they imply. Realistic discretizations are numerically intractable when investing in multiple assets. Indeed, splitting the unit interval in $h$ points yields $h^{NK}$ possibilities for feature values. The number of options for weight combinations is exponentially increasing $N$. As an example: just 10 possible values for 10 features of 10 stocks yield $10^{100}$ permutations.
The problems mentioned above are of course not restricted to portfolio construction. Many solutions have been proposed to solve Markov Decision Processes in continuous spaces. We refer for instance to Section 4 in Powell and Ma (2011) for a review of early methods (outside finance).
This curse of dimensionality is accompanied by the fundamental question of training data. Two options are conceivable: market data versus simulations. Under a given controlled generator of samples, it is hard to imagine that the algorithm will beat the solution that maximizes a given utility function. If anything, it should converge towards the static optimal solution under a stationary data generating process (see, e.g., Chaouki et al. (2020) for trading tasks), which is by the way a very strong modelling assumption.
This leaves market data as a preferred solution but even with large datasets, there is little chance to cover all the (actions, states) combinations mentioned above. Characteristics-based datasets have depths that run through a few decades of monthly data, which means several hundreds of time-stamps at most. This is by far too limited to allow for a reliable learning process. It is always possible to generate synthetic data (as in Yu et al. (2019)), but it is unclear that this will solidly improve the performance of the algorithm.
Beyond the discretization of action and state spaces, a powerful trick is parametrization. When $a$ and $s$ can take discrete values, action-value functions must be computed for all pairs $(a,s)$, which can be prohibitively cumbersome. An elegant way to circumvent this problem is to assume that the policy is driven by a relatively modest number of parameters. The learning process is then focused on optimizing this set of parameters $\theta$. We then write $\pi_{\boldsymbol{\theta}}(a,s)$ for the probability of choosing action $a$ in state $s$. One intuitive way to define $\pi_{\boldsymbol{\theta}}(a,s)$ is to resort to a soft-max form:
where the output of function $\textbf{h}(a,s)$, which has the same dimension as $\theta$ is called a feature vector representing the pair $(a,s)$. Typically, $h$ can very well be a simple neural network with two input units and an output dimension equal to the length of $\theta$.
One desired property for $\pi_{\boldsymbol{\theta}}$ is that it be differentiable with respect to $\theta$ so that $\theta$ can be improved via some gradient method. The most simple and intuitive results about policy gradients are known in the case of episodic tasks (finite horizon) for which it is sought to maximize the average gain $\mathbb{E}_{\boldsymbol{\theta}}[G_t]$ where the gain is defined in Equation (16.3). The expectation is computed according to a particular policy that depends on $\theta$, this is why we use a simple subscript. One central result is the so-called policy gradient theorem which states that
This result can then be used for gradient ascent: when seeking to maximize a quantity, the parameter change must go in the upward direction:
This simple update rule is known as the REINFORCE algorithm. One improvement of this simple idea is to add a baseline, and we refer to section 13.4 of Sutton and Barto (2018) for a detailed account on this topic.
A popular extension of REINFORCE is the so-called actor-critic (AC) method which combines policy gradient with $Q$- or $v$-learning. The AC algorithm can be viewed as some kind of mix between policy gradient and SARSA. A central requirement is that the state-value function $v(\cdot)$ be a differentiable function of some parameter vector $\textbf{w}$ (it is often taken to be a neural network). The update rule is then
but the trick is that the vector $\textbf{w}$ must also be updated. The actor is the policy side which is what drives decision making. The critic side is the value function that evaluates the actor’s performance. As learning progresses (each time both sets of parameters are updated), both sides improve. The exact algorithmic formulation is a bit long and we refer to Section 13.5 in Sutton and Barto (2018) for the precise sequence of steps of AC.
Another interesting application of parametric policies is outlined in Aboussalah and Lee (2020). In their article, the authors define a trading policy that is based on a recurrent neural network. Thus, the parameter $\theta$ in this case encompasses all weights and biases in the network.
Another favorable feature of parametric policies is that they are compatible with continuous sets of actions. Beyond the form (16.14), there are other ways to shape $\pi_{\boldsymbol{\theta}}$. If $\mathcal{A}$ is a subset of $\mathbb{R}$, and $f_{\boldsymbol{\Omega}}$ is a density function with parameters $\boldsymbol{\Omega}$, then a candidate form for $\pi_{\boldsymbol{\theta}}$ is
in which the parameters $Ω$ are in turn functions of the states and of the underlying (second order) parameters $\theta$.
While the Gaussian distribution (see section 13.7 in Sutton and Barto (2018)) is often a preferred choice, they would require some processing to lie inside the unit interval. One easy way to obtain such values is to apply the normal cumulative distribution function to the output. In Wang and Zhou (2019), the multivariate Gaussian policy is theoretically explored, but it assumes no constraint on weights.
Some natural parametric distributions emerge as alternatives. If only one asset is traded, then the Bernoulli distribution can be used to determine whether or not to buy the asset. If a riskless asset is available, the beta distribution offers more flexibility because the values for the proportion invested in the risky asset span the whole interval; the remainder can be invested into the safe asset. When many assets are traded, things become more complicated because of the budget constraint. One ideal candidate is the Dirichlet distribution because it is defined on a simplex (see Equation (16.13)):
where $B(\boldsymbol{\alpha})$ is the multinomial beta function:
If we set $\pi=\pi_{\boldsymbol{\alpha}}=f_{\boldsymbol{\alpha}}$, the link with factors or characteristics can be coded through $α$ via a linear form:
which is highly tractable, but may violate the condition that $\alpha_{n,t}>0$ for some values of $\theta_{k,t}$. Indeed, during the learning process, an update in $\theta$ might yield values that are out of the feasible set of $\boldsymbol{\alpha}_t$. In this case, it is possible to resort to a trick that is widely used in online learning (see, e.g., section 2.3.1 in ). The idea is simply to find the acceptable solution that is closest to the suggestion from the algorithm. If we call $\boldsymbol{\theta}^*$ the result of an update rule from a given algorithm, then the closest feasible vector is
where $||\cdot||$ is the Euclidean norm and $\Theta(\textbf{x}_t)$ is the feasible set, that is, the set of vectors $\theta$ such that the $\alpha_{n,t}=\theta_{0,t} + \sum_{k=1}^K \theta_{t}^{(k)}x_{t,n}^{(k)}$ are all non-negative.
A second option for the form of the policy, $\pi^2_{\boldsymbol{\theta}_t}$, is slightly more complex but remains always valid (i.e., has positive $\alpha_{n,t}$ values):
which is simply the exponential of the first version. With some algebra, it is possible to derive the policy gradients. The policies $\pi^j_{\boldsymbol{\theta}_t}$ are defined by the Equations $(\textbf{Fj})$ above. Let $\digamma$ denote the digamma function. Let $1$ denote the $\mathbb{R}^N$ vector of all ones. We have
where $e^{\textbf{X}}$ is the element-wise exponential of a matrix $\textbf{X}$.
The allocation can then either be made by direct sampling, or using the mean of the distribution $(\textbf{1}'\boldsymbol{\alpha})^{-1}\boldsymbol{\alpha}$. Lastly, a technical note: Dirichlet distributions can only be used for small portfolios because the scaling constant in the density becomes numerically intractable for large values of $N$ (e.g., above 50).
To illustrate the gist of the problems mentioned above, we propose two implementations of $Q$-learning. For simplicity, the first one is based on simulations. This helps understand the learning process in a simplified framework. We consider two assets: one risky and one riskless, with return equal to zero. The returns for the risky process follow an autoregressive model of order one (AR(1)): $r_{t+1}=a+\rho r_t+\epsilon_{t+1}$ with $|\rho|<1$ and $ϵ$ following a standard white noise with variance $\sigma^2$. In practice, individual (monthly) returns are seldom autocorrelated, but adjusting the autocorrelation helps understand if the algorithm learns correctly (see exercise below).
The environment consists only in observing the past return $r_t$. Since we seek to estimate the $Q$ function, we need to discretize this state variable. The simplest choice is to resort to a binary variable: equal to -1 (negative) if $r_t<0$ and to +1 (positive) if $r_t\ge 0$. The actions are summarized by the quantity invested in the risky asset. It can take 5 values: 0 (risk-free portfolio), 0.25, 0.5, 0.75 and 1 (fully invested in the risky asset). This is for instance the same choice as in Pendharkar and Cusatis (2018).
For the sake of understanding, we resort to code an intuitive implementation of $Q$-learning ourselves. It requires a dataset with the usual inputs: state, action, reward and subsequent state. We start by simulating the returns: they drive the states and the rewards (portfolio returns). The actions are sampled randomly. The data is built in the chunk below.
from statsmodels.tsa.arima_process import ArmaProcess # Sub-library for generating AR(1)
import numpy as np
import pandas as pd
n_sample = 10**5 # Number of samples to be generated
rho=0.8 # Autoregressive parameter
sd=0.4 # Std. dev. of noise
a=0.06*(1-rho) # Scaled mean of returns
#
ar1 = np.array([1, -rho]) # template for ar param, note that you need to inverse the sign of rho
AR_object1 = ArmaProcess(ar1) # Creating the AR object
simulated_data_AR1 = AR_object1.generate_sample(nsample=n_sample,scale=sd) # generate sample from AR object
#
returns=a/rho+simulated_data_AR1 # Returns via AR(1) simulation
action = np.round(np.random.uniform(size=n_sample)*4) / 4 # Random action (portfolio)
state = np.where(returns < 0, "neg", "pos") # Coding of state
reward = returns * action # Reward = portfolio return
#
data_RL = pd.DataFrame([returns, action, state, reward]).T # transposing for display consistency
data_RL.columns = ['returns', 'action', 'state', 'reward'] # naming the columns for future table print
data_RL['new_state'] = data_RL['state'].shift(-1) # Next state using lag
data_RL = data_RL.dropna(axis=0).reset_index(drop=True) # Remove one missing new state, last row
data_RL.head() # Show first lines
returns | action | state | reward | new_state | |
---|---|---|---|---|---|
0 | 0.063438 | 0.75 | pos | 0.047579 | neg |
1 | -0.602351 | 0.25 | neg | -0.150588 | pos |
2 | 0.112012 | 0.5 | pos | 0.056006 | pos |
3 | 0.220316 | 0.5 | pos | 0.110158 | pos |
4 | 0.437028 | 0.75 | pos | 0.327771 | pos |
There are 3 parameters in the implementation of the Q-learning algorithm:
alpha = 0.1 # Learning rate
gamma = 0.7 # Discount factor for rewards
epsilon = 0.5 # Exploration rate
def looping_w_counters(obj_array): # create util function for loop with counter
_dict = {z:i for i,z in enumerate(obj_array)} # Dictionary comprehensions
return _dict
s =looping_w_counters(data_RL['state'].unique()) # Dict for states
a =looping_w_counters(data_RL['action'].unique()) # Dict for actions
fit_RL = np.zeros(shape=(len(s),len(a))) # Placeholder for Q matrix
r_final = 0
for z, row in data_RL.iterrows(): # loop for Q-learning
act = a[row.action]
r = row.reward
s_current = s[row.state]
s_new = s[row.new_state]
if np.random.uniform(size=1) < epsilon:
best_new = a[np.random.choice(list(a.keys()))] # Explore action space
else:
best_new = np.argmax(fit_RL[s_new,]) # Exploit learned values
r_final += r
fit_RL[s_current, act] += alpha * (r + gamma * fit_RL[s_new, best_new] - fit_RL[s_current, act])
fit_RL=pd.DataFrame(fit_RL, index=s.keys(), columns=a.keys()).sort_index(axis = 1)
print(fit_RL)
print(f'Reward (last iteration): {r_final}')
0.00 0.25 0.50 0.75 1.00 pos 0.730337 1.099200 0.863375 1.409665 1.503934 neg 0.299238 0.121513 0.012260 0.131469 -0.041919 Reward (last iteration): 456.1509459790951
The output shows the Q function, which depends naturally both on states and actions. When the state is negative, large risky positions (action equal to 0.75 or 1.00) are associated with the smallest average rewards, whereas small positions yield the highest average rewards. When the state is positive, the average rewards are the highest for the largest allocations. The rewards in both cases are almost a monotonic function of the proportion invested in the risky asset. Thus, the recommendation of the algorithm (i.e., the policy) is to be fully invested in a positive state and to refrain from investing in a negative state. Given the positive autocorrelation of the underlying process, this does make sense.
Basically, the algorithm has simply learned that positive (resp. negative) returns are more likely to follow positive (resp. negative) returns. While this is somewhat reassuring, it is by no means impressive, and much simpler tools would yield similar conclusions and guidance.
The second application is based on the financial dataset. To reduce the dimensionality of the problem, we will assume that:
The construction of the dataset is unelegantly coded below.
return_3 = pd.Series(data_ml.loc[data_ml['stock_id']==3, 'R1M_Usd'].values) # Return of asset 3
return_4 = pd.Series(data_ml.loc[data_ml['stock_id']==4, 'R1M_Usd'].values) # Return of asset 4
pb_3 = pd.Series(data_ml.loc[data_ml['stock_id']==3, 'Pb'].values) # P/B ratio of asset 3
pb_4 = pd.Series(data_ml.loc[data_ml['stock_id']==4, 'Pb'].values) # P/B ratio of asset 4
action_3 = pd.Series(np.floor(np.random.uniform(size=len(pb_3))*3) - 1) # Action for asset 3 (random)
action_4 = pd.Series(np.floor(np.random.uniform(size=len(pb_4))*3) - 1) # Action for asset 4 (random)
RL_data = pd.concat([return_3, return_4, pb_3, pb_4, action_3, action_4],axis=1) # Building the dataset
RL_data.columns = ['return_3','return_4', 'Pb_3','Pb_4','action_3', 'action_4'] # Adding columns names
RL_data['action']=RL_data.action_3.astype(int).apply(str)+" "+RL_data.action_4.astype(int).apply(str) # Uniting actions
RL_data['Pb_3'] = np.round(5*RL_data['Pb_3']) # Simplifying states (P/B)
RL_data['Pb_4'] = np.round(5*RL_data['Pb_4']) # Simplifying states (P/B)
RL_data['state'] = RL_data.Pb_3.astype(int).apply(str)+" "+RL_data.Pb_4.astype(int).apply(str) # Uniting states
RL_data['new_state'] = RL_data['state'].shift(-1) # Infer new state
RL_data['reward'] = RL_data.action_3*RL_data.return_3+RL_data.action_4*RL_data.return_4 # Computing rewards
RL_data = RL_data[['action','state','reward','new_state']].dropna(axis=0).reset_index(drop=True) # Remove one missing new state, last row
RL_data.head() # Show first lines
action | state | reward | new_state | |
---|---|---|---|---|
0 | -1 0 | 1 1 | -0.077 | 1 1 |
1 | 0 -1 | 1 1 | -0.000 | 1 1 |
2 | -1 0 | 1 1 | -0.018 | 1 1 |
3 | 1 1 | 1 1 | 0.016 | 1 1 |
4 | 0 1 | 1 1 | 0.014 | 1 1 |
Actions and states have to be merged to yield all possible combinations. To simplify the states, we round 5 times the price-to-book ratios.
We keep the same hyperparameters as in the previous example. Columns below stand for actions: the first ( $resp.$ second) number notes the position in the first ($resp.$ second) asset. The rows correspond to states. The scaled P/B ratios are separated by a point (e.g., “X2.3” means that the first ( $resp.$ second) asset has a scaled P/B of 2 ( $resp.$ 3).
alpha = 0.1 # Learning rate
gamma = 0.7 # Discount factor for rewards
epsilon = 0.1 # Exploration rate
s =looping_w_counters(RL_data['state'].unique()) # Dict for states
a =looping_w_counters(RL_data['action'].unique()) # Dict for actions
fit_RL2 = np.zeros(shape=(len(s),len(a))) # Placeholder for Q matrix
r_final = 0
for z, row in RL_data.iterrows(): # loop for Q-learning
act = a[row.action]
r = row.reward
s_current = s[row.state]
s_new = s[row.new_state]
if np.random.uniform(size=1) < epsilon: # Explore action space
best_new = a[np.random.choice(list(a.keys()))]
else:
best_new = np.argmax(fit_RL2[s_new,]) # Exploit learned values
r_final += r
fit_RL2[s_current, act] += alpha * (r + gamma * fit_RL2[s_new, best_new] - fit_RL2[s_current, act])
fit_RL2=pd.DataFrame(fit_RL2, index=s.keys(), columns=a.keys()).sort_index(axis = 1)
print(fit_RL2)
print(f'Reward (last iteration): {r_final}')
-1 -1 -1 0 -1 1 0 -1 0 0 0 1 1 -1 \ 1 1 0.009832 -0.019082 0.000117 0.004888 0.004349 0.006774 0.015898 2 1 0.018813 0.001866 0.011900 -0.005006 0.003401 0.007681 -0.002214 2 2 -0.008100 -0.006100 0.000000 -0.009486 0.000000 -0.003830 -0.012200 3 1 0.000000 0.000000 -0.003000 0.000000 0.001131 0.000000 0.000000 1 2 0.006686 0.012915 0.007092 0.010514 0.011722 0.026586 0.003747 2 3 0.000000 0.025063 0.031800 0.000000 0.000000 0.000000 0.000000 1 3 0.000000 -0.010912 -0.005928 0.000000 0.001087 -0.001939 0.000000 0 3 0.015800 -0.024012 0.000000 -0.001600 0.000000 0.000000 0.000000 0 2 0.000000 -0.013800 -0.010209 0.001000 0.000000 0.000000 0.000000 1 0 1 1 1 1 0.010850 0.019301 2 1 0.006473 0.004977 2 2 -0.011600 -0.008400 3 1 0.000000 -0.010100 1 2 0.053084 0.006205 2 3 0.000000 0.000000 1 3 -0.005000 0.005528 0 3 0.000000 0.000000 0 2 0.000000 0.000000 Reward (last iteration): -0.374
The output shows that there are many combinations of states and actions that are not spanned by the data: basically, the $Q$ function has a zero and it is likely that the combination has not been explored. Some states seem to be more often represented (“X1.1”, “X1.2” and “X2.1”), others, less (“X3.1” and “X3.2”). It is hard to make any sense of the recommendations. Some states close “X0.1” and “X1.1” but the outcomes related to them are very different (buy and short versus hold and buy). Moreover, there is no coherence and no monotonicity in actions with respect to individual state values: low values of states can be associated to very different actions.
One reason why these conclusions do not appear trustworthy pertains to the data size. With only 200+ time points and 99 state-action pairs (11 times 9), this yields on average only two data points to compute the $Q$ function. This could be improved by testing more random actions, but the limits of the sample size would eventually (rapidly) be reached anyway. This is left as an exercise (see below).
Reinforcement learning has been applied to financial problems for a long time. Early contributions in the late 1990s include Neuneier (1996), Moody and Wu (1997), Moody et al. (1998) and Neuneier (1998). Since then, many researchers in the computer science field have sought to apply RL techniques to portfolio problems. The advent of massive datasets and the increase in dimensionality make it hard for RL tools to adapt well to very rich environments that are encountered in factor investing.
Recently, some approaches seek to adapt RL to continuous action spaces (Wang and Zhou (2019), Aboussalah and Lee (2020)) but not to high-dimensional state spaces. These spaces are those required in factor investing because all firms yield hundreds of data points characterizing their economic situation. In addition, applications of RL in financial frameworks have a particularity compared to many typical RL tasks: in financial markets, actions of agents have no impact on the environment (unless the agent is able to perform massive trades, which is rare and ill-advised because it pushes prices in the wrong direction). This lack of impact of actions may possibly mitigate the efficiency of traditional RL approaches.
Those are challenges that will need to be solved in order for RL to become competitive with alternative (supervised) methods. Nevertheless, the progressive (online-like) way RL works seems suitable for non-stationary environments: the algorithm slowly shifts paradigms as new data arrives. In stationary environments, it has been shown that RL manages to converge to optimal solutions (Kong et al. (2019), Chaouki et al. (2020)). Therefore, in non-stationary markets, RL could be a recourse to build dynamic predictions that adapt to changing macroeconomic conditions. More research needs to be carried out in this field on large dimensional datasets.
We end this chapter by underlining that reinforcement learning has also been used to estimate complex theoretical models (Halperin and Feldshteyn (2018), Garcı́a-Galicia, Carsteanu, and Clempner (2019)). The research in the field is incredibly diversified and is orientated towards many directions. It is likely that captivating work will be published in the near future.
Aboussalah, Amine Mohamed, and Chi-Guhn Lee. 2020. “Continuous Control with Stacked Deep Dynamic Recurrent Reinforcement Learning for Portfolio Optimization.” Expert Systems with Applications 140: 112891.
Almahdi, Saud, and Steve Y Yang. 2019. “A Constrained Portfolio Trading System Using Particle Swarm Algorithm and Recurrent Reinforcement Learning.” Expert Systems with Applications 130: 145–56.
Bertoluzzo, Francesco, and Marco Corazza. 2012. “Testing Different Reinforcement Learning Configurations for Financial Trading: Introduction and Applications.” Procedia Economics and Finance 3: 68–77.
Bertsekas, Dimitri P. 2017. Dynamic Programming and Optimal Control - Volume Ii, Fourth Edition. Athena Scientific.
Chaouki, Ayman, Stephen Hardiman, Christian Schmidt, Joachim de Lataillade, and others. 2020. “Deep Deterministic Portfolio Optimization.” arXiv Preprint, no. 2003.06497.
Charpentier, Arthur, Romuald Elie, and Carl Remlinger. 2020. “Reinforcement Learning in Economics and Finance.” arXiv Preprint, no. 2003.10014.
Garcı́a-Galicia, Mauricio, Alin A Carsteanu, and Julio B Clempner. 2019. “Continuous-Time Reinforcement Learning Approach for Portfolio Management with Time Penalization.” Expert Systems with Applications 129: 27–36.
Halperin, Igor, and Ilya Feldshteyn. 2018. “Market Self-Learning of Signals, Impact and Optimal Trading: Invisible Hand Inference with Free Energy.” arXiv Preprint, no. 1805.06126.
Kolm, Petter N, and Gordon Ritter. 2019b. “Modern Perspectives on Reinforcement Learning in Finance.” Journal of Machine Learning in Finance 1 (1).
Kong, Weiwei, Christopher Liaw, Aranyak Mehta, and D Sivakumar. 2019. “A New Dog Learns Old Tricks: RL Finds Classic Optimization Algorithms.” Proceedings of the ICLR Conference, 1–25.
Meng, Terry Lingze, and Matloob Khushi. 2019. “Reinforcement Learning in Financial Markets.” Data 4 (3): 110.
Moody, John, and Lizhong Wu. 1997. “Optimization of Trading Systems and Portfolios.” In Proceedings of the Ieee/Iafe 1997 Computational Intelligence for Financial Engineering (Cifer), 300–307. IEEE.
Moody, John, Lizhong Wu, Yuansong Liao, and Matthew Saffell. 1998. “Performance Functions and Reinforcement Learning for Trading Systems and Portfolios.” Journal of Forecasting 17 (5-6): 441–70.
Mosavi, Amir, Pedram Ghamisi, Yaser Faghan, Puhong Duan, and Shahab Shamshirband. 2020. “Comprehensive Review of Deep Reinforcement Learning Methods and Applications in Economics.” arXiv Preprint, no. 2004.01509.
Neuneier, Ralph. 1996. “Optimal Asset Allocation Using Adaptive Dynamic Programming.” In Advances in Neural Information Processing Systems, 952–58.
Neuneier, Ralph. 1998. “Enhancing Q-Learning for Optimal Asset Allocation.” In Advances in Neural Information Processing Systems, 936–42.
Pendharkar, Parag C, and Patrick Cusatis. 2018. “Trading Financial Indices with Reinforcement Learning Agents.” Expert Systems with Applications 103: 1–13.
Powell, Warren B, and Jun Ma. 2011. “A Review of Stochastic Algorithms with Continuous Value Function Approximation and Some New Approximate Policy Iteration Algorithms for Multidimensional Continuous Applications.” Journal of Control Theory and Applications 9 (3): 336–52.
Sato, Yoshiharu. 2019. “Model-Free Reinforcement Learning for Financial Portfolios: A Brief Survey.” arXiv Preprint, no. 1904.04973.
Silver, David, Aja Huang, Chris J Maddison, Arthur Guez, Laurent Sifre, George Van Den Driessche, Julian Schrittwieser, Ioannis Antonoglou, Veda Panneershelvam, and Marc Lanctot. 2016. “Mastering the Game of Go with Deep Neural Networks and Tree Search.” Nature 529: 484–89.
Sutton, Richard S, and Andrew G Barto. 2018. Reinforcement Learning: An Introduction (2nd Edition). MIT Press.
Wang, Haoran, and Xun Yu Zhou. 2019. “Continuous-Time Mean-Variance Portfolio Selection: A Reinforcement Learning Framework.” SSRN Working Paper 3382932.
Watkins, Christopher JCH, and Peter Dayan. 1992. “Q-Learning.” Machine Learning 8 (3-4): 279–92.
Xiong, Zhuoran, Xiao-Yang Liu, Shan Zhong, Hongyang Yang, and Anwar Walid. 2018. “Practical Deep Reinforcement Learning Approach for Stock Trading.” arXiv Preprint, no. 1811.07522.
Yang, Steve Y, Yangyang Yu, and Saud Almahdi. 2018. “An Investor Sentiment Reward-Based Trading System Using Gaussian Inverse Reinforcement Learning Algorithm.” Expert Systems with Applications 114: 388–401.
Yu, Pengqian, Joon Sern Lee, Ilya Kulyatin, Zekun Shi, and Sakyasingha Dasgupta. 2019. “Model-Based Deep Reinforcement Learning for Dynamic Portfolio Optimization.” arXiv Preprint, no. 1901.08740.
Zhang, Zihao, Stefan Zohren, and Stephen Roberts. 2020. “Deep Reinforcement Learning for Trading.” Journal of Financial Data Science 2 (2): 25–40.
footnotes