IC208 - Programming for Finance Seminar 5
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
IC208 - Programming for Finance
Seminar 5
1) Read data into a dataframe from the file ‘stock indices.csv’ (which can be downloaded
in BlackBoard> IC208> Lecture Materials> Week 5).
2) Save data of individual stock index in separate csv files. For example, FTSE Index
would be saved in ‘Uk.csv’ or ‘FTSEALLSHARE.csv’ . DAX30 Index would be saved in ‘Germany.csv’ or ‘DAX30.csv’
3) Read all stock indices into a data frame from the individual files.
4) Save data of each stock index in a separate subfolder. For example, FTSE Index would
be saved in subfolder named ‘Uk’ or ‘FTSEALLSHARE’ . DAX30 Index would be saved in subfolder named ‘Germany’ or ‘DAX30’ .
5) Inspect basic characteristics and summary statistics of the dataframe.
6) Create a new column, named ‘num’, which has random numbers in interval [0,1)
7) Drop the ‘num’ column
8) Rename the column ‘indx’ to ‘price’
9) Count how many rows in the dataframe is for the UK i.e. country==’Uk’
10) Create a new dataframe which only includes observations/rows for the UK
11) Add a column of percentage return of the FTSE All Share index
12) Add a column of logarithmic return of the FTSE All Share index
13) Plot FTSE All Share returns over time.
14) Plot the stock indices of all country in the dataframe in one graph
15) Create a dataframe that has 4 columns corresponding to logarithm returns of 04 stock indices.
There are many ways to code in Python. The below is only a guide.
1) Ans:
import os
import pandas as pd
os.chdir(<path>)
data = pd.read_csv('stock indices.csv')
2) Ans:
import os, glob
os.chdir(<path>)
glob.glob(
for c in countries:
temp_data = data[data['country']==c]
temp_data.to_csv('{}.csv'.format(c), index=False)
3) Ans:
import os, glob
os.chdir(<path>)
csv_files = glob.glob('*.csv')
data = pd.DataFrame()
for f in csv_files:
temp_data = pd.read_csv(f)
data = data.append(temp_data)
4) Ans:
data = pd.read_csv('stock indices.csv')
countries = list(data['country'].unique())
for c in countries:
temp_data = data[data['country']==c]
os.mkdir('{}'.format(c))
temp_data.to_csv('{}\{}.csv'.format(c,c), index=False)
5) Ans:
data.shape
data.size
data.dtypes
data.columns.values
data.sample(10)
data.describe()
6) Ans:
numbers = np.random.rand(data.shape[0])
data['num'] = numbers
7) Ans:
data = data.drop(columns=['num'])
8) Ans:
data = data.rename(columns={'indx':'price'})
9) Ans:
data[data['country']=='Uk'].shape[0]
#or
mask = (data['country']=='Uk')
data[mask].shape[0]
10) Ans:
data_uk = data[data['country']=='Uk']
data_uk['datetime'] = pd.to_datetime(data_uk['date'])
data_uk = data_uk.set_index('datetime')
11) Ans:
data_uk['pct_return'] = data_uk['price'].pct_change()
12) Ans:
data_uk['log_price'] = np.log(data_uk['price'])
data_uk['log_return'] = data_uk['log_price'].diff(1)
#or
data_uk['log_return']= np.log(data_uk['price']/data_uk['price'].shift(1))
13) Ans:
data_uk['log_return'].plot()
data_uk[['log_return','pct_return']].plot()
#or
import matplotlib as mpl
from matplotlib import pyplot as plt
fig=plt.figure()
ax1=fig.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(data_uk['log_return'])
ax1.set_xlabel('date')
ax1.set_ylabel('log return')
ax1.set_title('FTSE All Share')
plt.show()
14) Ans:
new_data = pd.DataFrame()
countries = list(data['country'].unique())
for c in countries:
temp_data = data.loc [data['country']==c,['price','date']] temp_data['datetime'] = pd.to_datetime(temp_data['date']) temp_data = temp_data.set_index('datetime') new_data['{}'.format(c)] = temp_data['price']
#or
idata = data[['country','date','price']]
new_data = idata.pivot(index='date',columns='country',values='price') new_data['datetime'] = pd.to_datetime(new_data.index)
new_data = new_data.set_index('datetime')
new_data = new_data.sort_index()
#then
fig=plt.figure()
ax1=fig.add_axes([0.1,0.1,0.8,0.8])
ax1.plot (new_data[countries])
ax1.set_xlabel('date')
ax1.set_ylabel('price')
ax1.set_title('Major indices')
plt.show()
15) Ans:
def log_change(price):
return np.log(price / price.shift(1))
cols = list(new_data.columns.values)
for col in cols:
new_data['ret_{}'.format(col)] = log_change(new_data['{}'.format(col)])
2023-03-09