Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dragon inc #8

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
654 changes: 654 additions & 0 deletions .ipynb_checkpoints/QUBO (3)-checkpoint.ipynb

Large diffs are not rendered by default.

394 changes: 394 additions & 0 deletions .ipynb_checkpoints/deriveZ-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,394 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "39502968-c6bb-4b52-8a6e-bc58fb5d8cc3",
"metadata": {
"tags": []
},
"source": [
"Deriving z."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8203c1f5-f9fc-472b-b06f-4ca4256bc4ae",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_219/316017550.py:4: DeprecationWarning: \n",
"Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),\n",
"(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)\n",
"but was not found to be installed on your system.\n",
"If this would cause problems for you,\n",
"please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466\n",
" \n",
" import pandas as pd\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"VaR at 95% confidence level: -1.9863165e-02 -0.054498\n",
"-1.9305019e-02 -0.052114\n",
"4.3494020e-03 -0.065133\n",
"-6.5832785e-03 -0.073864\n",
"1.1202068e-02 -0.051338\n",
"-1.6676961e-02 -0.080386\n",
"-3.1958164e-03 -0.062213\n",
"-3.6061820e-02 -0.086725\n",
"-6.2434964e-03 -0.068405\n",
"5.3885423e-02 -0.041255\n",
"1.9841270e-02 -0.053425\n",
"5.2570093e-03 -0.048105\n",
"-8.8888889e-03 -0.074956\n",
"-4.9275362e-03 -0.045472\n",
"-2.0058504e-02 -0.061249\n",
"3.0338181e-03 -0.103650\n",
"-2.5770824e-02 -0.057937\n",
"0.0000000e+00 -0.079655\n",
"-1.7283951e-02 -0.096552\n",
"1.1389522e-02 -0.078960\n",
"-1.2942779e-02 -0.049639\n",
"-1.5117830e-02 -0.051520\n",
"8.3017112e-04 -0.040895\n",
"4.6242775e-03 -0.058778\n",
"-2.3952096e-03 -0.069087\n",
"2.4055629e-02 -0.077388\n",
"-1.7333333e-02 -0.073769\n",
"-6.4360418e-03 -0.047025\n",
"1.0585888e-02 -0.116658\n",
"3.3211333e-02 -0.064501\n",
"-2.9184038e-02 -0.035317\n",
"-9.4182825e-03 -0.045576\n",
"dtype: float64\n",
"Z-score for 95% confidence level: 1.6448536269514722\n"
]
}
],
"source": [
"# modifying \"reading in financial data\" to 32 assets and right T value\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"# You may choose to select different parameters/values\n",
"number_assets = 32\n",
"T = 392\n",
"# Read returns\n",
"df = pd.read_csv('returns_data.txt', sep='\\s+')\n",
"\n",
"Rraw = df.values.T\n",
"\n",
"# Select the first N,T assets and scenarios, you may choose a different strategy if you would like to do so.\n",
"R = Rraw[:number_assets,:T]\n",
"\n",
"# Expected return of each asset\n",
"expected_returns = np.mean(R, axis = 1)\n",
"\n",
"# Covariance matrix of asset returns\n",
"covariance_matrix = np.cov(R)\n",
"\n",
"mean_returns = df.mean()\n",
"std_dev_returns = df.std()\n",
"\n",
"# Set the confidence level\n",
"confidence_level = 0.95\n",
"\n",
"# Calculate the z-score for the confidence level\n",
"from scipy.stats import norm\n",
"z_score = norm.ppf(confidence_level)\n",
"\n",
"# Calculate VaR at the 95% confidence level\n",
"VaR_95 = mean_returns - z_score * std_dev_returns\n",
"\n",
"print(f\"VaR at 95% confidence level: {VaR_95}\")\n",
"print(f\"Z-score for 95% confidence level: {z_score}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1454b6b3-e891-4511-8c78-f8396fedce42",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(array([0.00418642, 0.02575848, 0.06693708, 0.06799167, 0.05898317,\n",
" 0.07067119, 0.02501272, 0.01295003, 0.04382892, 0.06962844,\n",
" 0.05880262, 0.02637454, 0.02220247, 0.01063647, 0.01108875,\n",
" 0.00419621, 0.00399815, 0.0395862 , 0.02611385, 0.06111845,\n",
" 0.07534739, 0.01170318, 0.02671506, 0.01058358, 0.04007832,\n",
" 0.00407736, 0.0111234 , 0.01482553, 0.02636917, 0.03315742,\n",
" 0.02972971, 0.00622408]),\n",
" 1.0)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# import random weight function from random_weights.ipynb\n",
"\n",
"def generate_random_weights(number_of_assets):\n",
" weights = np.random.rand(number_of_assets) # Generate random numbers\n",
" weights /= np.sum(weights) # Normalize so that the sum is 1\n",
" return weights\n",
"\n",
"# Test the function with 32 assets\n",
"n_assets = 32\n",
"random_weights = generate_random_weights(n_assets)\n",
"random_weights, sum(random_weights)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "864d0c11-90ac-4fb3-8a06-ddbbf77add41",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Redefining the calculate_historical_VaR function\n",
"def calculate_historical_VaR(weights, mu_R, confidence_level=0.95):\n",
" # Calculate portfolio returns for the given weights\n",
" portfolio_returns = np.dot(mu_R, np.transpose(np.array(weights)))\n",
" # Calculate the VaR at the specified confidence level\n",
" VaR = np.percentile(portfolio_returns, 100 * (1 - confidence_level))\n",
" return -VaR"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d9e7df0d-6d54-41d0-8396-3419e202c41f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Define a function to simulate the random generation of weights and calculation of VaR\n",
"def simulate_VaR(num_simulations, num_assets, mu_R, confidence_level=0.95):\n",
" VaR_results = []\n",
" weights_list = []\n",
"\n",
" for _ in range(num_simulations):\n",
" weights = generate_random_weights(num_assets)\n",
" VaR = calculate_historical_VaR(weights, mu_R, confidence_level)\n",
" VaR_results.append(VaR)\n",
" weights_list.append(weights)\n",
" \n",
" return VaR_results, weights_list"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "74fdb282-cfd1-4ac7-84f1-2a2c0d79139a",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"([0.3128811166148638,\n",
" 0.34202650160735865,\n",
" 0.35334276930136604,\n",
" 0.3410365740045231,\n",
" 0.3165141564733053],\n",
" [array([0.0495565 , 0.04090496, 0.04357528, 0.03138587, 0.04548934,\n",
" 0.04759537, 0.00425981, 0.04478657, 0.03877712, 0.04574071,\n",
" 0.04282936, 0.04846589, 0.04523607, 0.02474504, 0.00173475,\n",
" 0.01009745, 0.04951992, 0.00847554, 0.03235129, 0.02737412,\n",
" 0.02521216, 0.01083173, 0.03016904, 0.0412418 , 0.03148121,\n",
" 0.04565462, 0.00779222, 0.04414392, 0.04107802, 0.00390577,\n",
" 0.03467097, 0.00091758]),\n",
" array([0.01403353, 0.00982509, 0.02215693, 0.04888251, 0.03759727,\n",
" 0.04673139, 0.03929041, 0.02254295, 0.0167434 , 0.03030957,\n",
" 0.03559299, 0.0318543 , 0.02907078, 0.04154272, 0.01493068,\n",
" 0.04450683, 0.04399607, 0.00446831, 0.05255986, 0.03136256,\n",
" 0.05252859, 0.04756222, 0.03254053, 0.01271068, 0.00609097,\n",
" 0.02039448, 0.05474213, 0.04600814, 0.01125323, 0.04347461,\n",
" 0.04532531, 0.00937095]),\n",
" array([0.04276039, 0.05577527, 0.06599738, 0.02122337, 0.01577919,\n",
" 0.01777574, 0.06049104, 0.0060848 , 0.04150497, 0.05034561,\n",
" 0.04371688, 0.01410727, 0.00454085, 0.05396461, 0.03262252,\n",
" 0.05792247, 0.02488471, 0.02395531, 0.04751275, 0.00454294,\n",
" 0.01212546, 0.00017779, 0.02708621, 0.00191693, 0.00403708,\n",
" 0.05340689, 0.0411082 , 0.02861891, 0.06517827, 0.04447769,\n",
" 0.0257816 , 0.01057689]),\n",
" array([0.02446189, 0.04810576, 0.01109954, 0.02208651, 0.04830805,\n",
" 0.06095532, 0.04611498, 0.03494051, 0.02218763, 0.01404973,\n",
" 0.01502475, 0.01276091, 0.00501265, 0.00937838, 0.02563213,\n",
" 0.04173373, 0.03744621, 0.0257746 , 0.01127356, 0.04091599,\n",
" 0.06029149, 0.02897527, 0.06148641, 0.01376761, 0.00746243,\n",
" 0.00780886, 0.03731954, 0.03871881, 0.05245977, 0.05246551,\n",
" 0.04014242, 0.04183903]),\n",
" array([0.05262785, 0.00862903, 0.04958039, 0.02477631, 0.01194494,\n",
" 0.03535696, 0.04781062, 0.05548981, 0.00623476, 0.04606431,\n",
" 0.03779445, 0.05056566, 0.01432316, 0.01568648, 0.04037295,\n",
" 0.03577056, 0.03621072, 0.01843537, 0.0535073 , 0.02829103,\n",
" 0.05666226, 0.03065277, 0.01180278, 0.04231911, 0.03407627,\n",
" 0.00302919, 0.02812648, 0.01991216, 0.05019098, 0.01203927,\n",
" 0.03826156, 0.00345451])])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Example usage\n",
"num_simulations = 1000 # The number of times we want to run the simulation\n",
"num_assets = 32 # Number of assets in the portfolio\n",
"\n",
"# We need the mu_R matrix to run the simulations which should be the historical returns matrix\n",
"# For the sake of the example, let's create a dummy mu_R with random values\n",
"# Normally, you would use actual historical returns data for your assets here\n",
"mu_R = np.random.randn(1000, num_assets) # 1000 scenarios, 32 assets\n",
"\n",
"# Run the simulation\n",
"VaR_results, weights_list = simulate_VaR(num_simulations, num_assets, mu_R)\n",
"\n",
"# Display the first few results to ensure it's working\n",
"VaR_results[:5], weights_list[:5]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "434a4807-8824-48f7-bad9-1b6e8c0c57d9",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The mean VaR at a 95.0% confidence level is: -0.001012471817893799\n",
"The median VaR at a 95.0% confidence level is: -0.001007872904990408\n"
]
}
],
"source": [
"# let's now actually calculate z\n",
"\n",
"# Parameters\n",
"number_of_assets = 32\n",
"number_of_portfolios = 1000 # The number of random portfolios you want to generate\n",
"confidence_level = 0.95\n",
"\n",
"# Assuming mu_R is the matrix of historical returns for the assets\n",
"# Replace this with your actual historical returns data\n",
"mu_R = expected_returns # This should be the historical returns data for your assets\n",
"# expected retursn --> z = .001\n",
"# mu_R = R.T # z = .0045\n",
"# z provided from packages == 1.645\n",
"\n",
"# Generate the random weights for the portfolios and calculate the VaR for each\n",
"VaRs = []\n",
"for _ in range(number_of_portfolios):\n",
" weights = generate_random_weights(number_of_assets)\n",
" VaR = calculate_historical_VaR(weights, mu_R, confidence_level)\n",
" VaRs.append(VaR)\n",
"\n",
"# Calculate z\n",
"mean_VaR = np.mean(VaRs)\n",
"median_VaR = np.median(VaRs)\n",
"z = mean_VaR # or median_VaR, depending on your specific requirements\n",
"\n",
"print(f\"The mean VaR at a {confidence_level*100}% confidence level is: {mean_VaR}\")\n",
"print(f\"The median VaR at a {confidence_level*100}% confidence level is: {median_VaR}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "96b1afa0-a9b9-4f7c-b891-6dbc0eedf3df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The mean VaR at a 95.0% confidence level is: 0.0455241269789427\n",
"The median VaR at a 95.0% confidence level is: 0.045520151841198175\n"
]
}
],
"source": [
"# Parameters\n",
"number_of_assets = 32\n",
"number_of_portfolios = 1000 # The number of random portfolios you want to generate\n",
"confidence_level = 0.95\n",
"\n",
"# Assuming mu_R is the matrix of historical returns for the assets\n",
"# Replace this with your actual historical returns data\n",
"# mu_R = expected_returns # This should be the historical returns data for your assets\n",
"# expected retursn --> z = .001\n",
"mu_R = R.T # z = .0045\n",
"# z provided from packages == 1.645\n",
"\n",
"# Generate the random weights for the portfolios and calculate the VaR for each\n",
"VaRs = []\n",
"for _ in range(number_of_portfolios):\n",
" weights = generate_random_weights(number_of_assets)\n",
" VaR = calculate_historical_VaR(weights, mu_R, confidence_level)\n",
" VaRs.append(VaR)\n",
"\n",
"# Calculate z\n",
"mean_VaR = np.mean(VaRs)\n",
"median_VaR = np.median(VaRs)\n",
"z = mean_VaR # or median_VaR, depending on your specific requirements\n",
"\n",
"print(f\"The mean VaR at a {confidence_level*100}% confidence level is: {mean_VaR}\")\n",
"print(f\"The median VaR at a {confidence_level*100}% confidence level is: {median_VaR}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "297bcd02-43bb-4a1c-a4c6-1ef6a08bbfed",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 [Moody's]",
"language": "python",
"name": "python3_moodys_xbto4j"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading