Presentation is loading. Please wait.

Presentation is loading. Please wait.

Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap.

Similar presentations


Presentation on theme: "Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap."— Presentation transcript:

1 Financial Engineering Project Course

2 Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap agreement Lab Exercise – processing XML with JAXP

3 Using the Swap to transform a liability Company A Company B LIBOR 5% 5.2% LIBOR + 0.8%

4 Role of Financial Intermediary Company A Company B 5.2% LIBOR + 0.8% LIBOR 5.015% LIBOR 4.985% Financial Institution Source : John Hull

5 Class Name Member data Methods

6 SwapAgreement double notional; double fixedRate; int numYears; int numPayments; fixedCashFlow() floatCashFlow()

7 SwapAgreement public class SwapAgreement { double notional; double fixedRate; int numYears; int numPayments;

8 public SwapAgreement(double notional, double fixed, int years, int numPayments) { this.notional = notional; fixedRate = fixed / 100; numYears = years; this.numPayments = numPayments; } public double floatCashFlow() { return (numYears / (double) numPayments) * Libor.getLIBOR()/100 * notional; } public double fixedCashFlow() { return (numYears / (double) numPayments) * fixedRate * notional; }

9 LIBOR Static array of values Static double getLibor() Static void nextLibor()

10 LIBOR public class Libor { //private static double values[] = { 4.2, 4.8, 5.3, 5.5, 5.6, 5.9, 6.4 }; private static int i = 0; private static double values[] = { 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 }; public static double getLIBOR() { return values[i]; }

11 public static void next() { i++; } public static void main(String a[]) { System.out.println(Libor.getLIBOR()); }

12 “is a” InterestPayment FloatInterestPayment FixedInterestPayment loanAmount years numPayments abstract computeAmount Abstract Base class computeAmount uses LIBOR basisPoints rate computeAmount Works with fixed rate Derived Classes Abstract classes are incomplete.

13 InterestPayment public abstract class InterestPayment { private double loanAmount; private int years; private int numPayments; public InterestPayment(double loanAmount, int years, int numPayments) { this.loanAmount = loanAmount; this.years = years; this.numPayments = numPayments; }

14 public abstract double computeAmount(); public double getLoanAmount() { return loanAmount; } public int getYears() { return years; } public int getNumPayments() { return numPayments; } }

15 FloatInterestPayment public class FloatInterestPayment extends InterestPayment { private double basisPoints; public FloatInterestPayment(double loanAmount, int years, int numPayments, double basisPoints) { super(loanAmount, years, numPayments); this.basisPoints = basisPoints; }

16 public double computeAmount() { double LIBOR = Libor.getLIBOR() / 100.0; return getLoanAmount() * ( LIBOR + basisPoints * 0.01 * 0.01 ) * (getYears() / (double)getNumPayments()); }

17 public static void main(String args[]) { FloatInterestPayment f = new FloatInterestPayment(100, 1, 2, 80); System.out.println(f.computeAmount()); }

18 FixedInterestPayment public class FixedInterestPayment extends InterestPayment { private double rate; public FixedInterestPayment(double loanAmount, int years, int numPayments, double rate) { super(loanAmount, years, numPayments); this.rate = rate / 100; }

19 public double computeAmount() { return getLoanAmount() * rate * (getYears() / (double)getNumPayments()); } public static void main(String args[]) { FixedInterestPayment f = new FixedInterestPayment(100, 1, 2,10.0); System.out.println(f.computeAmount()); }

20 “is a” Party FloatPayerParty FixedPayerParty InterestPayment outside; SwapAgreement agreement; double balance; abstract adjust() Abstract Base class Adjust() Fill in the details constructor

21 Party FloatPayerParty FixedPayerParty InterestPayment outside; SwapAgreement agreement; double balance; abstract adjust() Adjust() Calls outside.computeAmount()

22 Party FloatPayerParty FixedPayerParty InterestPayment outside; SwapAgreement agreement; double balance; abstract adjust() Adjust() Calls outside.computeAmount() and asks the agreement in order to adjust the balance

23 Party public abstract class Party { protected InterestPayment outside; protected SwapAgreement agreement; private double balance; public Party(InterestPayment i, SwapAgreement s) { outside = i; agreement = s; }

24 public abstract void adjust(); public String toString() { return "Balance = " + balance; } void decreaseBalance(double x) { balance = balance - x; } void increaseBalance(double x) { balance = balance + x; }

25 FixedPayerParty public class FixedPayerParty extends Party { public FixedPayerParty(InterestPayment i, SwapAgreement s) { super(i,s); }

26 public void adjust() { // pay outside lender double pay = outside.computeAmount(); decreaseBalance(pay); // Pay counterParty LIBOR by decreasing balance // by the correct amount pay = agreement.floatCashFlow(); decreaseBalance(pay); // Receive fixedRate amount agreed upon double get = agreement.fixedCashFlow(); }

27 FloatPayerParty public class FloatPayerParty extends Party { public FloatPayerParty(InterestPayment i, SwapAgreement s) { super(i,s); }

28 public void adjust() { // pay outside lender LIBOR + Points double pay = outside.computeAmount(); decreaseBalance(pay); // Receive counterParty LIBOR by adding to balance double get = agreement.floatCashFlow(); increaseBalance(get); // Pay fixedRate amount agreed upon pay = agreement.fixedCashFlow(); decreaseBalance(pay); }

29 Simulator public class Simulator { public static void main(String a[]) { SwapAgreement agree = new SwapAgreement(100, 5, 3, 6 ); FloatInterestPayment floatpmt = new FloatInterestPayment(100,3,6,0); FixedInterestPayment fixedpmt = new FixedInterestPayment(100,3,6,5); FixedPayerParty fixedParty = new FixedPayerParty(fixedpmt,agree); FloatPayerParty floatParty = new FloatPayerParty(floatpmt, agree);

30 for(int period = 1; period <= 6; period++) { fixedParty.adjust(); floatParty.adjust(); System.out.println(floatParty); Libor.next(); }

31 Output C:\McCarthy\Financial Engineering\FixedFloatSwap>java Simulator Balance = -2.5 Balance = -5.0 Balance = -7.5 Balance = -10.0 Balance = -12.5 Balance = -15.0

32 Role of Financial Intermediary Company A Company B 5.2% LIBOR + 0.8% LIBOR 5.015% LIBOR 4.985% Financial Institution Source : John Hull

33 Role of Financial Intermediary Company A Company B 5.2% LIBOR + 0.8% LIBOR 5.015% LIBOR 4.985% Financial Institution Source : John Hull The Bank has two agreements.

34 XML For the Agreement 100 5 3 6

35 A DTD For The Agreement <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) >

36 Reading the SwapAgreement with JAXP import java.io.File; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

37 public class Simulator2 { public static void main(String argv[]) { Document doc; if(argv.length != 1 ) { System.err.println("usage: Simulator2 documentname"); System.exit(1); } try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); doc = docBuilder.parse(new File(argv[0])); Node top = doc.getDocumentElement();

38 if(top.hasChildNodes()) { NodeList list = top.getChildNodes(); for(int i = 0; i < list.getLength();i++) { Node n = list.item(i); if(n.getNodeType() == Node.ELEMENT_NODE){ Node content = n.getFirstChild(); String s = content.getNodeValue(); System.out.println(s); }

39 catch(SAXParseException err) { System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); }

40 Lab Problem: Execute the Simulator2 class on the SwapAgreement.xml file.


Download ppt "Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap."

Similar presentations


Ads by Google