{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "867bfa18", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.core.display import display, HTML\n", "display(HTML(\"\"))" ] }, { "cell_type": "code", "execution_count": 2, "id": "419ed573", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/bz/5cj1tzbj5xn319rpqrw6gpmh0000gn/T/ipykernel_9819/1974911599.py:17: FutureWarning: The default value of regex will change from True to False in a future version. In addition, single character regular expressions will *not* be treated as literal strings when regex=True.\n", " x_rates.columns = x_rates.columns.str.replace(\"[\",\"\").str.replace(\"]\",\"\")\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
dateaustralian_dollarbulgarian_levbrazilian_realcanadian_dollarswiss_francchinese_yuancypriot_poundczech_korunadanish_kroneestonian_kroonuk_poundgreek_drachmahong_kong_dollarcroatian_kunahungarian_forintindonesian_rupiahisraeli_shekelindian_rupeeiceland_kronajapanese_yenkorean_wonlithuanian_litaslatvian_latsmaltese_liramexican_pesomalaysian_ringgitnorwegian_kronenew_zealand_dollarphilippine_pesopolish_zlotyromanian_leurussian_roubleswedish_kronasingapore_dollarslovenian_tolarslovak_korunathai_bahtturkish_liraus_dollarsouth_african_rand
01999-01-041.9100NaNNaN1.80041.6168NaN0.582335.10707.450115.64660.7111327.15009.1332NaN251.48009,433.6100NaNNaN81.4800133.73001,398.59004.71700.66680.443211.64464.47988.85502.222945.51004.07121.311125.28759.46961.9554189.045042.991042.67990.37231.17896.9358
11999-01-051.8944NaNNaN1.79651.6123NaN0.582334.91707.449515.64660.7122324.70009.1341NaN250.80009,314.5100NaNNaN81.5300130.96001,373.01004.71740.66570.443211.59604.48058.77452.201144.74504.02451.316826.58769.40251.9655188.775042.848042.50480.37281.17906.7975
\n", "
" ], "text/plain": [ " date australian_dollar bulgarian_lev brazilian_real canadian_dollar swiss_franc chinese_yuan cypriot_pound czech_koruna danish_krone estonian_kroon uk_pound greek_drachma hong_kong_dollar croatian_kuna hungarian_forint indonesian_rupiah israeli_shekel indian_rupee iceland_krona japanese_yen korean_won lithuanian_litas latvian_lats maltese_lira mexican_peso malaysian_ringgit norwegian_krone new_zealand_dollar philippine_peso polish_zloty romanian_leu russian_rouble swedish_krona singapore_dollar slovenian_tolar slovak_koruna thai_baht turkish_lira us_dollar south_african_rand \n", "0 1999-01-04 1.9100 NaN NaN 1.8004 1.6168 NaN 0.5823 35.1070 7.4501 15.6466 0.7111 327.1500 9.1332 NaN 251.4800 9,433.6100 NaN NaN 81.4800 133.7300 1,398.5900 4.7170 0.6668 0.4432 11.6446 4.4798 8.8550 2.2229 45.5100 4.0712 1.3111 25.2875 9.4696 1.9554 189.0450 42.9910 42.6799 0.3723 1.1789 6.9358\n", "1 1999-01-05 1.8944 NaN NaN 1.7965 1.6123 NaN 0.5823 34.9170 7.4495 15.6466 0.7122 324.7000 9.1341 NaN 250.8000 9,314.5100 NaN NaN 81.5300 130.9600 1,373.0100 4.7174 0.6657 0.4432 11.5960 4.4805 8.7745 2.2011 44.7450 4.0245 1.3168 26.5876 9.4025 1.9655 188.7750 42.8480 42.5048 0.3728 1.1790 6.7975" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
dateaustralian_dollarbulgarian_levbrazilian_realcanadian_dollarswiss_francchinese_yuancypriot_poundczech_korunadanish_kroneestonian_kroonuk_poundgreek_drachmahong_kong_dollarcroatian_kunahungarian_forintindonesian_rupiahisraeli_shekelindian_rupeeiceland_kronajapanese_yenkorean_wonlithuanian_litaslatvian_latsmaltese_liramexican_pesomalaysian_ringgitnorwegian_kronenew_zealand_dollarphilippine_pesopolish_zlotyromanian_leurussian_roubleswedish_kronasingapore_dollarslovenian_tolarslovak_korunathai_bahtturkish_liraus_dollarsouth_african_rand
56972021-01-071.58361.95586.51721.56011.08337.9392NaN26.14707.4392NaN0.9019NaN9.51767.5660357.790017,259.99003.902790.0455155.3000127.13001,342.2900NaNNaNNaN24.25524.957010.34351.690759.04304.49984.871291.200010.05751.6253NaNNaN36.85908.99871.227618.7919
56982021-01-081.57581.95586.57481.55431.08277.9184NaN26.16307.4369NaN0.9013NaN9.49827.5690359.620017,247.33003.898189.7975155.5000127.26001,337.9000NaNNaNNaN24.47184.935910.28631.688358.94704.51134.870890.800010.05101.6228NaNNaN36.84809.01461.225018.7212
\n", "
" ], "text/plain": [ " date australian_dollar bulgarian_lev brazilian_real canadian_dollar swiss_franc chinese_yuan cypriot_pound czech_koruna danish_krone estonian_kroon uk_pound greek_drachma hong_kong_dollar croatian_kuna hungarian_forint indonesian_rupiah israeli_shekel indian_rupee iceland_krona japanese_yen korean_won lithuanian_litas latvian_lats maltese_lira mexican_peso malaysian_ringgit norwegian_krone new_zealand_dollar philippine_peso polish_zloty romanian_leu russian_rouble swedish_krona singapore_dollar slovenian_tolar slovak_koruna thai_baht turkish_lira us_dollar south_african_rand \n", "5697 2021-01-07 1.5836 1.9558 6.5172 1.5601 1.0833 7.9392 NaN 26.1470 7.4392 NaN 0.9019 NaN 9.5176 7.5660 357.7900 17,259.9900 3.9027 90.0455 155.3000 127.1300 1,342.2900 NaN NaN NaN 24.2552 4.9570 10.3435 1.6907 59.0430 4.4998 4.8712 91.2000 10.0575 1.6253 NaN NaN 36.8590 8.9987 1.2276 18.7919\n", "5698 2021-01-08 1.5758 1.9558 6.5748 1.5543 1.0827 7.9184 NaN 26.1630 7.4369 NaN 0.9013 NaN 9.4982 7.5690 359.6200 17,247.3300 3.8981 89.7975 155.5000 127.2600 1,337.9000 NaN NaN NaN 24.4718 4.9359 10.2863 1.6883 58.9470 4.5113 4.8708 90.8000 10.0510 1.6228 NaN NaN 36.8480 9.0146 1.2250 18.7212" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "import pprint\n", "\n", "pd.options.display.float_format = '{:20,.4f}'.format\n", "pd.set_option('display.max_rows', None)\n", "pd.set_option('display.max_columns', None)\n", "pd.set_option('display.width', 1000)\n", "pd.set_option('display.colheader_justify', 'center')\n", "pd.set_option('display.precision', 3)\n", "\n", "# open file\n", "x_rates = pd.read_csv(\"euro-daily-hist_1999_2020.csv\")\n", "\n", "# strip brackets and trailing space from country names\n", "# replace remaining space with underscore\n", "# lower case all column names\n", "x_rates.columns = x_rates.columns.str.replace(\"[\",\"\").str.replace(\"]\",\"\")\n", "x_rates.columns = x_rates.columns.str.rstrip()\n", "x_rates.columns = x_rates.columns.str.replace(\" \",\"_\")\n", "x_rates.columns = x_rates.columns.str.lower()\n", "\n", "# rename columns\n", "x_rates.rename(columns={\"period\\\\unit:\":\"date\",\n", " \"chinese_yuan_renminbi\":\"chinese_yuan\",\n", " \"uk_pound_sterling\":\"uk_pound\"}, inplace=True)\n", "\n", "# convert datetime\n", "x_rates[\"date\"] = pd.to_datetime(x_rates[\"date\"])\n", "\n", "# resort and reindex\n", "x_rates.sort_values(\"date\", inplace=True)\n", "x_rates.reset_index(drop=True, inplace=True)\n", "\n", "# convert hyphens in currency columns to NaN\n", "import numpy as np\n", "x_rates = x_rates.replace(\"-\", np.nan)\n", "\n", "# convert exchange rate values to float\n", "x_rates.iloc[:,1:] = x_rates.iloc[:,1:].astype(float)\n", "\n", "display(x_rates.head(2))\n", "display(x_rates.tail(2))" ] }, { "cell_type": "code", "execution_count": 3, "id": "e57164fa", "metadata": {}, "outputs": [], "source": [ "# miniaturize x_rates\n", "x_rates = x_rates.iloc[:,:6]\n", "\n", "# array with names of currencies\n", "currency_names = x_rates.columns.values\n", "currency_names = currency_names[1:]\n", "\n", "# create a list & dictionary of data frames for each currency with log rate of the exchange rate, 30 day rolling mean, and year\n", "col_names = [] # list with names of currencies\n", "df_list = [] # list of 40 dataframes\n", "df_dict = {} # dictionary with 40 currency:currency_dataframe pairs\n", "for currency in x_rates.columns[1:]:\n", " df_name = currency\n", " col_names.append(df_name)\n", " df = x_rates[[\"date\", currency]].copy()\n", " df = df[df[currency].notna()]\n", " df[\"log_rate\"] = np.log(df.iloc[:,1]/df.iloc[:,1].shift()) # getting the log of the exchange rate # double check this is the correct way to get log\n", " df[\"rolling_mean_30\"] = df[currency].rolling(30).mean()\n", " df[\"year\"] = df[\"date\"].dt.year\n", " df_dict[currency] = df\n", " df_list.append(df)" ] }, { "cell_type": "code", "execution_count": 4, "id": "27713ddc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bulgarian_lev_annual_vol
20000.0098
20010.0058
20020.0075
20030.0047
20040.0051
20050.0097
20060.0004
20070.0001
20080.0000
20090.0000
20100.0001
20110.0000
20120.0000
20130.0001
20140.0000
20150.0001
20160.0000
20170.0000
20180.0000
20190.0000
20200.0000
20210.0000
\n", "
" ], "text/plain": [ " bulgarian_lev_annual_vol\n", "2000 0.0098 \n", "2001 0.0058 \n", "2002 0.0075 \n", "2003 0.0047 \n", "2004 0.0051 \n", "2005 0.0097 \n", "2006 0.0004 \n", "2007 0.0001 \n", "2008 0.0000 \n", "2009 0.0000 \n", "2010 0.0001 \n", "2011 0.0000 \n", "2012 0.0000 \n", "2013 0.0001 \n", "2014 0.0000 \n", "2015 0.0001 \n", "2016 0.0000 \n", "2017 0.0000 \n", "2018 0.0000 \n", "2019 0.0000 \n", "2020 0.0000 \n", "2021 0.0000 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# function to create df with year and annual volatility for every row # i think i could use aggregation here but don't know how\n", "def volatizer(currency):\n", " annual_df_list = [currency[currency['year'] == y] for y in currency['year'].unique()] # list of annual dfs\n", " c_name = currency.columns[1]\n", " row_dict = {}\n", " for frame in annual_df_list:\n", " year_name = frame.iat[0,4] # first cell of the \"year\" column, becomes the \"year\" key for row_dict\n", " annual_volatility = frame[\"log_rate\"].std()*253**.5 # volatility measured by standard deviation * 253 trading days per year raised to the 0.5 power\n", " row_dict[year_name] = annual_volatility\n", " df = pd.DataFrame.from_dict(row_dict, orient=\"index\", columns=[c_name+\"_annual_vol\"]) # indexing on year, not sure if this is cool\n", " return df\n", "\n", "# apply volatizer to each currency df\n", "for key in df_dict:\n", " df_dict[key] = volatizer(df_dict[key])\n", "\n", "display(df_dict[\"bulgarian_lev\"])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }