Monte Carlo
The Monte Carlo method is implemented in finquant.monte_carlo
.
The module provides a class MonteCarlo
which is an implementation of the
Monte Carlo method and a class MonteCarloOpt
which allows the user to perform a
Monte Carlo run to find optimised financial portfolios, given an initial portfolio.
- class finquant.monte_carlo.MonteCarlo(num_trials=1000)
An object to perform a Monte Carlo run/simulation.
- __init__(num_trials=1000)
- Parameters:
num_trials (
int
) – Number of iterations of the Monte Carlo run/simulation, default: 1000
- run(fun, **kwargs)
- Parameters:
fun (
Callable
[...
,Any
]) – Function to call at each iteration of the Monte Carlo run.kwargs (
Dict
[str
,Any
]) – (optional) Additional arguments that are passed tofun
.
- Result:
Array of quantities returned from
fun
at each iteration.- Return type:
ndarray
[float64
,Any
]
- class finquant.monte_carlo.MonteCarloOpt(returns, num_trials=1000, risk_free_rate=0.005, freq=252, initial_weights=None)
An object to perform a Monte Carlo run/simulation for finding optimised financial portfolios.
Inherits from MonteCarlo.
- __init__(returns, num_trials=1000, risk_free_rate=0.005, freq=252, initial_weights=None)
- Parameters:
returns (
DataFrame
) – DataFrame of returns of stocks Note: If applicable, the given returns should be computed with the same risk free rate and time window/frequency (argumentsrisk_free_rate
andfreq
as passed in here.num_trials (
int
) – Number of portfolios to be computed, each with a random distribution of weights/allocation in each stock, default: 1000risk_free_rate (
Union
[floating
,float
]) – Risk free rate as required for the Sharpe Ratio, default: 0.005freq (
Union
[integer
,int
]) – Number of trading days in a year, default: 252initial_weights (
Optional
[ndarray
[float64
,Any
]]) – Weights of initial/given portfolio, only used to plot a marker for the initial portfolio in the optimisation plot, default:None
- optimisation()
Optimisation of the portfolio by performing a Monte Carlo simulation.
- Return type:
Tuple
[DataFrame
,DataFrame
]- Returns:
- opt_w:
DataFrame with optimised investment strategies for maximum Sharpe Ratio and minimum volatility.
- opt_res:
DataFrame with Expected Return, Volatility and Sharpe Ratio for portfolios with minimum Volatility and maximum Sharpe Ratio.
- plot_results()
Plots the results of the Monte Carlo run, with all of the randomly generated weights/portfolios, as well as markers for the portfolios with the minimum Volatility and maximum Sharpe Ratio.
- Return type:
None
- properties()
Prints out the properties of the Monte Carlo optimisation.
- Return type:
None