Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base...
-
date post
20-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base...
Lecture 3
• Object Oriented Design•Inheritance•Abstract Base Classes•Polymorphism
• Using XML to represent the swap agreement
• Lab Exercise – processing XML with JAXP
Role of Financial Intermediary
Company A Company B
5.2% LIBOR + 0.8%
LIBOR 5.015%
LIBOR4.985%
FinancialInstitution
Source : John Hull
SwapAgreement
double notional;double fixedRate;int numYears;int numPayments;
fixedCashFlow()floatCashFlow()
SwapAgreement
public class SwapAgreement {
double notional; double fixedRate; int numYears; int numPayments;
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;
}}
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];
}
public static void next() {
i++; } public static void main(String a[]) {
System.out.println(Libor.getLIBOR()); }}
“is a” “is a”
InterestPayment
FloatInterestPayment FixedInterestPayment
loanAmountyearsnumPayments
abstract computeAmount
AbstractBaseclass
computeAmountuses LIBOR
basisPoints rate
computeAmountWorks with fixed rate
DerivedClasses
Abstract classesare incomplete.
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;
}
public abstract double computeAmount();
public double getLoanAmount() { return loanAmount; }
public int getYears() { return years; }
public int getNumPayments() { return numPayments; }
}
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;
}
public double computeAmount() {
double LIBOR = Libor.getLIBOR() / 100.0;
return getLoanAmount() *
( LIBOR + basisPoints * 0.01 * 0.01 ) *
(getYears() / (double)getNumPayments());
}
public static void main(String args[]) {
FloatInterestPayment f = new FloatInterestPayment(100, 1,
2, 80);
System.out.println(f.computeAmount());
}
}
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;
}
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());
}}
“is a” “is a”
Party
FloatPayerParty FixedPayerParty
InterestPayment outside;SwapAgreement agreement;double balance;
abstract adjust()
AbstractBaseclass
Adjust() Adjust()
Fill in thedetails
constructor constructor
Party
FloatPayerParty FixedPayerParty
InterestPayment outside;SwapAgreement agreement;double balance;
abstract adjust()
Adjust() Adjust()
Calls outside.computeAmount()
Calls outside.computeAmount()
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
Partypublic abstract class Party {
protected InterestPayment outside; protected SwapAgreement agreement; private double balance;
public Party(InterestPayment i, SwapAgreement s) {
outside = i; agreement = s; }
public abstract void adjust();
public String toString() {
return "Balance = " + balance;
} void decreaseBalance(double x) { balance = balance - x; } void increaseBalance(double x) { balance = balance + x; }
}
FixedPayerPartypublic class FixedPayerParty extends Party {
public FixedPayerParty(InterestPayment i, SwapAgreement s) { super(i,s); }
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(); }}
FloatPayerParty
public class FloatPayerParty extends Party {
public FloatPayerParty(InterestPayment i, SwapAgreement s) { super(i,s); }
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); }}
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);
for(int period = 1; period <= 6; period++) {
fixedParty.adjust(); floatParty.adjust(); System.out.println(floatParty); Libor.next(); } }}
OutputC:\McCarthy\Financial Engineering\FixedFloatSwap>java SimulatorBalance = -2.5Balance = -5.0Balance = -7.5Balance = -10.0Balance = -12.5Balance = -15.0
Role of Financial Intermediary
Company A Company B
5.2% LIBOR + 0.8%
LIBOR 5.015%
LIBOR4.985%
FinancialInstitution
Source : John Hull
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.
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>
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) >
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;
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();
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); } } } }
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); }}