fork download
  1. /*
  2.  * Copyright (c) 2013 AWOLart.com
  3.  */
  4.  
  5. package com.awolart.fin.cu.ps2;
  6.  
  7.  
  8. import java.text.NumberFormat;
  9.  
  10. /**
  11.  * Suppose a 6-year swap with a notional principal of $10 million is being
  12.  * configured. What is the fixed rate of interest that will make the value
  13.  * of the swap equal to zero.
  14.  * (You should use the term structure of interest rates from Question 1.)
  15.  * <p/>
  16.  * Please submit your answer as a percentage rounded to two decimal places;
  17.  * i.e., if your answer is 4.567% or equivalently 0.04567,
  18.  * then submit answer as 4.57.
  19.  * <p/>
  20.  */
  21. public class FixedRateSwapZero {
  22.  
  23. /**
  24.   * <p>
  25.   * Method to calculate the discount rate $ d(0,4) $
  26.   * where $ d(0,4) = \frac{1}{(1 + r)^t} $
  27.   * <p/>
  28.   * More generically, $$ r = \frac{1 - d(0,T)}{\sum_{t0}^{T} d(0,T)} $$
  29.   * </p>
  30.   *
  31.   * @param s double representing the spot interest rate at time $ t $
  32.   * @param t double representing the time $ t $
  33.   * @return double containing the calculated discount rate
  34.   */
  35. public static double discountRate(double s, double t) {
  36. return ((1 / Math.pow(1 + s, t)));
  37. }
  38.  
  39.  
  40. public static double getFixedRateDenominator(double time, double num) {
  41. double X = 0.0;
  42. int idx = 1;
  43. for(int i = 0; i < num; ++i) {
  44. X += discountRate(i, idx);
  45. ++idx;
  46. }
  47.  
  48. return X;
  49.  
  50. }
  51.  
  52. private static double discountedFloatingRateCF(double C, double[] i, int n) {
  53. double dfr = 0.0;
  54. // return (C / (Math.pow((1 + i), n)));
  55. int i_len = i.length;
  56. for(int j = 0; j < i_len; ++j) {
  57. double dpv = ( C / ( Math.pow( ( 1 + i[j] ), n) ) );
  58. System.out.println("discounted pv at " + j + " = " + dpv);
  59. dfr += dpv;
  60. }
  61. System.out.println("DFR = " + dfr);
  62. return dfr;
  63. }
  64.  
  65. public static double zeroSwap(double[] s, double t) {
  66. double rate = 0.0;
  67. int idx = 1;
  68. int s_len = s.length;
  69. for(int i = 0; i < s_len; ++i) {
  70. double dr = ( 1 - discountRate(i, idx) );
  71. System.out.println(" Discount rate where time is " + i
  72. + " and interest rate is " + s[i] + " = " + dr);
  73. rate += dr;
  74. ++idx;
  75. }
  76.  
  77. // * r = 1−d(0,T) / ∑T t=1 d(0,t)
  78.  
  79. double Z = ( ( 1 - discountRate(0, s_len+1) ) / rate );
  80. return rate;
  81. }
  82.  
  83. public static double discountRateIterater(double[] s, double t) {
  84. int s_len = s.length;
  85. double total = 0.0;
  86. for(int i = 1; i <= s_len; ++i)
  87. {
  88. double idr = discountRate(s[i], i+1);
  89. System.out.println(" Discount rate where time is " + i
  90. + " and interest rate is " + s[i] + " = " + idr);
  91. total += idr;
  92. }
  93. return total;
  94. }
  95.  
  96.  
  97. /*
  98.   * 1. sum(all discount factors from 1:6)
  99.   * 2. find the discount factor of T = 6
  100.   * 3. 1 - (discount factor of T=6)
  101.   * 4 divide step 3/step 1
  102.   */
  103. public static double sumAllDiscountFactors(double[] s, double t) {
  104. double sum = 0.0;
  105. int s_len = s.length;
  106. double idx = 1.0;
  107. for(int i = 1; i < s_len; ++i) {
  108. double dr = discountRate(s[i], idx);
  109. sum += dr;
  110. ++idx;
  111. }
  112. return sum;
  113. }
  114.  
  115.  
  116.  
  117. /**
  118.   * Convenience main method to facilitate command line/ide testing.
  119.   *
  120.   * @param args
  121.   */
  122. public static void main(String[] args) {
  123.  
  124. NumberFormat formatter = NumberFormat.getNumberInstance();
  125. formatter.setMaximumFractionDigits(2);
  126. formatter.setMinimumFractionDigits(4);
  127.  
  128. double[] s = { 0.070, 0.073, 0.077, 0.081, 0.084, 0.088 };
  129.  
  130. /*
  131.   double[] sx = { 0.070, 0.073, 0.077, 0.081, 0.084, 0.088 };
  132.   double C = 10000000.00;
  133.   double d_0_T = ( 1 - (discountRate(s[5], 6) ) );
  134.   System.out.println("d(0,T) = " + formatter.format(d_0_T));
  135.   double sum_fpmts = C * ( d_0_T );
  136.   System.out.println("Discount FR Pmts = " + formatter.format(sum_fpmts));
  137.   */
  138.  
  139.  
  140. // C = notional; i = interest rate; and n = number of payments
  141. ////discountedFloatingRateCF(C, s, s.length);
  142.  
  143. /*
  144.   double Z = zeroSwap(s, s.length);
  145.   System.out.println("zeroSwap = " + formatter.format(Z));
  146.   */
  147.  
  148. /*
  149.   double sum = sumAllDiscountFactors(s, s.length);
  150.   System.out.println("sumAllDiscountFactors = " + formatter.format(sum));
  151.   */
  152.  
  153. /*
  154.   * 1. sum(all discount factors from 1:6)
  155.   * 2. find the discount factor of T = 6
  156.   */
  157. double d1 = discountRate(s[0], 1);
  158. System.out.println("d1 = " + formatter.format(d1));
  159. double d2 = discountRate(s[1], 2);
  160. System.out.println("d2 = " + formatter.format(d2));
  161. double d3 = discountRate(s[2], 3);
  162. System.out.println("d3 = " + formatter.format(d3));
  163. double d4 = discountRate(s[3], 4);
  164. System.out.println("d4 = " + formatter.format(d4));
  165. double d5 = discountRate(s[4], 5);
  166. System.out.println("d5 = " + formatter.format(d5));
  167. double d6 = discountRate(s[5], 6);
  168. System.out.println("d6 = " + formatter.format(d6));
  169. double sum1 = d1 + d2 + d3 + d4 + d5 + d6;
  170. System.out.println("sum d(0,1) to d(5,6) = " + formatter.format(sum1));
  171.  
  172. /*
  173.   * 3. 1 - (discount factor of T=6)
  174.   * 4 divide step 3/step 1
  175.   */
  176. double discounted = 1 - d6;
  177. System.out.println("1 - d(1,6) = " + formatter.format(discounted));
  178. double answer = discounted / sum1;
  179. System.out.println("Answer = " + formatter.format(answer * 100)); //
  180. }
  181.  
  182. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
6
compilation info
Main.java:21: error: class FixedRateSwapZero is public, should be declared in a file named FixedRateSwapZero.java
public class FixedRateSwapZero {
       ^
1 error
stdout
Standard output is empty