MCom in Risk Management
Risk Management Computing Skills 2025
Tutorial 7 - Guidelines
Problems
AIFMRM offers a quarterly equity risk service which provides a variety of risk statistics on all of the ordinary shares
that are listed on the JSE’s Equity Market. One of the most important statistics is the beta of each share with
respect to the FTSE/JSE All Share index (J203). FTSE/JSE, on the other hand, also rebalance all of the major market
indices on a quarterly basis, i.e., constituents of major market indices may change on a quarterly basis based on
their performance and liquidity. In this tutorial, you will use quarterly index constituent data from the FTSE/JSE and
equity risk statistics from AIFMRM to calculate various statistics for some important indices on a quarterly basis. In
doing so, you will use tables, structures and cells to store your final results.
#1 Import Data using MATLAB’s Import Tool
On MATLAB’s Home tab, in the VARIABLE section, click on Import Data and then fine and select the CSV
file named “FTSEJSE [Link]”. In the window that pops up, on the IMPORT tab in the IMPORT section, click
on the drop-down menu named “Import Selection” and select “Generate Function”. Rename this function to
ImportFTSEJSE IC, and save the m-file. This function will allow you to import the data from the CSV file and
automatically create a table called FTSEJSE IC in your workspace, by using the command:
>> FTSEJSE IC = ImportFTSEJSE IC(‘FTSEJSE [Link]’);
Do the same for the other CSV file named ”AIFMRM [Link]”. Test that both your functions work.
This problem requires you to use MATLAB’s Import Data functionality to generate functions to import the two
CSV files (“FTSEJSE [Link]” and ”AIFMRM [Link]”). Make sure your functions are called ImportFTSEJSE IC
and ImportAIFMRM ERS respectively.
#2 Get Index Constituents and Calculate Weights
Write a function called GetICsAndWeights that takes as input:
• the table object called FTSEJSE IC;
• a datetime object called rDate; and
• a string object called indexCode.
The rDate variable will be one of the quarter end dates, for e.g., ’31-Dec-2019’, while the indexCode will be
either ‘TOPI’, ‘DTOP’, ‘RESI’, ‘FINI’, ‘INDI’, ‘PCAP’, ‘SAPY’ or ‘ALTI’.
The function should return a string column vector called ICs that contains the constituents of the respec-
tive indexCode in the same month and year as the inputted rDate. To determine this, you have to:
(i) identify the relevant dates using a logical check on the Date column and rDate;
(ii) use a logical check on the column variable names to identify the column which has the indexCode along
with the suffix ’New’ as a name; and
(iii) select the share codes from the Alpha column when the corresponding element from the column selected
in (ii) is the same as the indexCode
The function should also return a numeric column vector called weights which contains the market capitalisa-
tion based weight of each index constituent. To do this, in step (iii) above also select the market capitalisation
from the GrossMarketCapitalisation column. The weight of each constituent is simply the market capitali-
sation of each share divided by the sum of the market capitalisation of all shares within the respective index.
You may use for loops and if statements to do this, or logical indexing will also work. Test your function with
1
rDate = datetime(‘31-Dec-2019’) and indexCode = ‘‘RESI’’, which should return 10 constituents.
This problem requires you to return the constituents of a given index code on a given date as a string vector.
You are also required to return each constitents respective market capitalisation as a proportion of the total
index market capitalisation - this will be the weights vector. The problem uses the information contained in
the “FTSEJSE [Link]” which you would have imported in problem 1 and assigned to the variable FTSEJSE IC.
Given FTSEJSE IC, rDate and indexCode as inputs:
(a) Check which dates in the Date column have the same month and year as rDate.
(b) Look for the column in FTSEJSE IC that is called ‘‘indexCode’’New, so in this case ‘‘RESINew’’.
(c) Now you want to select all the share codes that are constituents of the index code on the relevant date.
To do this, when you find the column in (b), look for elements within the column that are the same as the
indexCode, also paying attention to the relevant date.
(d) Once you have found these elements, find the corresponding share codes from the Alpha column. Return
this as ICs.
(e) Also find the corresponding market capitalisations from the GrossMarketCapitalisation column and use
this to calculate the weights as the market capitalisation of each share divided by the sum of the market
capitalisation of all shares within the respective index.
#3 Get Betas, Market and Specific Volatilities
Write a function called GetBetasMktAndSpecVols that takes as input:
• the table object called AIFMRM ERS;
• a datetime object called rDate; and
• a string column vector called ICs.
The rDate variable is as above, while the ICs input comes from the function created above.
The function should return a numeric column vector called betas that contains the betas of the respective
shares contained in ICs, in the same month and year as the inputted rDate. To do this, you have to:
(i) identify the relevant dates using a logical check on the Date column and rDate; and
(ii) select the betas from the Beta column when the corresponding element from the Instrument column is
the same as a constituent in the ICs vector.
The function should also return a numeric column vector called specVols which contains the specific volatilities
of each constituent. To do this, in step (ii) above also select the specific volatility from the UniqueRisk column
for each constituent.
Finally, the function should return a numeric scalar called mktVol that contains the volatility of the mar-
ket index, the J203, on rDate. You can find this quantity in the TotalRisk column.
You may use for loops and if statements to do this, or logical indexing will also work. Test your function with
rDate = datetime(‘31-Dec-2019’) and ICs from the results of the test in Problem 2 above.
This problem requires you to return the betas and specific volatilities of the shares in ICs. You are also required
to return the volatility of the market index, the J203, on the given date. The problem uses the information
contained in the “AIFMRM [Link]” which you would have imported in problem 1 and assigned to the variable
AIFMRM ERS. Given AIFMRM ERS, rDate and ICs as inputs:
(a) Check which dates in the Date column have the same month and year as rDate.
(b) In the Instrument column, look for all the elements that are the same as the constituents in the ICs
vector, on the relevant date.
(c) Once you have found these elements, find the corresponding betas from the Beta column. Return this as
betas.
(d) Also find the corresponding specific volatilities from the UniqueRisk column.
(e) Lastly, find the volatility of the market index, by looking for the corresponding volatility in the TotalRisk
column. You can also find the market index in the Instrument column.
#4 Calculate Statistics Write a function called CalcStats that takes as input:
• a numeric column vector called weights;
2
• a numeric column vector called betas;
• a numeric scalar called mktVol; and
• a numeric column vector called specVols.
Of course, all of these inputs come from the functions built above.
Assuming that the market, or J203, volatility is denoted by m, then for an index or portfolio or consisting
of n assets with: (i) weights w = [w1 , w2 , . . . , wn ]⊺ ; (ii) betas β = [β1 , β2 , . . . , βn ]⊺ ; and (iii) specific volatilities
s = [s1 , s2 , . . . , sn ]⊺ , we may calculate the following statistics:
Portfolio Beta = w⊺ β ,
Systematic Covariance Matrix = β β ⊺ m2 ,
Portfolio Systematic Variance = w⊺ β β ⊺ w m2 ,
Specific Covariance Matrix = S 2 ,
Portfolio Specific Variance = w⊺ S 2 w ,
Total Covariance Matrix = β β ⊺ m2 + S 2 ,
Portfolio Variance = w⊺ β β ⊺ w m2 + w⊺ S 2 w ,
Correlation Matrix = D−1 β β ⊺ m2 + S 2 D−1 ,
where S is a diagonal matrix constructed with s and D is a diagonal matrix of total asset volatilities.
Using the above as a reference, your function should return the following outputs:
• pfBeta - the portfolio beta;
• sysCov - the systematic covariance matrix;
• pfSysVol - the portfolio systematic volatility;
• specCov - the specific covariance matrix;
• pfSpecVol - the portfolio specific volatility;
• totCov - the total covariance matrix;
• pfVol - the portfolio total volatility; and
• CorrMat - the correlation matrix.
Test your function with data from the tests for the problems above.
This problem should not be too difficult and just requires you to implement the given formulae. The only
difficulty may be computing D which is a diagonal matrix of total asset volatilities. Total asset variances are
the elements on the diagonal of your total covariance matrix, therefore the total asset volatilities are the square
root of these elements.
#5 Create a Table and Cell Array
In a script file, create a table with the following columns:
(i) Dates corresponding to AIFMRM quarter-end reporting dates with format ‘MMM-yy’;
(ii) IndexCodes with the codes of the respective indices;
(iii) IndexBetas using pfBeta from the previous function;
(iv) IndexSysVols using pfSysVol from the previous function;
(v) IndexSpecVols using pfSpecVol from the previous function; and
(vi) IndexVols using pfVol from the previous function.
where each row of the table pairs a unique reporting date from the AIFMRM ERS table with each of the
indexCodes listed in Problem 2. If you’ve done this correctly, you will end up with 112 rows in your table.
In a script file, create a cell array with the following data in column:
(1) AIFMRM quarter-end reporting dates with format ‘MMM-yy’;
(2) codes of the respective indices;
(3) ICs from the function in Problem 2;
(4) totCov from the previous function; and in column
3
(5) CorrMat from the previous function.
where each row of the cell pairs a unique reporting date from the AIFMRM ERS table with each of the indexCodes
listed in Problem 2. If you’ve done this correctly, you will end up with a 112 × 5 cell object in your workspace.
The previous problems required to perform your calculations given a specific date and index code. This question
requires you to create a table and cell that contain information for all index codes for all relevant dates. This
requires you to run your functions from problems 2-4 through loops to populate these objects.
Practical steps to take to complete this problem:
• For the given date (’31-Dec-2019’), try and create a table and cell, to get an idea of how each will be
populated.
• Repeat this step for a couple of dates to further make sure you understand.
• Extract the uniques dates from the AIFMRM ERS table. You can type out the list of indexCodes as given
in Problem 2.
• For each of these unique date and indexCode combinations, call your three functions to get the relevant
output and use these to populate the table, struct and cell. Hint: Use a nested for loop.