### Install riskparityportfolio via pip Source: https://mirca.github.io/riskparity.py/index.html Standard installation command for the package. ```bash $ pip install riskparityportfolio ``` -------------------------------- ### Install development version from source Source: https://mirca.github.io/riskparity.py/index.html Commands to clone the repository and install the package in editable mode. ```bash $ git clone https://github.com/dppalomar/riskparity.py $ cd riskparity.py $ pip install -e . ``` -------------------------------- ### Import riskparityportfolio Library Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Import the necessary library to start using its functionalities. ```python import riskparityportfolio as rp ``` -------------------------------- ### Get optimal weights from pyrb Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Retrieves the calculated optimal weights from the ERC object after solving. ```python optimal_weights = ERC.x optimal_weights ``` -------------------------------- ### Get scaled risk contributions from pyrb Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Retrieves the scaled risk contributions for each asset from the ERC object. ```python risk_contributions_scaled = ERC.get_risk_contributions() risk_contributions_scaled ``` -------------------------------- ### Initialize `RiskParityPortfolio` Class Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Instantiate the `RiskParityPortfolio` class with the covariance matrix and budgeting vector. This approach is more flexible for advanced constraints. ```python my_portfolio = rp.RiskParityPortfolio(covariance=Sigma, budget=b) ``` -------------------------------- ### Initialize and Design Risk Parity Portfolio Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Initializes the RiskParityPortfolio object and runs the design optimization. ```python my_rpp = rpp.RiskParityPortfolio(covariance=cov) ``` ```python my_rpp.design() ``` -------------------------------- ### Design Portfolio using `vanilla.design` function Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Use the `rp.vanilla.design` function to compute portfolio weights based on the covariance matrix and budgeting vector. This is suitable for vanilla risk parity problems. ```python w = rp.vanilla.design(Sigma, b) ``` -------------------------------- ### Import Risk Parity Portfolio Library Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Imports the core RiskParityPortfolio class and numpy for numerical computations. ```python import riskparityportfolio as rp import numpy as np ``` -------------------------------- ### Initialize EqualRiskContribution object Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Initializes the EqualRiskContribution solver from pyrb with the covariance matrix. ```python ERC = EqualRiskContribution(cov) ``` -------------------------------- ### Display Portfolio Weights Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Display the calculated portfolio weights. ```python w ``` -------------------------------- ### Calculate optimal weights with riskparityportfolio Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Calculates the optimal weights using the vanilla design function from the riskparityportfolio package. ```python rpp.vanilla.design(cov, b) ``` -------------------------------- ### Import necessary libraries Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Imports pandas, numpy, and the EqualRiskContribution class from pyrb. ```python from pyrb import EqualRiskContribution import pandas as pd import numpy as np import riskparityportfolio as rpp ``` -------------------------------- ### Design Portfolio using Class Method Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Call the `design` method on the `RiskParityPortfolio` instance to compute the portfolio weights. This method may involve iterative calculations. ```python my_portfolio.design() ``` -------------------------------- ### Verify Constraints Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Checks if the portfolio weights satisfy the defined inequality constraints. ```python Dmat @ my_rpp.weights ``` -------------------------------- ### Display Budgeting Vector Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Display the defined budgeting vector to verify its contents. ```python b ``` -------------------------------- ### Verify Risk Contributions Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Calculate and display the actual risk contributions of the portfolio weights to verify they match the desired budgeting vector. ```python rc = w @ (Sigma * w) rc/np.sum(rc) ``` -------------------------------- ### Optimize Portfolio with Mean Return Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Iterates through different alpha values to add mean return to the portfolio, designs the portfolio, and collects risk concentration and mean return data. ```python risk_parity = [] mean_return = [] for alpha in 10 ** np.arange(-5, 0, .25): my_portfolio.add_mean_return(alpha=alpha, mean=mu) my_portfolio.design() risk_parity.append(my_portfolio.risk_concentration.evaluate()) mean_return.append(my_portfolio.mean_return) ``` -------------------------------- ### Apply Inequality Constraints Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Defines inequality constraint matrices and applies them to the portfolio design. ```python # inequality constraints matrix and vector Dmat = np.array([[0,0,0,0,-1,-1,-1,-1], [1,-1,0,0,1,-1,0,0]]) dvec = np.array([-0.3,-0.05]) ``` ```python my_rpp.design(Dmat=Dmat, dvec=dvec) ``` -------------------------------- ### Visualize Risk-Volatility Trade-off Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Plot the relationship between risk parity metrics and portfolio volatility. ```python plt.plot(risk_parity, volatility, 'ko') plt.plot(risk_parity, volatility, 'k-') plt.ylabel("volatility") plt.xlabel("risk parity") ``` -------------------------------- ### Access Portfolio Results Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Retrieves the calculated weights and risk contributions of the portfolio. ```python my_rpp.weights ``` ```python my_rpp.risk_contributions ``` -------------------------------- ### Access Portfolio Weights from Class Instance Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Retrieve the computed portfolio weights from the `weights` attribute of the `RiskParityPortfolio` instance. ```python my_portfolio.weights ``` -------------------------------- ### Define target weights for riskparityportfolio Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Creates a NumPy array representing the target weights (equal allocation) for the risk parity portfolio calculation. ```python b = np.ones(len(cov)) / len(cov) ``` -------------------------------- ### Define Budget Vector Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Defines the budget vector for the assets. ```python b = np.array((0.1594, 0.0126, 0.8280)) ``` ```python b ``` ```python array([0.1594, 0.0126, 0.828 ]) ``` -------------------------------- ### Import Libraries and Configure Plotting Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Imports necessary libraries for plotting and numerical operations, and configures matplotlib for high-resolution output. ```python %matplotlib inline ``` ```python import matplotlib.pyplot as plt %config InlineBackend.figure_format = "retina" from matplotlib import rcParams rcParams["savefig.dpi"] = 100 rcParams["figure.dpi"] = 100 rcParams["font.size"] = 14 ``` -------------------------------- ### Time riskparityportfolio design function Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Measures the execution time of the rpp.vanilla.design() function using %timeit. ```python %timeit rpp.vanilla.design(cov, b) ``` -------------------------------- ### Time EqualRiskContribution solver Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Measures the execution time of the ERC.solve() method using %timeit. ```python %timeit ERC.solve() ``` -------------------------------- ### Optimize Portfolio with Variance Penalty Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Iterate through lambda values to add variance to the objective function and design the portfolio for each step. ```python risk_parity = [] volatility = [] for lmd in 10 ** np.arange(-5, 0, .25): my_portfolio.add_variance(lmd=lmd) my_portfolio.design() risk_parity.append(my_portfolio.risk_concentration.evaluate()) volatility.append(my_portfolio.volatility) ``` -------------------------------- ### Import NumPy Library Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Import the NumPy library for numerical operations, especially for handling arrays and matrices. ```python import numpy as np ``` -------------------------------- ### Plot Risk Parity vs. Mean Return Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Plots the relationship between risk concentration and mean return for the optimized portfolios. ```python plt.plot(risk_parity, mean_return, 'ko') plt.plot(risk_parity, mean_return, 'k-') plt.ylabel("mean return") plt.xlabel("risk parity") ``` ```python Text(0.5, 0, 'risk parity') ``` -------------------------------- ### Load and process covariance matrix Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Loads a covariance matrix from a CSV file and calculates the annualized covariance. Ensure the data.csv file is accessible. ```python covariance_matrix = pd.read_csv("https://raw.githubusercontent.com/jcrichard/pyrb/master/notebooks/data.csv",sep=";",index_col=0).pct_change().cov() * 260 ``` -------------------------------- ### Display Plot Label Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Output of the matplotlib xlabel command. ```python Text(0.5, 0, 'risk parity') ``` -------------------------------- ### Display Covariance Matrix Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Display the defined covariance matrix to verify its contents. ```python Sigma ``` -------------------------------- ### Define Mean Return Vector Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Defines the mean return vector for the assets. ```python mu = np.array([0.1837, 0.3465, 0.5210]) ``` ```python mu ``` ```python array([0.1837, 0.3465, 0.521 ]) ``` -------------------------------- ### Display covariance matrix Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Prints the calculated covariance matrix. ```python covariance_matrix ``` -------------------------------- ### Access Risk Contributions from Class Instance Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Retrieve the calculated risk contributions from the `risk_contributions` attribute of the `RiskParityPortfolio` instance. ```python my_portfolio.risk_contributions ``` -------------------------------- ### Define Budgeting Vector Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Define the budgeting vector (b) representing the desired relative risk contributions for each asset. ```python b = np.array((0.1594, 0.0126, 0.8280)) ``` -------------------------------- ### Define Covariance Matrix Source: https://mirca.github.io/riskparity.py/tutorials/minimal-usage.html Define the covariance matrix (Sigma) for the assets in the portfolio using NumPy arrays. ```python Sigma = np.vstack((np.array((1.0000, 0.0015, -0.0119)), np.array((0.0015, 1.0000, -0.0308)), np.array((-0.0119, -0.0308, 1.0000)))) ``` -------------------------------- ### Define Covariance Matrix Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Calculates a covariance matrix from volatility and correlation inputs. ```python vol = [0.05,0.05,0.07,0.1,0.15,0.15,0.15,0.18] cor = np.array([[100, 80, 60, -20, -10, -20, -20, -20], [ 80, 100, 40, -20, -20, -10, -20, -20], [ 60, 40, 100, 50, 30, 20, 20, 30], [-20, -20, 50, 100, 60, 60, 50, 60], [-10, -20, 30, 60, 100, 90, 70, 70], [-20, -10, 20, 60, 90, 100, 60, 70], [-20, -20, 20, 50, 70, 60, 100, 70], [-20, -20, 30, 60, 70, 70, 70, 100]])/100 cov = np.outer(vol,vol)*cor ``` -------------------------------- ### Define Covariance Matrix Source: https://mirca.github.io/riskparity.py/tutorials/including-mean-return-and-variance.html Defines the covariance matrix for three assets using numpy arrays. ```python Sigma = np.vstack((np.array((1.0000, 0.0015, -0.0119)), np.array((0.0015, 1.0000, -0.0308)), np.array((-0.0119, -0.0308, 1.0000)))) ``` ```python Sigma ``` ```python array([[ 1. , 0.0015, -0.0119], [ 0.0015, 1. , -0.0308], [-0.0119, -0.0308, 1. ]]) ``` -------------------------------- ### Convert covariance matrix to NumPy array Source: https://mirca.github.io/riskparity.py/tutorials/comparison-with-pyrb.html Converts the pandas DataFrame covariance matrix into a NumPy array for numerical operations. ```python cov = np.asarray(covariance_matrix) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.