Now, after we introduce basis functions,

and saw how they can be constructed.

Let's come back to our original problem of implementing

the Backward Recursion for the optimal Q-function with Monte Carlo.

We said earlier that conditioning on FT in conditional expectations that appear in

our formulas for the Backward Recursion should be understood

as conditioning on the whole set of old Monte Carlo paths.

Now, we get the great method to compute

such expectations using the approach of basis functions.

Let's assume that we define a set of basis functions Phi_n of X,

where the argument X will be the current value of the state variable.

We can, and actually will choose

B-splines that we discussed in the last video as such functions.

But we will keep the notation general.

So that other basis functions Phi_n of X can be picked if needed as well.

In particular, we could incorporate

some external risk factors of stock prices into our set of basis functions.

For example, we could include the S&P 500 index price or volatility indexes such as VIX.

As such, risk factors that describe the state of the market,

and hence can impact the price of a particular stock.

We could also use some combinations of these factors with stock prices.

These could capture some interaction effects between stock prices and the factors.

In short, there is substantial freedom in how to choose the basis functions.

So, we will continue with the general formulation.

So, we have two unknown functions in our problem.

The optimal hedge, the optimal action,

a star of X,

and the optimal Q-function,

Q star of X.

Now, if our set of basis functions Phi_n of X is representative enough,

we can use it to approximate both functions a star of X and Q star of X.

Respectively, we present both as expansion's in the same set Phi_n of X.

Also because our problem is time-dependent,

we make coefficient of this expansion's time dependent.

So, we have two expansion's with coefficients Phi and T and

omega and NT for the optimal action and optimal Q function respectively.

Now, the problem of option pricing and hedging amounts to finding these coefficients.

And overall we have two M unknown coefficients in total.

So, if we have say 12 basis functions,

then it means that M is equal to 12.

And in this case,

we will have 24 unknown coefficients to compute.

This reduces the original infinite dimensional function optimization for functions

a star of X and Q star of X to

finite dimensional optimization with a small number of parameters.

The Backward Recursion should now be

reformulated as a scheme to compute coefficients Phi and

C and omega and T by going backwards in time starting from time capital T minus one.

Let's start with computing coefficients Phi and C of the optimal action.

To this end, let's look again at

the Backward-recursion formula for the Q function that we derive earlier.

As we mentioned before,

the right-hand side of this relation is a quadratic function of A_t.

Therefore, it will also be a quadratic function of coefficients Phi

and T. And to compute these coefficients with Monte Carlo,

we do several things with this relation.

First, we block here the expansion for A_t,

then replace all expectations by Monte Carlo averages,

and also drop all terms that are independent of A_t.

Then we flip the sign of the whole expression to turn

the maximization problem for A_t into a minimization problem.

And this gives us the objective function G_t of Phi shown here.

Which we should minimize to find optimal coefficients Phi and T. Now,

the good news is that,

because this is a quadratic function of Phi and T,

it can be minimized semi-analytically.

By setting the derivative of this expression with respect to sound value of

Phi of M_t to Zero and rearranging terms,

we get a system of linear equations for coefficients Phi and T,

that is shown here.

It's defined in terms of a square matrix A of dimension M x N,

and a vector B of M,

that are computed as shown in these two formulas.

The matrix A is positive definite,

or it can be made positive definite by regularization as we will discuss in the moment.

So, it has an inverse.

Therefore, the solution for coefficients Phi and T is simply

given by the product of the inverse of matrix A and vector B.

So, the whole calculation is as easy as the previous analytical formula,

but now we have a computable expression for the optimal action a star,

rather than a theoretical formula involving two conditional expectations.

We can in fact compare our results in

expression with the previous analytical formulas in a bit more details.

Let's write it once again as a product of the inverse of matrix a and vector B.

One thing that should be noted here is that because,

this involves matrix inversion,

it might be a good idea to supplement this calculation by

regularization for matrix A to prevent possible numerical instabilities.

The simplest practical adjustment would be to add a unit of metrics with

a small regularization parameter epsilon to matrix A as shown here.

Another point to note is that,

the resultant expression looks very similar to

our previous analytical formula for the optimal hedge.

Indeed, both the numerator and denominator in both formulas look very similar,

the only difference being in the presence of basis functions in these expressions.

These basis functions and these formulas do exactly what we want them to do.

Namely, they implement conditioning on the whole set of

Monte Carlo parts that we needed to compute optimal hedges.

Now, our formula for Phi and T used as a solution for the optimal action a star of

X at a time step T. When substituted back to the expansion of a star in basis functions.

Our next tasks therefore,

will be to find a way to compute coefficients only guarantee,

that determine the optimal Q-function.

Let's do it in our next video.