Top Banner
Financial Engineering Project Course
40

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

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap.

Financial Engineering Project Course

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

Lecture 3

• Object Oriented Design•Inheritance•Abstract Base Classes•Polymorphism

• Using XML to represent the swap agreement

• Lab Exercise – processing XML with JAXP

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

Using the Swap to transform a liability

Company A Company B

LIBOR

5%

5.2% LIBOR + 0.8%

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

Role of Financial Intermediary

Company A Company B

5.2% LIBOR + 0.8%

LIBOR 5.015%

LIBOR4.985%

FinancialInstitution

Source : John Hull

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

Class Name

Member data

Methods

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

SwapAgreement

double notional;double fixedRate;int numYears;int numPayments;

fixedCashFlow()floatCashFlow()

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

SwapAgreement

public class SwapAgreement {

double notional; double fixedRate; int numYears; int numPayments;

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

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;

}}

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

LIBOR

Static array of values

Static double getLibor()Static void nextLibor()

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

LIBORpublic 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];

}

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

public static void next() {

i++; } public static void main(String a[]) {

System.out.println(Libor.getLIBOR()); }}

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

“is a” “is a”

InterestPayment

FloatInterestPayment FixedInterestPayment

loanAmountyearsnumPayments

abstract computeAmount

AbstractBaseclass

computeAmountuses LIBOR

basisPoints rate

computeAmountWorks with fixed rate

DerivedClasses

Abstract classesare incomplete.

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

InterestPaymentpublic 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;

}

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

public abstract double computeAmount();

public double getLoanAmount() { return loanAmount; }

public int getYears() { return years; }

public int getNumPayments() { return numPayments; }

}

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

FloatInterestPaymentpublic class FloatInterestPayment extends InterestPayment {

private double basisPoints; public FloatInterestPayment(double loanAmount, int years, int numPayments, double basisPoints) {

super(loanAmount, years, numPayments); this.basisPoints = basisPoints;

}

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

public double computeAmount() {

double LIBOR = Libor.getLIBOR() / 100.0;

return getLoanAmount() *

( LIBOR + basisPoints * 0.01 * 0.01 ) *

(getYears() / (double)getNumPayments());

}

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

public static void main(String args[]) {

FloatInterestPayment f = new FloatInterestPayment(100, 1,

2, 80);

System.out.println(f.computeAmount());

}

}

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

FixedInterestPaymentpublic 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;

}

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

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());

}}

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

“is a” “is a”

Party

FloatPayerParty FixedPayerParty

InterestPayment outside;SwapAgreement agreement;double balance;

abstract adjust()

AbstractBaseclass

Adjust() Adjust()

Fill in thedetails

constructor constructor

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

Party

FloatPayerParty FixedPayerParty

InterestPayment outside;SwapAgreement agreement;double balance;

abstract adjust()

Adjust() Adjust()

Calls outside.computeAmount()

Calls outside.computeAmount()

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

Party

FloatPayerParty FixedPayerParty

InterestPayment outside;SwapAgreement agreement;double balance;

abstract adjust()

Adjust() Adjust()

Calls outside.computeAmount()and asks the agreement in orderto adjust the balance

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

Partypublic abstract class Party {

protected InterestPayment outside; protected SwapAgreement agreement; private double balance;

public Party(InterestPayment i, SwapAgreement s) {

outside = i; agreement = s; }

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

public abstract void adjust();

public String toString() {

return "Balance = " + balance;

} void decreaseBalance(double x) { balance = balance - x; } void increaseBalance(double x) { balance = balance + x; }

}

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

FixedPayerPartypublic class FixedPayerParty extends Party {

public FixedPayerParty(InterestPayment i, SwapAgreement s) { super(i,s); }

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

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(); }}

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

FloatPayerParty

public class FloatPayerParty extends Party {

public FloatPayerParty(InterestPayment i, SwapAgreement s) { super(i,s); }

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

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); }}

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

Simulatorpublic 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);

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

for(int period = 1; period <= 6; period++) {

fixedParty.adjust(); floatParty.adjust(); System.out.println(floatParty); Libor.next(); } }}

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

OutputC:\McCarthy\Financial Engineering\FixedFloatSwap>java SimulatorBalance = -2.5Balance = -5.0Balance = -7.5Balance = -10.0Balance = -12.5Balance = -15.0

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

Role of Financial Intermediary

Company A Company B

5.2% LIBOR + 0.8%

LIBOR 5.015%

LIBOR4.985%

FinancialInstitution

Source : John Hull

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

Role of Financial Intermediary

Company A Company B

5.2% LIBOR + 0.8%

LIBOR 5.015%

LIBOR4.985%

FinancialInstitution

Source : John Hull

The Bank has twoagreements.

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

XML For the Agreement

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"><FixedFloatSwap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments></FixedFloatSwap>

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

A DTD For The Agreement

<?xml version="1.0" encoding="utf-8"?><!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) ><!ELEMENT Notional (#PCDATA) ><!ELEMENT Fixed_Rate (#PCDATA) ><!ELEMENT NumYears (#PCDATA) ><!ELEMENT NumPayments (#PCDATA) >

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

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;

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

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();

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

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); } } } }

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

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); }}

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

Lab Problem:

Execute the Simulator2 class on the SwapAgreement.xml file.