0% found this document useful (0 votes)
3 views4 pages

Simulation Engine for Portfolio Metrics

This lesson defines a simulation function for asset allocation and calculating key portfolio metrics, including the Sharpe ratio, which measures risk-adjusted return. The function takes portfolio weights and an initial investment amount as inputs and outputs expected return, volatility, Sharpe ratio, return on investment, and final portfolio value. The lesson also discusses the importance of considering risk when evaluating portfolios and provides examples of calculating the Sharpe ratio for different portfolios.

Uploaded by

yehmerafakehai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Simulation Engine for Portfolio Metrics

This lesson defines a simulation function for asset allocation and calculating key portfolio metrics, including the Sharpe ratio, which measures risk-adjusted return. The function takes portfolio weights and an initial investment amount as inputs and outputs expected return, volatility, Sharpe ratio, return on investment, and final portfolio value. The lesson also discusses the importance of considering risk when evaluating portfolios and provides examples of calculating the Sharpe ratio for different portfolios.

Uploaded by

yehmerafakehai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Define the Simulation Engine Function

[00:00:07.72] RYAN AHMED: Hello, everyone, and welcome to this lesson. In this lesson, we
will define the simulation function that performs asset allocation and also calculates the key
portfolio metrics. Please note that we are going to use this simulation function in the next lesson
when we run Monte Carlo simulations.

[00:00:29.48] So before we jump into the code, I just wanted to show you from a very high level
a new concept, which is known as Sharpe ratio. So let's assume that you have two portfolios.
These are Portfolio X and Portfolio Y. And let's assume that I told you that Portfolio X returns an
expected annual return, RpX, of 15%, and I have another portfolio, which is Portfolio Y, and that
portfolio returns expected annual return of 12%.

[00:01:01.49] So just based on this information, which portfolio would you choose? Well,
simply, Portfolio X looks like a better choice just purely based on the return because Portfolio
X's return is 15%, which is higher than Portfolio Y. The only problem with this choice is you
haven't considered the risk. We only looked at the expected annual return.

[00:01:27.59] So let me assume right now that we have additional information about Portfolio X
and Y. So we assume right now that Portfolio X, the expected annual return is still 15%, but now
I know the standard deviation, which is the dispersion away from the mean, and provides me
with an indication of the portfolio volatility, or risk. And the standard deviation for Portfolio X is
set at 7%, while the standard deviation for Portfolio Y is at 4%, meaning that Portfolio X has
higher risk compared to Portfolio Y.

[00:02:07.39] So now which one would you choose? Well, that's where Sharpe ratio comes into
play and help answer that question. So simply, Sharpe ratio is used to calculate the return of an
investment compared to its risk. And this is simply the formula to calculate Sharpe ratio.

[00:02:25.22] So we subtract Rp, which is my portfolio expected return, minus Rf, and Rf is the
risk-free rate of return, and you can use the yield from a 10-year US government bond as the
risk-free rate. And then you divide that by the standard deviation of the portfolio, or the
volatility.

[00:02:47.80] And please note that Rp minus Rf, we call that market risk premium. Simply, it's
the excess return above the risk-free rate. And Sharpe ratio from a very high level is simply a
calculation of the average return earned in excess of the risk-free rate per unit of risk. So think of
it as more of a risk adjusted return.

[00:03:13.52] So as Sharpe ratio increases, risk adjusted return increases and security becomes
more desirable by investors. And please note that we're going to calculate Sharpe ratio for the
portfolios that we're going to generate in our simulations.

[00:03:28.52] So finally, I just wanted to walk you through a quick example. So let's assume that
we have the exact same numbers for Portfolio X and Y. So I have the expected return for
Portfolio X, RpX, is 50%. The expected annual return for Portfolio Y, RpY, is 12%. I also have
the standard deviation for Portfolio X and Y as well at 7% and 4%.

[00:03:51.86] So if I would like to go up here and substitute in this equation, I will simply be
able to calculate Sharpe ratio for Portfolio X and Portfolio Y. So if I simply substitute RpX
minus Rf over sigma pX, I'm going to end up with 1.71. And Sharpe ratio for Portfolio Y, that is
going to be 12 minus 3, or 4. I will end up with 2.25.

[00:04:15.79] And just by looking at these two numbers, Portfolio Y has higher Sharpe ratio
compared to Portfolio X, and that means it offers a better risk adjusted return compared to
Portfolio X. So simply, you're just going to go and choose Portfolio Y over X.

[00:04:32.82] OK. So let me go ahead and show you the code. So what I'm going to do right now
is I'm going to define a function. Let me Zoom in a little bit. And I'm going to call this function
it's the simulation engine.

[00:04:44.59] And simply, this function is going to be the core function that performs asset
allocation, and also return back to me all the key metrics about this specific simulation run. So I
can get the expected return, I can get Sharpe ratio, I can get the return on investment. So let me
walk you through the inputs and outputs from that function.

[00:05:06.38] So the function receives the following. The function is going to receive the
portfolio weights, and it's also going to receive the initial investment amount. And then the
function is going to perform asset allocation, and also calculate portfolio statistical metrics, such
as Sharpe ratio.

[00:05:23.21] And these are the outputs from the function. The function is going to return the
expected portfolio return. It's going to also return the volatility, Sharpe ratio, return on
investment, and final portfolio value in dollars.

[00:05:37.41] So here we go. This is simply the function. First, the function is going to receive
the weights and the initial investment. First, I'm going to perform asset allocation, f. And if you
recall, we learned how to do that before. You just call the asset allocation function that we
defined in the previous couple of lessons.

[00:05:55.74] The function going to take the closed price, df, it's going to take the weights, and is
going to also take the initial investment. The function is simply going to return a pandas
dataframe that contains all the allocated assets across all ticker symbols, along with the total
value of the portfolio and also the daily return.

[00:06:16.60] So what I'm going to do next is I'm going to calculate that will be the first metric,
the return on investment. Simply, I'm going to grab the last value in my total portfolio value, and
then I'm going to subtract that minus the initial investment divided by the initial investment times
100.
[00:06:37.18] So let's assume, for example, that the portfolio, let's say returned at the end on
December 16 of 2022. The total value of the portfolio is let's say $3 million, and my initial value
was $1 million. Just substituted here, $3 million minus $1 million over $1 million. And that will
give me the overall return on investment.

[00:06:59.34] The next, simply, calculation that I'm going to do is I'm going to first drop the date
column. I'm going to drop the portfolio value. I'm going to drop the portfolio daily return. And
we're simply going to end up with the portfolio daily return underscore df, and that is going to
include only the stock positions.

[00:07:21.66] Once I do that, I'm going to calculate the percentage return or percentage daily
return using pct underscore change. We learned how to do that before. And once I do that, then I
can go ahead and simply calculate the expected portfolio return and the expected volatility, and
then I will be able to calculate Sharpe ratio.

[00:07:43.15] So please note that this is simply the core here. So Sharpe ratio is the expected
portfolio return minus rf divided by the expected volatility. Rf is the risk-free rate. So here is the
risk-free rate. And please note that I've included a link for you here, and that will help you check
out the chart for the 10-years US Treasury bills.

[00:08:07.02] And you can hear right now we're talking about approximately 3%, 3.5%. And
part of the practice opportunity, I'm going to ask you to actually go ahead change this value here
to 1% and see how would that affect of the results.

[00:08:22.99] So if you go and check out this link, and I have it open in here, what you can see,
they can simply see the return for the 10-year Treasury rate. And you can see right now as we
speak, we're talking about around 3.5%, and you can use that, an approximation to the risk-free
rate. And you can see that early on, the risk-free rate was actually standing at around 1%
approximately. OK.

[00:08:47.82] So here, I'm just going to assume it's 0.03. So right now we know how to get the
risk-free rate, or rf, and please note that we're going to use it here when we calculate Sharpe
ratio. Now I need to calculate the expected portfolio return, which is rp, and the expected
volatility. And simply, these are the two formulas to calculate the expected return and the
volatility as well.

[00:09:12.61] So here the expected portfolio return is going to be the following. I'm going to
multiply the weights times the portfolio daily return, df dot mean, and I'm going to sum that up
and multiply that by 252 because we have 252 trading days per year. I think we wanted to get the
annualized expected portfolio return.

[00:09:36.81] When it comes to the risk, this is simply the formula to calculate the risk. So the
risk of an asset is measured using the standard deviation, which indicates simply the dispersion
away from the mean. So the risk of a portfolio is not a simple sum of all the risks of individual
assets within the portfolio. That's why portfolio risk must consider all the correlations between
assets within a portfolio. And that will be indicated by the covariance matrix.
[00:10:07.12] So the covariance determined the relationship between the movements of two
random variables. When two stocks move together, they are going to have positive covariance.
When they move inversely, they have a negative covariance.

[00:10:20.10] And this is simply the calculation here. I'm going to grab my portfolio daily return,
df. I'm going to say dot cov for covariance, annualize it, and that will return the covariance. And
then I'm going to perform dot product between the covariance times the weights, multiply that
times perform dot product with the weights, obtain the square root, and that is going to calculate
the expected volatility.

[00:10:43.13] And I'm going to simply substitute here in the Sharpe ratio equation. And expected
portfolio return minus ff divided by the expected volatility, and that is going to return back for
me Sharpe ratio. And here at the end, simply, these are going to be the returns from the function.

[00:11:00.66] So when I call the function, the function is going to return back for me the
expected portfolio return, expected volatility, Sharpe ratio, and also the final value as well, and
the return on investment. These are simply all the calculations that we have done in here.

[00:11:16.13] Let's go ahead and test it out. If you press Shift and Enter, that is going to run or
execute this function. And it worked, which is great. Now I simply need to go and call that
function or invoke that function.

[00:11:29.09] You call the function with its name. So here I'm going to test it out. I'm going to
say initial investment is going to be $1,000,000. And then I'm going to call the simulation
engine, send it weights and my initial investment, and then the function is going to return
portfolio metrics.

[00:11:45.20] Let's test it out. If we press Shift-Enter, here we go. And now I can simply go
ahead and print out all the metrics, such as expected annual return, standard deviation, Sharpe
ratio, final value of the investment, and the return on investment as well. If you press Shift and
Enter right now, here we go.

[00:12:02.79] So simply, right now, based on the specific weights, random weights that I chose,
which is these weights in here, this is going to be the metrics coming out for that specific
portfolio. So here I have the expected portfolio annual return, around 15.79%, the standard
deviation or the volatility is around 19.59%, Sharpe ratio is around 0.65, the final value is $2.9
million. Please remember that our initial investment was $1,000,000. That means the return on
investment is around 190%.

[00:12:39.05] And of course, every time you go ahead and run it, you will end up with different
metrics. So that's it. That's pretty much all I have for this lesson. I hope you enjoyed it, and see
you in the next lesson.

You might also like