Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Portfolio Management with python

Codebook for Case-Study 2

2023-07

0.Get ready


# Import the data from the excel

rets = pd .read_excel( 'portfoliom_data .xlsx',

header=0,

index_col=0,

parse_dates=True )


# Show data

rets

Out[3]:               Asset A:    Asset B:    Asset C:    Asset D:    S&p 500

Date:

2015-01-01        -0.069          0.063        -0.090        -0.043         -0.023

2015-03-01           0.015           0.122          0.080           0.021           0.073

2015-05-01           0.167          0.259          0.003           0.165           0.149

2021-08-01          0.025          0.039         -0.142          0.092         -0.004

2021-10-01          0.043          0.041          0.029         -0.031           0.020

2021-12-01        -0.056          0.053          0.106          0.049           0.057

84 rows × 5 columns

Out[4]:               Asset A:    Asset B:    Asset C:    Asset D:

Date:

2015-01-01        -0.069          0.063        -0.090        -0.043

2015-03-01           0.015           0.122          0.080           0.021

2015-05-01           0.167          0.259          0.003           0.165

2021-08-01          0.025          0.039         -0.142          0.092

2021-10-01          0.043          0.041          0.029         -0.031

2021-12-01        -0.056          0.053          0.106          0.049

84 rows × 4 columns

Out[5]:

In [6]:

Out[6]:

<Axes: xlabel='Date:'>

# Assign the last column into variable 'rets_sp'

rets_sp = rets .iloc[:,-1 ]

rets_sp

Date:

2015-01-01

2015-02-01

2015-03-01

2015-04-01

2015-05-01

2021-08-01

2021-09-01

2021-10-01

2021-11-01

2021-12-01

-0 .023

0.006

0.073

0.035

0.149

...

-0 .004

0.069

0.020

0.128

0.057

Name: S&p 500, Length: 84, dtype: float64


1.Calculate the average monthly return, population variance and standard deviation for each of the 4 stocks;


Out[8]:

Asset A:

Asset B:

Asset C:

0.013750

0.037405

0.018107


Asset D:     0.015762

dtype: float64

0.023809523809523808

Out[11]:  (84, 4)

84


Out[13]:

Asset A:

Asset B:

Asset C:

0.011424

0.034342

0.015415



Asset D:     0.013852

dtype: float64

Out[15]:

Asset A:

Asset B:

Asset C:

0.011424

0.034342

0.015415



Asset D:     0.013852

dtype: float64

Out[16]:

Asset A: True

Asset B: True

Asset C: True

Asset D: True

dtype: bool


0.022521772275280405

Out[18]:

(Asset

Asset

Asset

Asset

dtype:

Asset

Asset

Asset

Asset

dtype:

0.004721

0.006367

0.005276

0.004024

float64,

0.068710

0.079794

0.072633

0.063432

float64)

2.Construct a co-variance matrix;


# Now we construct the variance-covariance matrix, this is easier than we expected.

cov_assets = rets_assets .cov (ddof=0 )

cov_assets

Out[19]:            Asset A:     Asset B:     Asset C:     Asset D:

Asset A:     0.004721    0.002225    0.001479    0.000932

Asset C:     0.001479    0.001485    0.005276      0.001411

3.Form a portfolio with equal weights. Calculate the expected return, variance and standard deviation of the equal weight-portfolio;


# Set the equal weights for the equal weight portfolio

ew = np .ones (4 )*0.25

ew


# Call the function to calculate the return and volatility of the equal weight portfolio

ewport_r = portfolio_r (weights