**What problem are we trying to solve?****Framing the problem as a Machine Learning solution****The model****The loss function****The training process****Improvements**

## What problem are we trying to solve?

Deep Learning really helped simplify the implementation of Reinforcement Learning problems. Let’s consider the portfolio optimization problem as an example. I am going to simplify the problem as much as I can to focus on the optimization aspect and how Reinforcement Learning can help with that.

Let’s consider the Nasdaq Stock Market, for example. There are about 3,300 listed companies in that market. Let’s say we want to build a stock portfolio from the stocks available in that market. In the beginning, we have an initial cash amount (e.g., $1M), and we want to allocate a percentage of that money into different stocks.

Each day, we may want to reallocate the money to update our portfolio.

Each day, the price of the individual stocks fluctuates, and the value of the portfolio evolves over time. Typically, we want the value of the portfolio to increase over time.

We also want to minimize the risk in building that portfolio. “Risk“ is usually defined as the fluctuations over time of the portfolio, and the time variance is a good measure of risk. For now, I am going to ignore risk and focus on maximizing the value of the portfolio over time. This is a bit unrealistic, but it would not be difficult to adjust the optimization problem to factor in the risk component.

The value of a portfolio is just the sum of all the money allocated in the different stocks of the portfolio. For example, if I have $10 in cash and $20 in Google stocks, my portfolio value is $10 + $20 = $30. If * V_{0}* is the initial value of the portfolio and

*its value after a time*

**V**_{T}*, we want to maximize the following quantity:*

**T**If initially, we have $1M in cash, then we are going to allocate a percentage of that money to each of the stocks available in the market. If we don’t own some specific stock, we just say the percentage is 0%. Let’s call **w _{s,t}**

_{ }the percentage allocated to a specific stock

*at time*

**S***The value of that stock in our portfolio at time*

**t.***is*

**t**where * V_{t}* is the value of our portfolio at time

*.*

**t**Every day, the prices of the stocks fluctuate and if, on average, the prices of the stocks we own increase, we make money, otherwise, we lose some.

Let’s * p_{s,t-1}* be the price of the stock

*at time*

**S***and*

**t-1***its price at time*

**p**_{s,t}*. The value of the stock in our portfolio at time*

**t***is*

**t-1***, therefore the value of that stock at time*

**v**_{s,t-1}*is*

**t**Its value is adjusted by the rate of change of the stock price. Let’s define the rate of change vector for the whole stock market (let’s assume we have * M* stocks in that market) as:

Let’s also define the percentage vector of stock allocation in our portfolio at time * t*:

Because the different components of that vector are percentages (or probabilities), we have:

Let’s say we allocate the portfolio with a vector * w_{t-1}* at

*. The rate of change for our portfolio value is*

**t-1***.*

**y**_{t}*, and the value of our portfolio after one time period is simply:*

**w**_{t-1}Because we also have

Then we have

Therefore, by applying this identity iteratively, we can express the value of the portfolio at time * T* as a function of the portfolio value at time

*:*

**0**So our optimization problem becomes maximizing the following quantify:

The only action we can take at every time step is to reallocate the money among the different stocks. So the problem becomes:

How can we reallocate the money every day so that we can maximize the portfolio returns? How do we choose

at every timestep to optimize our portfolio?w_{t}

## Framing the problem as a Machine Learning solution

## Keep reading with a 7-day free trial

Subscribe to The AiEdge Newsletter to keep reading this post and get 7 days of free access to the full post archives.