Search code examples
numpyrandomdataframequantitative-finance

How to simulate random returns with numpy


What is a quick way to simulate random returns. I'm aware of numpy.random. However, that doesn't guide me towards how to model asset returns.

I've tried:

import numpy as np

r = np.random.rand(100)

But this doesn't feel accurate. How are others dealing doing this?


Solution

  • I'd suggest one of two approaches:


    One:
    Assume returns are normally distributed with mean equal to 0.1% and stadard deviation about 1%. This looks like:

    import numpy as np
    
    np.random.seed(314)
    r = np.random.randn(100) / 100 + 0.001
    

    seed(314) sets the random number generator at a specific point so that if we both use the same seed, we should see the same results.

    randn pulls from the normal distribution.

    I'd also recommend using pandas. It's a library that implements a DataFrame object similar to R

    import pandas as pd
    
    df = pd.DataFrame(r)
    

    You can then plot the cumulative returns like this:

    df.add(1).cumprod().plot()
    

    enter image description here


    Two:
    The second way is to assume returns are log normally distributed. That means the log(r) is normal. In this scenario, we pull normally distributed random numbers and then use those values as the exponent of e. It looks like this:

    r = np.exp(np.random.randn(100) / 100 + 0.001) - 1
    

    If you plot it, it looks like this:

    pd.DataFrame(r).add(1).cumprod().plot()
    

    enter image description here