Created
December 28, 2022 00:06
-
-
Save EvanMarie/91cefea0c4cd80f706f71a43111d6e28 to your computer and use it in GitHub Desktop.
portfolio_returns_tracking_errors.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyOeFTZ+I7p9f9kxaZrQbqZy", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/EvanMarie/91cefea0c4cd80f706f71a43111d6e28/portfolio_returns_tracking_errors.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "3Pbhahof_7st" | |
}, | |
"outputs": [], | |
"source": [ | |
"def portfolio_returns(weights, portfolio):\n", | |
" return portfolio.dot(weights)\n", | |
"\n", | |
"\n", | |
"def tracking_error(weights, portfolio, index_data):\n", | |
" result = portfolio_returns(weights, portfolio).sub(index_data).std() * np.sqrt(250)\n", | |
" colorprint(f'The annualized tracking error for the portfolio is {(result*100):.2f}%', fontsize = 3)\n", | |
" return result\n", | |
" \n", | |
" \n", | |
"def tracking_error_general(data, portfolio_list, weights, \n", | |
" index, start, end, title = None,\n", | |
" printout=True):\n", | |
" \n", | |
" results = data.loc[start:end, portfolio_list].dot(weights).\\\n", | |
" sub(data.loc[start:end, index]).std() * np.sqrt(250)\n", | |
" if printout is True:\n", | |
" highlight(title, 'yellow', 'black', 2)\n", | |
" highlight(f'Period: {start} to {end}', 'yellow', 'black', 2)\n", | |
" highlight(f'Overall tracking error: {(results *100): .2f}%', 'yellow', 'black', 2)\n", | |
" else:\n", | |
" return results" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment