CommencerCommencer gratuitement

Portfolio Simulation - Part I

In the next few exercises, you will calculate the expected returns of a stock portfolio & characterize its uncertainty.

Suppose you have invested $10,000 in your portfolio comprising of multiple stocks. You want to evaluate the portfolio's performance over 10 years. You can tweak your overall expected rate of return and volatility (standard deviation of the rate of return). Assume the rate of return follows a normal distribution.

First, let's write a function that takes the principal (initial investment), number of years, expected rate of return and volatility as inputs and returns the portfolio's total value after 10 years.

Upon completion of this exercise, you will have a function you can call to determine portfolio performance.

Cet exercice fait partie du cours

Statistical Simulation in Python

Afficher le cours

Instructions

  • In the function definition, accept four arguments: number of years yrs, the expected rate of return avg_return, volatility sd_of_return, and principal (initial investment) principal as inputs.
  • Simulate rates of return for each year as a normal random variable.
  • Initialize end_return to the principal input. In the for loop, end_return is scaled up by the rate each year.
  • Use portfolio_return() to calculate and print result.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# rates is a Normal random variable and has size equal to number of years
def portfolio_return(____):
    np.random.seed(123)
    rates = ____(loc=avg_return, scale=sd_of_return, size=yrs)
    # Calculate the return at the end of the period
    end_return = ____
    for x in rates:
        end_return = end_return*(1+____)
    return end_return

result = portfolio_return(yrs = 5, avg_return = 0.07, sd_of_return = 0.15, principal = 1000)
print("Portfolio return after 5 years = {}".format(____))
Modifier et exécuter le code