讲解data编程、辅导program程序设计、Matlab编程语言调试
- 首页 >> Java编程 Computational Finance – HW 2
Complete the following problems in your home work groups. All calculations must be done in Matlab. When the question tells you to report or output an answer, your code must print it using the fprintf function. When the question tells you to plot something, you must copy and paste the plot into your Word file. When the question asks you to describe something or compare, you need to answer it in your own words. You may type your answer directly in the Word file that you will be submitting.
You may find it easiest to have a separate Matlab script file (ending in .m) for each question. But as I said in class, copy and paste both your code and your output from all questions into one Word file. See the Sample Homework on OneDrive as a template. I recommend starting each script file with commands to close all figures, clear the workspace, and clear the command window: close all; clc; clear;
Some helpful hints for the following questions (I’ll leave it to you to figure out where these hints apply ):
MATLAB has a function that calculates the cumulative sum of a vector. You can look it up using MATLAB’s help.
When you call the fprintf() function, if you leave the ‘\n’ off of the format string, the next time you call fprintf() will print again on the same line. This allows you to call fprintf() different ways for multiple items that you print on the same line.
1.Read the excerpt below from Options, Futures, and other Derivatives by John Hull. You will be writing a MATLAB program to simulate credit rating changes of a correlated Baa company and a Ba company following the steps outlined in the question parts below. You will use numbers to represent ratings: 0: default, 1: Aaa, 2: Aa, 3: A, etc. The one-year rating transition matrix is given in Table 22.6 on the next page.
a.[Hint for part a: you should use the RunBedford.m code from the Quiz1 Practice Problems I gave you in Week 3 as the basis for how you write the portion for the discrete inverse transform in this function]
First, write a function, called get_end_of_year_ratings (in its own function file), that generates N correlated rating transitions. Your function should produce the following two separate outputs (i.e., there should be two output variables, each of which is a vector, as specified here):
rating1: an N-element column vector of the end-of-year ratings for company 1.
rating2: an N-element column vector of the end-of-year ratings for company 2.
The function must take the following 5 arguments as inputs, in this order:
p1: a row vector of the cumulative probability boundaries for all the rating categories.
p2: a row vector of the cumulative probability boundaries for all the rating categories.
rho: a 2x2 correlation matrix with the Gaussian copula correlation coefficient between the two companies included.
categories: a row vector of the numeric values to use for each category represented by the cumulative probability vectors p1 and p2 (NOTE: you should represent the default category as a zero, and it does not need to be included in this “categories” vector or in the p1/p2 vectors; simply make it whatever is left over)
N: the number of end-of-year ratings to generate.
Paste the code from your function file here (no outputs for this one):
b.Write a script file that performs the following steps:
Generate 10 million end-of-year ratings for each of two companies, where Company 1 is a Ba-rated company at the start of the year, and Company 2 is Baa to start the year, using a correlation coefficient ρ=0.9. (Hint: start out with a much smaller number, like 100,000 simulations, so that your code runs quickly as you’re making sure it’s working, then run 10 million once you’re satisfied it works properly, as this will take quite a bit longer to run) As stated above, use numbers to represent ratings: 0: default, 1: Aaa, 2: Aa, 3: A, etc., and use the one-year rating transition matrix from Table 22.6 above.
For each company, generate a vector containing the proportion of simulated results of each rating index (the numeric values representing the ratings) 0 through 8. This should result in two nine-element vectors (one for each company), each of which contains the proportion of 0s, the proportion of 1s, proportion of 2s, etc., all the way through proportion of 8s for that company. These proportions are the simulated probabilities of each rating index occurring for each company. Note: Do this in a ‘for’ loop. If you simply copy-and-paste the same code nine times for each of these proportions, you will lose significant points.
Produce a line plot (just using the standard plot() function) of each of these vectors of proportions (times 100 to make them percentages) with the title “part (b): Rating Probabilities” as well as a legend indicating the lines as “Baa Probabilities” and “Ba Probabilities”.
c.Using the two proportion/probability vectors that you just plotted in part (b) times 100, produce a nice-looking, lined-up table that looks like the one I have pictured below using a short series of fprintf() function calls. As a helpful hint, for each column that displays a numeric value, I used a %8.2f placeholder, which turned out to be a nice width. Note: when you print out the numeric values in the table, you should do so using only one fprintf statement to print all of them for the row at once, and do it without indexing the vector (i.e., the entire vector should be the second argument in the fprintf() function call, not one element at a time as multiple arguments). Thus, you print all numeric values in the table for the Baa company at once. Then, when you’re printing the numeric values for the Ba company, you print all of those at once. This part is tricky, and you may have to do some research for how to reuse the same format for multiple values in a vector/matrix with one function call to fprintf().
Once you have produced this table, compare it to Table 22.6’s probability densities. Is it largely the same or different? And what does this tell you about how correlating random variables affects the individual variables’ univariate distributions? (NOTE: I have intentionally produced numbers very different from what you’ll get so I don’t give anything away)
d.For this part, you don’t need to write any code. I have produced a table for you using 10 million simulations like you will for part c (I will provide you the solution code to generate this table). The rightmost three columns represent the following:
Independent (%): The theoretical probability that both companies will end with the same rating specified in the leftmost column if they were independent/uncorrelated. (e.g. that both will default in the first row, that both will have a Aaa rating in the second row, etc.). I calculated this by multiplying the probability densities of each company in Table 22.6 for that end-of-year rating.
Correlated (%): The probability that both companies will end with the same rating specified in the leftmost column based on our simulation, in which the companies are correlated.
Diff (%): This is simply the Correlated (%) column minus the Independent (%) problem.
Given the values in this table, coupled with your conclusion from part (c) regarding correlation’s effects on the univariate probabilities, briefly explain what it looks like the correlation’s effect is overall.
Complete the following problems in your home work groups. All calculations must be done in Matlab. When the question tells you to report or output an answer, your code must print it using the fprintf function. When the question tells you to plot something, you must copy and paste the plot into your Word file. When the question asks you to describe something or compare, you need to answer it in your own words. You may type your answer directly in the Word file that you will be submitting.
You may find it easiest to have a separate Matlab script file (ending in .m) for each question. But as I said in class, copy and paste both your code and your output from all questions into one Word file. See the Sample Homework on OneDrive as a template. I recommend starting each script file with commands to close all figures, clear the workspace, and clear the command window: close all; clc; clear;
Some helpful hints for the following questions (I’ll leave it to you to figure out where these hints apply ):
MATLAB has a function that calculates the cumulative sum of a vector. You can look it up using MATLAB’s help.
When you call the fprintf() function, if you leave the ‘\n’ off of the format string, the next time you call fprintf() will print again on the same line. This allows you to call fprintf() different ways for multiple items that you print on the same line.
1.Read the excerpt below from Options, Futures, and other Derivatives by John Hull. You will be writing a MATLAB program to simulate credit rating changes of a correlated Baa company and a Ba company following the steps outlined in the question parts below. You will use numbers to represent ratings: 0: default, 1: Aaa, 2: Aa, 3: A, etc. The one-year rating transition matrix is given in Table 22.6 on the next page.
a.[Hint for part a: you should use the RunBedford.m code from the Quiz1 Practice Problems I gave you in Week 3 as the basis for how you write the portion for the discrete inverse transform in this function]
First, write a function, called get_end_of_year_ratings (in its own function file), that generates N correlated rating transitions. Your function should produce the following two separate outputs (i.e., there should be two output variables, each of which is a vector, as specified here):
rating1: an N-element column vector of the end-of-year ratings for company 1.
rating2: an N-element column vector of the end-of-year ratings for company 2.
The function must take the following 5 arguments as inputs, in this order:
p1: a row vector of the cumulative probability boundaries for all the rating categories.
p2: a row vector of the cumulative probability boundaries for all the rating categories.
rho: a 2x2 correlation matrix with the Gaussian copula correlation coefficient between the two companies included.
categories: a row vector of the numeric values to use for each category represented by the cumulative probability vectors p1 and p2 (NOTE: you should represent the default category as a zero, and it does not need to be included in this “categories” vector or in the p1/p2 vectors; simply make it whatever is left over)
N: the number of end-of-year ratings to generate.
Paste the code from your function file here (no outputs for this one):
b.Write a script file that performs the following steps:
Generate 10 million end-of-year ratings for each of two companies, where Company 1 is a Ba-rated company at the start of the year, and Company 2 is Baa to start the year, using a correlation coefficient ρ=0.9. (Hint: start out with a much smaller number, like 100,000 simulations, so that your code runs quickly as you’re making sure it’s working, then run 10 million once you’re satisfied it works properly, as this will take quite a bit longer to run) As stated above, use numbers to represent ratings: 0: default, 1: Aaa, 2: Aa, 3: A, etc., and use the one-year rating transition matrix from Table 22.6 above.
For each company, generate a vector containing the proportion of simulated results of each rating index (the numeric values representing the ratings) 0 through 8. This should result in two nine-element vectors (one for each company), each of which contains the proportion of 0s, the proportion of 1s, proportion of 2s, etc., all the way through proportion of 8s for that company. These proportions are the simulated probabilities of each rating index occurring for each company. Note: Do this in a ‘for’ loop. If you simply copy-and-paste the same code nine times for each of these proportions, you will lose significant points.
Produce a line plot (just using the standard plot() function) of each of these vectors of proportions (times 100 to make them percentages) with the title “part (b): Rating Probabilities” as well as a legend indicating the lines as “Baa Probabilities” and “Ba Probabilities”.
c.Using the two proportion/probability vectors that you just plotted in part (b) times 100, produce a nice-looking, lined-up table that looks like the one I have pictured below using a short series of fprintf() function calls. As a helpful hint, for each column that displays a numeric value, I used a %8.2f placeholder, which turned out to be a nice width. Note: when you print out the numeric values in the table, you should do so using only one fprintf statement to print all of them for the row at once, and do it without indexing the vector (i.e., the entire vector should be the second argument in the fprintf() function call, not one element at a time as multiple arguments). Thus, you print all numeric values in the table for the Baa company at once. Then, when you’re printing the numeric values for the Ba company, you print all of those at once. This part is tricky, and you may have to do some research for how to reuse the same format for multiple values in a vector/matrix with one function call to fprintf().
Once you have produced this table, compare it to Table 22.6’s probability densities. Is it largely the same or different? And what does this tell you about how correlating random variables affects the individual variables’ univariate distributions? (NOTE: I have intentionally produced numbers very different from what you’ll get so I don’t give anything away)
d.For this part, you don’t need to write any code. I have produced a table for you using 10 million simulations like you will for part c (I will provide you the solution code to generate this table). The rightmost three columns represent the following:
Independent (%): The theoretical probability that both companies will end with the same rating specified in the leftmost column if they were independent/uncorrelated. (e.g. that both will default in the first row, that both will have a Aaa rating in the second row, etc.). I calculated this by multiplying the probability densities of each company in Table 22.6 for that end-of-year rating.
Correlated (%): The probability that both companies will end with the same rating specified in the leftmost column based on our simulation, in which the companies are correlated.
Diff (%): This is simply the Correlated (%) column minus the Independent (%) problem.
Given the values in this table, coupled with your conclusion from part (c) regarding correlation’s effects on the univariate probabilities, briefly explain what it looks like the correlation’s effect is overall.