This is a Python code snippet for portfolio analysis. The code uses various libraries such as pandas
, numpy
, matplotlib
, and yfinance
. It retrieves historical stock data, calculates daily returns, calculates portfolio returns, and calculates the historical Value at Risk (VaR) for the portfolio.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
from random import *
In this section, the necessary libraries are imported for data manipulation, mathematical operations, visualization, and retrieving stock data using the Yahoo Finance API.
TICKERS = {
"AAPL": 0.5,
"MSFT": 0.5
}
The TICKERS dictionary contains the tickers (stock symbols) and their corresponding weights for the portfolio. In this example, the portfolio is equally weighted between Apple (AAPL) and Microsoft (MSFT).
datas = pd.DataFrame()
def get_data(tickers=TICKERS):
global datas
for ticker in tickers.keys():
stock = yf.Ticker(ticker)
data = stock.history(start='2010-01-01', end="2022-12-31")["Close"]
datas[f"{ticker}"] = data
get_data()
The get_data() function retrieves historical stock data for the specified tickers using the Yahoo Finance API. It stores the closing prices of each stock in a pandas DataFrame called datas.
def calculate_daily_returns():
global datas
for ticker in TICKERS:
datas[f"{ticker}_returns"] = datas[f"{ticker}"].pct_change()
calculate_daily_returns()
The calculate_daily_returns() function calculates the daily returns for each stock in the portfolio. It uses the pct_change() function to compute the percentage change in the closing prices and stores the results in new columns in the datas DataFrame.
def calc_portfolio_return():
global datas
datas["portfolio_return"] = 0
for ticker, weight in TICKERS.items():
datas["portfolio_return"] += weight * datas[f"{ticker}_returns"]
calc_portfolio_return()
The calculate_daily_returns() function calculates the daily returns for each stock in the portfolio. It uses the pct_change() function to compute the percentage change in the closing prices and stores the results in new columns in the datas DataFrame.
def calc_portfolio_var(portfolio_return=datas[["portfolio_return"]], alpha=0.95):
var = portfolio_return.squeeze().quantile(1 - alpha)
return var
print(calc_portfolio_var())
The calc_portfolio_var() function calculates the historical Value at Risk (VaR) for the portfolio. It uses the quantile() function to determine the portfolio return value below which the specified percentile (1 - alpha) lies. The result is the VaR at the specified confidence level. Finally, the VaR value is printed along with the datas DataFrame containing the portfolio data.
This code snippet demonstrates a simple implementation of portfolio analysis using historical stock data. It retrieves data, calculates daily returns, computes portfolio returns, and determines the historical VaR for the portfolio. Feel free to modify the code according to your requirements and explore additional analysis techniques.
This is a Python code snippet for simulated Value at Risk (VaR) analysis. The code uses numpy
, pandas
, and matplotlib
libraries. It calculates simulated returns, defines portfolio weights, and calculates the VaR of the portfolio.
The provided code performs the following steps:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from exc_1 import datas
from random import *
print(datas.describe())
expected_return = [datas["AAPL_returns"].mean(), datas["MSFT_returns"].mean()]
volatility = [datas["AAPL_returns"].std(), datas["MSFT_returns"].std()]
correlation = 0.3
The expected return and volatility are calculated based on the mean and standard deviation of daily returns for Apple (AAPL) and Microsoft (MSFT) stocks. The correlation represents the relationship between the two stocks.
weights = [1/volatility[0], 1/volatility[1]]
weights = weights/np.sum(weights)
print(weights)
The portfolio weights are defined inversely proportional to the volatility of each stock. The weights are normalized to ensure their sum is equal to 1. The resulting weights are printed.
def simulated_returns(expected_return=expected_return,
volatility=volatility,
correlation=correlation,
numOfSim=1000):
dZA = np.random.normal(0, 1, size=numOfSim)
dZB = np.random.normal(0, 1, size=numOfSim)
dWA = dZA
dWB = correlation*dZA + np.sqrt(1-correlation**2)*dZB
dln_S_A = (expected_return[0]-volatility[0]**2/2) + volatility[0]*dWA
dln_S_B = (expected_return[1]-volatility[1]**2/2) + volatility[1]*dWB
plt.plot(dln_S_A)
plt.show()
return np.array([dln_S_A, dln_S_B])
The simulated_returns() function simulates the logarithmic returns of the two stocks based on the provided expected return, volatility, correlation, and number of simulations. It generates random values (dZA and dZB) from a normal distribution, and then calculates the logarithmic returns (dln_S_A and dln_S_B) using the provided formulas. The resulting simulated returns are plotted for the first stock (dln_S_A).
def calc_portfolio_var(weights=weights, alpha=0.05):
simulated_returns_ = simulated_returns()
portfolio_returns = np.matmul(weights, simulated_returns_)
var = np.quantile(portfolio_returns, alpha)
return var
This code snippet demonstrates how to calculate the Value at Risk (VaR) for the portfolio using simulated returns. The calc_portfolio_var() function takes the portfolio weights and the desired confidence level (alpha) as inputs. It calls the simulated_returns() function to obtain an array of simulated returns. The portfolio returns are calculated by multiplying the weights with the simulated returns using np.matmul(). Finally, the VaR is computed using the np.quantile() function, specifying the desired confidence level (alpha).
This code snippet demonstrates how to perform simulated Value at Risk (VaR) analysis for a portfolio. It uses simulated returns based on expected returns, volatilities, and correlation between assets. The portfolio weights are defined based on asset volatility, and the VaR is calculated using the simulated returns and portfolio weights.
We begin by importing the necessary libraries for our analysis: pandas, matplotlib.pyplot, yfinance, sys, and seaborn.
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
import sys
import seaborn as sns
TICKER = "VOO"
data = pd.DataFrame()
def get_data():
global data
stock = yf.Ticker(TICKER)
close = stock.history(start='2010-01-01', end="2022-12-31")["Close"]
data[f"{TICKER}"] = close
We define a function get_data() to download the historical price data using the yfinance library. It retrieves the closing prices for the specified ETF from January 1, 2010, to December 31, 2022, and stores them in the data DataFrame under the column with the ticker symbol.
Next, we define a function calculate_daily_returns() to calculate the daily returns of the ETF. It adds a new column to the data DataFrame called {TICKER}_returns, which represents the percentage change in the closing prices.
def calculate_daily_returns():
global data
data[f"{TICKER}_returns"] = data[f"{TICKER}"].pct_change()
We call the get_data() and calculate_daily_returns() functions to download the data and calculate the daily returns.
Finally, we drop any rows with missing values (NaN) from the data DataFrame.
get_data()
calculate_daily_returns()
data = data.dropna()
We define a function calculate_ewma_variance() to calculate the exponentially weighted moving average (EWMA) variance of the ETF returns. The function takes parameters df_etf_returns (defaulting to the data DataFrame), decay_factor (defaulting to 0.94), and window (defaulting to 100).
def calculate_ewma_variance(df_etf_returns=data[[f"{TICKER}_returns"]],
decay_factor=0.94,
window=100):
ewma = df_etf_returns.ewm(alpha=1-decay_factor,
min_periods=window,
adjust=False).var()
return ewma
Next, we define a function plot_ewma_vars_sea() to plot the EWMA variances using seaborn. It calls the calculate_ewma_variance() function twice with different decay factors to obtain two sets of variances. It then uses seaborn and matplotlib to create a line plot of the EWMA variances.
def plot_ewma_vars_sea():
ewma_var_094 = calculate_ewma_variance(decay_factor=0.94)
ewma_var_097 = calculate_ewma_variance(decay_factor=0.97)
sns.set(style='darkgrid') # Set the seaborn style
plt.figure(figsize=(15, 10))
plt.plot(ewma_var_094, label='EWMA Variance (Decay Factor = 0.94)')
plt.plot(ewma_var_097, label='EWMA Variance (Decay Factor = 0.97)')
plt.xlabel('Date')
plt.ylabel('Variance')
plt.title('EWMA Variance of ETF Returns')
plt.legend()
plt.show()
if __name__ == "__main__":
plot_ewma_vars_sea()
We begin by importing the necessary libraries for our analysis: pandas, matplotlib.pyplot, sklearn.model_selection, sklearn.linear_model, and seaborn.
from exc_3 import data, TICKER
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score, TimeSeriesSplit
from sklearn.linear_model import LinearRegression
import seaborn as sns
We define a function create_lagged_squared_returns() to create lagged squared returns for the specified ETF. It adds columns to the data DataFrame representing squared returns at different lags.
def create_lagged_squared_returns():
global data
lags = 20
for i in range(1, lags + 1):
data[f'Lagged Squared Returns {i}'] = data[f"{TICKER}_squared_returns"].shift(i)
data = data.dropna()
create_lagged_squared_returns()
Next, we define the dependent variable y and the independent variables X for our analysis. X is created by dropping the columns "VOO", "VOO_returns", and "VOO_squared_returns" from the data DataFrame.
X = data.drop(["VOO", "VOO_returns", "VOO_squared_returns"], axis=1)
y = data["VOO_squared_returns"]
We initialize a linear regression model model and a time series cross-validator tscv with 20 splits.
model = LinearRegression()
tscv = TimeSeriesSplit(n_splits=20)
We use cross-validation to evaluate the model's performance. We calculate the negative mean squared error scores for the linear regression model using the cross_val_score() function.
scores = -cross_val_score(model, X, y, scoring='neg_mean_squared_error', cv=tscv)
Next, we fit the linear regression model to the entire dataset using the fit() method. We predict the variance for the entire dataset using the trained model.
model.fit(X, y)
y_pred_all = model.predict(X)
Finally, we define a function plot_predicted_vs_actual_variance2() to plot the predicted variance and actual variance over time. It uses seaborn and matplotlib to create a line plot.
def plot_predicted_vs_actual_variance2(data, TICKER, y_pred_all):
plt.plot(data.index, data[f"{TICKER}_squared_returns"], label='Actual Variance')
plt.plot(data.index, y_pred_all, label='Predicted Variance')
plt.xlabel('Time')
plt.ylabel('Variance')
plt.title('Predicted vs Actual Variance')
plt.legend()
plt.show()
We call the plot_predicted_vs_actual_variance2() function to generate the plot.
plot_predicted_vs_actual_variance2(data=data, TICKER=TICKER, y_pred_all=y_pred_all)