Questions attached in PDF file.
{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "run\n" ] } ], "source": [ "import numpy as np\n", "import scipy as sp\n", "import scipy.stats as sps\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import sys\n", "import time\n", "\n", "\n", "%matplotlib inline\n", "\n", "starttime = time.time()\n", "\n", "print('run')\n", "data = pd.read_excel(\"Country Indices.xlsx\", index_col = 0, sheet_name= 1, skiprows = [1,2])\n", "\n", "countriesls = pd.read_excel(\"Country Indices.xlsx\",index_col=None , sheet_name= 1, nrows = 0)\n", "firstcol = pd.read_excel(\"Country Indices.xlsx\",index_col= None, sheet_name = 1, usecols = \"A\")\n", "\n", "\n", "\n", "dates_list = []\n", "for i in range(len(firstcol)):\n", " dates_list.append( str(firstcol.iloc[i][0] ) [0:10]) \n", "\n", "# Create a list with the dates. Dates are in form YYYY-MM-DD\n", "dates_list = dates_list[2:-1]\n", "\n", " \n", "#country_list = []\n", "# for i in countriesls:\n", "# country_list.append(i)\n", " \n", "#del country_list[0]\n", "\n", "# Create list of countries\n", "countries = ['Italy', 'US', 'Australia', 'UK ', \\\n", " 'Brazil', 'Japan', 'Russia', 'China', 'France', 'Canada', \\\n", " 'Korea', 'Switzerland', 'Spain', 'India', 'Mexico', 'Germany', \\\n", " 'Indonesia', 'Netherlands', 'Saudi Arabia', 'Turkey']\n", "\n", "\n", "\n", "\n", "\n", "### Question 1\n", "### (ii)\n", "\n", "t_start_ind = dates_list.index('2007-03-01')\n", "t_end_ind = dates_list.index('2020-08-31')\n", "\n", "\n", "\n", "\n", "# Rebase the indices at 2007-01-03 to 100. Change all values until 2020-08-31 \n", "# The rebased value is given by 100*value_j/value_1\n", "# Where value_j is the index at the jth date and value_1 is the index at the initial date\n", "\n", "# Initialise empty arrays to store country index as a list in a list\n", "initial_ind = []\n", "new_vals = []\n", "\n", "for j in range(len(countries)):\n", " initial_ind . append ( data.iloc[t_start_ind][j]) # Get starting value at initial date\n", " \n", " newvals = []\n", " for i in range(t_start_ind,t_end_ind):\n", " data.iloc[i][j] = 100* data.iloc[i][j] /initial_ind[j]\n", " newvals.append(data.iloc[i][j])\n", " new_vals.append(newvals)\n", "\n", "\n", " \n", "\n", "# Plot the data using different colours\n", "# GFC blue\n", "# GFC peak yellow\n", "# INTERIM green\n", "# COVID-19 orange\n", "# COVID-19 peak red\n", "\n", "\n", "## Time periods\n", "GFC_start = '2007-03-01'\n", "GFC_end = '2010-05-31'\n", "GFC_peak_start = '2008-09-02'\n", "GFC_peak_end = '2009-06-01'\n", "COVID_start = '2020-03-11'\n", "COVID_end = '2020-08-31'\n", "COVID_peak_start = '2020-03-11'\n", "COVID_peak_end = '2020-05-29'\n", "INTERIM_start = '2010-06-01'\n", "INTERIM_end = '2020-03-10'\n", "\n", "# Get the indices\n", "GFC_start_ind = dates_list.index('2007-01-03') \n", "GFC_end_ind = dates_list.index('2010-05-31') \n", "GFC_peak_start_ind = dates_list.index('2008-09-02') \n", "GFC_peak_end_ind = dates_list.index('2009-06-01') \n", "COVID_start_ind = dates_list.index('2020-03-11') \n", "COVID_end_ind = dates_list.index('2020-08-31') \n", "COVID_peak_start_ind = dates_list.index('2020-03-11') \n", "COVID_peak_end_ind = dates_list.index('2020-05-29')\n", "INTERIM_start_ind = dates_list.index('2010-06-01') \n", "INTERIM_end_ind = dates_list.index('2020-03-10') \n", "\n", "\n", "\n", "# GFC: 2007-01-03 -- 2008-09-02 (BLUE)\n", "# GFC peak: 2008-09-02 -- 2009-06-01 (YELLOW)\n", "# GFC: 2009-06-01 -- 2010 --05-31 (BLUE)\n", "# INTERIM: 2010-06-01 -- 2020-03-11 (GREEN)\n", "# COVID-19 PEAK: 2020-03-11 -- 2020-05-29 (RED)\n", "# COVID-19: 2020-05-30 -- 2020-08-31 (ORANGE)\n", "\n", "\n", "\n", "\n", "\n", "for i in range(len(countries)):\n", " plt.figure(1)\n", " plt.plot(dates_list[ t_start_ind :GFC_peak_start_ind ] , new_vals[i][0:GFC_peak_start_ind - t_start_ind ] ,'b' )\n", " plt.plot(dates_list[GFC_peak_start_ind : GFC_peak_end_ind ] , new_vals[i][GFC_peak_start_ind - t_start_ind :GFC_peak_end_ind - t_start_ind ] ,'y' )\n", " plt.plot(dates_list[GFC_peak_end_ind:GFC_end_ind ] , new_vals[i][GFC_peak_end_ind - t_start_ind:GFC_end_ind - t_start_ind ] ,'b' )\n", " plt.plot(dates_list[GFC_end_ind :COVID_peak_start_ind ] , new_vals[i][GFC_end_ind - t_start_ind:COVID_peak_start_ind - t_start_ind ] ,'g' )\n", " plt.plot(dates_list[COVID_peak_start_ind :COVID_peak_end_ind ] , new_vals[i][COVID_peak_start_ind- t_start_ind :COVID_peak_end_ind - t_start_ind] ,'r' )\n", " plt.plot(dates_list[COVID_peak_end_ind :COVID_end_ind ] , new_vals[i][COVID_peak_end_ind - t_start_ind:COVID_end_ind - t_start_ind ] ,color='orange')\n", " \n", " #plt.figure(2)\n", " #plt.plot(dates_list[t_start_ind :t_end_ind ] , new_vals[i] ,color='orange')\n", "\n", "\n", " \n", "positions = []\n", "labels = []\n", "new_year_inds = []\n", "\n", "print('running')\n", "\n", "years = [2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020]\n", "for i in range(len(years)):\n", " year = years[i]\n", " \n", " # Find first recorded date of the year. Assume that at least 1 day from the 1st to the 9th is recorded\n", " for j in range(1,10):\n", " date_str = str(year) + '-01-0' + str(j)\n", " \n", " \n", " # Check if date is recorded. If it is recorded, find index\n", " if date_str in dates_list:\n", "\n", " positions.append(dates_list.index(date_str) - t_start_ind)\n", " labels.append(year)\n", " break\n", " \n", " \n", " \n", " \n", "plt.xticks(positions, labels , rotation='vertical')\n", "plt.figure(1)\n", "beingsaved = plt.figure(1)\n", "plt.show()\n", "beingsaved.savefig('q1_2.eps', format='eps')\n", "\n", "\n", "## (iii) correlation matrix\n", "\n", "\n", "\n", "\n", "endtime = time.time()\n", "\n", "print(starttime - endtime)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3",