I have a number of given time series for bond yields (given in a dataframe in pandas package in Python). I need to do the following task in Python:

"1. Simulate 1000 path 30 steps ahead for any yield series you chose:

a. Plot the paths for any one index you chose

b. Plot the density for each index at step 30

Using the yield-difference data randomly sample 30 different values from the series. Calculate the cumulative sum of the 30 values. This gives you a possible path of yields if we start at a value of zero, but since the latest yield is not zero, we have to add it to each member of the path. Repeat this 1000 times for each series. This is a type of Monte Carlo simulation. You can use a different bootstrapping scheme for your Monte Carlo if you wish."

As a total noob in finance and programming, I am struggling to understand what I am actually required to do and how to go about this. Does the question require me to: pick out at random 30 values of a given time series, and then essentially use these 30 values to "predict" what the future values will be? And then re-do this procedure 1000 times? Or something else?

I'd be grateful if someone could explain the meaning of this task and what I could do in Python for it. Thank you.


2 Answers 2


I do not know Python but this is what I would do in Excel (I am assuming you are familiar with Excel and can then translate the steps into Python:

  1. Pick a time series of Bond Yields which has $n$ yields.
  2. Generate a series of Bond Yield changes from that time series resulting in $n-1$ yield changes in a column.
  3. Assign each of these yield changes an integer value from $1$ to $n-1$ in a column to the left of the yield changes.
  4. Use randbetween($1,n-1$) $30$ times to generate $30$ random integers between 1 and $n-1$. If $n >> 30$, then it should not matter if you sample with or without replacement.
  5. Use vlookup(random number you generated, range that includes random numbers and the yield changes, 2). This will generate a simulated path of yield changes.
  6. Add the t=0 yield of the index you chose to the first random yield change you chose from step 5. Take the result and add it to the second random yield change you generated in step 5. Repeat for each of the $30$ random yield changes. Now you have a single $30$ step path of yields. Note that this assumes that each step has the same tenor as the yield change differences.
  7. Repeat steps 4 - 6 $999$ times so you now have $1000$ $30$ step paths.
  8. Plot all $1000$ paths.
  9. Plot a histogram of all of the $1000$ terminal step $30$ values.

Monte Carlo simulation is a technique to generate a distribution of paths based on an assumed distribution (in this case historical values). You now have $1000$ possible $30$ step paths of this interest rate index, which you can then use to infer Value-At-Risk, derivative prices etc.

Hope this helps.

  • $\begingroup$ Thank you for your answer! - I think it makes sense to me; I''ll try it and then get back to you if something is unclear @AlRacoon $\endgroup$
    – Alex
    Commented Feb 12, 2018 at 1:36

To simulate a time-series with "x" random paths, there must first be a model which describes the process. The process you use may be dependent on your comfort level with stochastic processes, empirical observations about that data, and/or beliefs about the underlying process.

The Wiki entry on short rate models summarizes some of the major models which are commonly used for modeling interest rates. For simplicity, let’s choose the Kalotay–Williams–Fabozzi model, since it requires only two parameters. It is also the same thing as Geometric Brownian Motion which is commonly used to simulate stock prices (why does everyone insist on putting their names on models?).

Given the process: $$ {\displaystyle d\ln(r_{t})=\theta _{t}\,dt+\sigma \,dW_{t}}$$

The explicit solution to $r_t$ can be found through Ito's Lemma:

$$\mathbb{E}\left[r_{t+\Delta t } \right]=r_t \, e^{ (\theta _{t} - \frac{ \sigma^2}{2})\Delta t+\sigma \,Z \sqrt{\Delta t} }$$

Where Z is a normally distributed random variable with mean = 0 and variance = 1:

$Z \sim{N\left[0,1 \right]} $

To proceed with the Monte Carlo, the models needs to be calibrated. The simplest approach is to estimate the parameters for $\theta$ and $\sigma^2$ based on historical data. In the context of this model, $\theta$ is the expected logarithmic change in yield (i.e., $\mathbb{E}\left[ Ln[\frac{r_{t+ \Delta t}}{r_{t}}] \right]$) and $\sigma$ is expected standard deviation (i.e.,$\sqrt{ \mathbb{E}\left[ Ln[\frac{r_{t+ \Delta t}}{r_{t}}] -\theta\right]^2}$ ).

From there, it is simply a matter of simulating independent increments for each of the 1000 paths.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.