Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
ISOM5160 Exercise 04 - Stock Portfolio
The dataset contains 9960 rows and 6 columns Date - the last day of the week in the format of d/m/yyyy ^HSI - the close point of ^HSI index on that day Towngas - the close price of Towngas (0003.hk) on that day HSBC - the close price of HSBC (0005.hk) on that day HKEX - the close price of HKEX (0388.hk) on that day Tencent - the close price of Tencent (0700.hk) on that day PetroChina the close price of PetroChina (0857.hk) on that day Task 4.0: Imports packages and the data file In [3]: import numpy as np import pandas as pd import matplotlib.pyplot as plt # To print multiple outputs for one cell from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all' # Set the float number format when displaying a dataframe pd.options.display.float_format = "{:,.2f}".format In [2]: Task 4.1: Plot 5 box plots of Hang Seng Index for 2016, 2017, ..., 2020, respectively Hint: store each year's data into one array, and then combine them into a ragged nested list to pass to the boxplot() function
RangeIndex: 261 entries, 0 to 260
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 261 non-null datetime64[ns]
1 ^HSI 261 non-null float64
2 Towngas 261 non-null float64
3 HSBC 261 non-null float64
4 HKEX 261 non-null float64
5 Tencent 261 non-null float64
6 PetroChina 261 non-null float64
7 Year 261 non-null int64
dtypes: datetime64[ns](1), float64(6), int64(1)
memory usage: 16.4 KB
Out[2]:
Date ^HSI Towngas HSBC HKEX Tencent PetroChina Year
0 2016-01-01 20,333.34 9.11 57.30 184.10 140.20 4.56 2016
1 2016-01-08 19,817.41 9.03 56.30 179.80 137.10 4.48 2016
2 2016-01-15 18,542.15 8.31 51.90 162.50 133.10 4.18 2016
3 2016-01-22 19,195.83 8.35 53.35 162.20 141.60 4.56 2016
4 2016-01-29 19,183.09 8.52 52.00 165.50 141.90 4.61 2016
Out[2]:
Date ^HSI Towngas HSBC HKEX Tencent PetroChina Year
256 2020-11-27 26,728.50 11.37 43.40 384.80 589.00 2.48 2020
257 2020-12-04 26,410.59 11.22 41.90 384.20 578.00 2.32 2020
258 2020-12-11 26,678.38 10.97 41.70 393.20 583.00 2.40 2020
259 2020-12-18 26,386.56 10.82 40.20 413.00 557.50 2.38 2020
260 2020-12-25 27,231.13 10.99 40.65 430.00 566.00 2.39 2020
stocks = pd.read_csv('stocks_2016-2020.csv')
stocks['Date'] = pd.to_datetime(stocks['Date'])
stocks['Year'] = pd.DatetimeIndex(stocks['Date']).year
stocks.info()
stocks.head(5)
stocks.tail(5)
In [4]:
# Example of plotting two boxplots side by side
x1 = np.random.rand(10)
x2 = np.random.rand(9)
plt.boxplot(np.array([x1, x2], dtype='object'), labels=['x1', 'x2']);
In [17]:
Task 4.2: Given the number of shares below, plot a line chart showing the weekly portfolio values in
2016-2020
Out[17]:
Text(0.5, 0.98, '')
Out[17]:
Text(0.5, 1.0, '')
Out[17]:
Text(0.5, 0, 'Hang Seng Index')
boxplot = stocks.boxplot(grid=False, column=['^HSI'], by=['Year'])
plt.suptitle('')
plt.title('')
boxplot.set_xlabel('Hang Seng Index', fontweight='bold')
plt.show()
In [6]:
# Example of setting major ticks and rotate axis labels
x = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
y = np.random.rand(7)
fig, ax = plt.subplots()
ax.plot(x, y);
ax.xaxis.set_major_locator(plt.MaxNLocator(4));
plt.xticks(rotation = 45);
In [9]:
Out[9]:
[]
Out[9]:
(array([16600., 16800., 17000., 17200., 17400., 17600., 17800., 18000.,
18200., 18400., 18600., 18800.]),
[Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, '')])
Out[9]:
Text(0.5, 1.0, 'Portfolio Value in 2016-2020')
shares = {'Towngas':5000, 'HSBC':1000, 'HKEX':300, 'Tencent':200, 'PetroChina':10000}
stocks["Portfolio"] = stocks.Towngas* shares['Towngas'] + \
stocks.HSBC* shares['HSBC'] + \
stocks.HKEX* shares['HKEX'] + \
stocks.PetroChina* shares['PetroChina']
fig, ax = plt.subplots(figsize=(12,6))
ax.plot(stocks.Date, stocks.Portfolio, color="green");
ax.xaxis.set_major_locator(plt.MaxNLocator(12))
plt.xticks(rotation = 45);
ax.set_title("Portfolio Value in 2016-2020", fontweight="bold")
Task 4.3: Given the same number of shares , plot one pie chart showing the portfolio composition at the
end of 2020, with the stock name and percentage as the data labels
In [21]:
Out[21]:
([,
,
,
,
],
[Text(0.9770747438877047, 0.5052968878360292, 'Towngas 22.11%'),
Text(0.2861617284159295, 1.0621259177658777, 'HSBC 16.36%'),
Text(-1.029310681842863, 0.3879684526403928, 'HKEX 51.91%'),
Text(0.1887128497396573, -1.0836915891263241, 'Tencent 45.55%'),
Text(1.0763843481951236, -0.22670848012493694, 'PetroChina 9.62%')])
Out[21]:
Text(0.5, 0, 'Portfolio composition in 2020')
shares = {'Towngas':5000, 'HSBC':1000, 'HKEX':300, 'Tencent':200, 'PetroChina':10000}
stocks_2020 = stocks.tail(1)[['Towngas','HSBC','HKEX','Tencent','PetroChina', 'Portfolio']]
for key in shares.keys():
stocks_2020[key] = stocks_2020[key]*shares[key]/stocks_2020.Portfolio*100
stocks_2020.drop("Portfolio",axis=1,inplace=True)
stocks_2020 = stocks_2020.T
stocks_2020.columns = ['stocks']
colors = ['#f7fcfd', '#ccece6', '#66c2a4', '#238b45', '#00441b']
labels = []
for stock in stocks_2020.index:
labels.append(stock + " {:.2f}%".format(stocks_2020.loc[stock,'stocks']))
fig, ax= plt.subplots(figsize=(7,7))
ax.pie(stocks_2020.stocks, labels=labels, colors= colors);
ax.set_xlabel("Portfolio composition in 2020", fontweight="bold")
Task 4.4: Plot 5 stacked bars for the accumulated portfolio values of the stocks at the last day of 2016,
2017, 2018, 2019, 2020
In [4]:
# Example of plotting a stacked bar
y = [10, 30, 80, 20]
fig, ax = plt.subplots();
ax.bar("x", y[0]);
ax.bar("x", y[1], bottom=y[0]);
ax.bar("x", y[2], bottom=y[0]+y[1]);
ax.bar("x", y[3], bottom=y[0]+y[1]+y[2], color='yellow');
In [22]:
Task 4.5: Design one plot of your choice to visualize some data about this portfolio of 5 stocks
Must be of a chart type different from the previous 4 tasks
shares = {'Towngas':5000, 'HSBC':1000, 'HKEX':300, 'Tencent':200, 'PetroChina':10000}
years=range(2016,2021)
stacked_bar=pd.DataFrame()
for year in years:
lastdate =stocks[stocks.Year==year].sort_values("Date").tail(1).Date.squeeze()
stacked_bar =stacked_bar.append(pd.Series(shares)*(stocks.loc[stocks.Date==lastdate,"
stacked_bar=stacked_bar.set_index(pd.Series(years))
ax = stacked_bar.plot(kind='bar', stacked=True)
In [34]:
File "", line 11
fig, ax = plt.subplots(1,1 , figsize=(9, 9))
^
IndentationError: unexpected indent
index = ['Towngas', 'HSBC', 'HKEX', 'PetroChina']
shares = {'Towngas':5000, 'HSBC':1000, 'HKEX':300, 'Tencent':200, 'PetroChina':10000}
profits = {'Towngas':10000, 'HSBC':20000, 'HKEX':90000, 'Tencent':40000, 'PetroChina':50000
stocks["Earning Per Share"] = profits['Towngas'] / shares['Towngas'] + \
profits['HSBC'] / shares['HSBC'] + \
profits['HKEX'] / shares['HKEX'] + \
profits['PetroChina'] / shares['PetroChina']
fig, ax = plt.subplots(1,1 , figsize=(9, 9))
ax.barh(index, stocks['Earning Per Share'])
ax.set_xlabel('Avg Porfits')
ax.set_ylabel('Sector')