Returns Path

perfana.monte_carlo.returns.returns_path(data, weights, rebalance=True, quantile=None)[source]

Returns a matrix of the returns path of the portfolio.

The first axis represents the time and the second axis represents the trials.

If the quantile argument is specified, the specific quantile for each time period will be returned. Thus, if the 0.75 quantile is specified, it is the 75th quantile for each time period and not the path the 75th quantile in the terminal period took.

Parameters
  • data (ndarray) – Monte carlo simulation data. This must be 3 dimensional with the axis representing time, trial and asset respectively.

  • weights (Union[Iterable[Union[int, float]], ndarray, Series]) – Weights of the portfolio. This must be 1 dimensional and must match the dimension of the data’s last axis.

  • rebalance (bool) – If True, portfolio is assumed to be rebalanced at every step.

  • quantile (Union[int, float, Iterable[Union[int, float]], ndarray, Series, None]) – Quantile or sequence of quantiles to compute, which must be between 0 and 1 inclusive

Returns

The returns path for the portfolio

Return type

ndarray

Examples

>>> from perfana.datasets import load_cube
>>> from perfana.monte_carlo import returns_path
>>> cube = load_cube()[..., :3]
>>> weights = [0.33, 0.34, 0.33]
>>> returns_path(cube, weights).shape
(1000, 81)
>>> returns_path(cube, weights, quantile=0.75).shape  # 75th quantile
(81, )
>>> returns_path(cube, weights, quantile=[0.25, 0.5, 0.75]).shape  # 25th, 50th and 75th quantile
(3, 81)
import matplotlib.pyplot as plt
import numpy as np

from perfana.datasets import load_cube
from perfana.monte_carlo import returns_path

data = load_cube()[..., :7]
weights = [0.25, 0.18, 0.13, 0.11, 0.24, 0.05, 0.04]
quantile = [0.05, 0.25, 0.5, 0.75, 0.95]
color = np.array([
    (200, 200, 200),
    (143, 143, 143),
    (196, 8, 8),
    (143, 143, 143),
    (200, 200, 200),
]) / 255

paths = returns_path(data, weights, quantile=quantile)
n, t = paths.shape
x = np.arange(t)

fig = plt.figure(figsize=(8, 6))
sp = fig.add_subplot(111)
for i in reversed(range(n)):
    label = f"{int(quantile[i] * 100)}"
    sp.plot(x, paths[i], figure=fig, label=label, color=color[i])

sp.fill_between(x, paths[0], paths[-1], color=color[0])
sp.fill_between(x, paths[1], paths[-2], color=color[1])
sp.set_title("Cumulative returns path")
sp.set_xlabel("Time Period")
sp.set_ylabel("Returns")
sp.grid()
sp.legend(title="Percentile")
fig.show()

(Source code)