-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from agrotz/master
Use native finmath curves in equities framework
- Loading branch information
Showing
10 changed files
with
970 additions
and
1,130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 10 additions & 53 deletions
63
src/main/java/net/finmath/equities/marketdata/FlatYieldCurve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,26 @@ | ||
package net.finmath.equities.marketdata; | ||
|
||
import java.time.LocalDate; | ||
|
||
import net.finmath.time.daycount.DayCountConvention; | ||
|
||
/** | ||
* Class to provide methods of a flat yield curve. | ||
* TODO This class should be integrated into or replaced by finmat-lib's curve universe. | ||
* | ||
* @author Andreas Grotz | ||
*/ | ||
|
||
public class FlatYieldCurve { | ||
private final LocalDate curveDate; | ||
private final double rate; | ||
private final DayCountConvention dayCounter; | ||
|
||
public FlatYieldCurve( | ||
final LocalDate curveDate, | ||
final double rate, | ||
final DayCountConvention dayCounter) | ||
{ | ||
this.curveDate = curveDate; | ||
this.rate = rate; | ||
this.dayCounter = dayCounter; | ||
} | ||
|
||
public FlatYieldCurve rollToDate(LocalDate date) | ||
{ | ||
return new FlatYieldCurve(date, rate, dayCounter); | ||
} | ||
|
||
public double getRate(double maturity) | ||
{ | ||
assert maturity >= 0.0 : "maturity must be positive"; | ||
return rate; | ||
} | ||
|
||
public double getRate(LocalDate date) | ||
{ | ||
return getRate(dayCounter.getDaycountFraction(curveDate, date)); | ||
} | ||
public class FlatYieldCurve extends YieldCurve { | ||
|
||
public double getDiscountFactor(double maturity) | ||
{ | ||
assert maturity >= 0.0 : "maturity must be positive"; | ||
return Math.exp(-maturity * rate); | ||
} | ||
private final static int longTime = 100; | ||
|
||
public double getForwardDiscountFactor(double start, double expiry) | ||
{ | ||
assert start >= 0.0 : "start must be positive"; | ||
assert expiry >= start : "start must be before expiry"; | ||
return getDiscountFactor(expiry) / getDiscountFactor(start); | ||
} | ||
public FlatYieldCurve(final LocalDate curveDate, final double rate, final DayCountConvention dayCounter) { | ||
super("NONE", curveDate, dayCounter, new LocalDate[] { curveDate.plusYears(longTime) }, new double[] { | ||
Math.exp(-rate * dayCounter.getDaycountFraction(curveDate, curveDate.plusYears(longTime))) }); | ||
} | ||
|
||
public double getDiscountFactor(LocalDate date) | ||
{ | ||
return getDiscountFactor(dayCounter.getDaycountFraction(curveDate, date)); | ||
} | ||
public FlatYieldCurve rollToDate(LocalDate date) { | ||
assert date.isAfter(baseCurve.getReferenceDate()) : "can only roll to future dates"; | ||
return new FlatYieldCurve(date, getRate(baseCurve.getReferenceDate().plusYears(longTime)), dayCounter); | ||
} | ||
|
||
public double getForwardDiscountFactor(LocalDate startDate, LocalDate endDate) | ||
{ | ||
assert !startDate.isBefore(curveDate) : "start date must be after curve date"; | ||
assert !endDate.isBefore(startDate) : "end date must be after start date"; | ||
return getDiscountFactor(endDate) / getDiscountFactor(startDate); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/net/finmath/equities/marketdata/YieldCurve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.finmath.equities.marketdata; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Arrays; | ||
import net.finmath.marketdata.model.curves.CurveInterpolation.ExtrapolationMethod; | ||
import net.finmath.marketdata.model.curves.CurveInterpolation.InterpolationEntity; | ||
import net.finmath.marketdata.model.curves.CurveInterpolation.InterpolationMethod; | ||
import net.finmath.marketdata.model.curves.DiscountCurveInterpolation; | ||
import net.finmath.time.daycount.DayCountConvention; | ||
|
||
/** | ||
* Class to provide methods of a yield curve. | ||
* | ||
* @author Andreas Grotz | ||
*/ | ||
|
||
public class YieldCurve { | ||
|
||
protected final LocalDate referenceDate; | ||
protected final LocalDate[] discountDates; | ||
protected final DayCountConvention dayCounter; | ||
protected final DiscountCurveInterpolation baseCurve; | ||
|
||
public YieldCurve(final String name, final LocalDate referenceDate, final DayCountConvention dayCounter, | ||
final LocalDate[] discountDates, final double[] discountFactors) { | ||
this.dayCounter = dayCounter; | ||
this.discountDates = discountDates; | ||
double[] times = new double[discountDates.length]; | ||
boolean[] isParameter = new boolean[discountDates.length]; | ||
for (int i = 0; i < times.length; i++) { | ||
times[i] = dayCounter.getDaycountFraction(referenceDate, discountDates[i]); | ||
} | ||
baseCurve = DiscountCurveInterpolation.createDiscountCurveFromDiscountFactors(name, referenceDate, times, | ||
discountFactors, isParameter, InterpolationMethod.LINEAR, ExtrapolationMethod.CONSTANT, | ||
InterpolationEntity.LOG_OF_VALUE_PER_TIME); | ||
|
||
this.referenceDate = referenceDate; | ||
} | ||
|
||
public YieldCurve rollToDate(LocalDate date) { | ||
assert date.isAfter(referenceDate) : "can only roll to future dates"; | ||
LocalDate[] rolledDiscountDates = Arrays.stream(discountDates).filter(p -> p.isAfter(date)) | ||
.toArray(LocalDate[]::new); | ||
double[] rolledDiscountFactors = new double[rolledDiscountDates.length]; | ||
for (int i = 0; i < rolledDiscountDates.length; i++) { | ||
rolledDiscountFactors[i] = getForwardDiscountFactor(date, rolledDiscountDates[i]); | ||
} | ||
|
||
return new YieldCurve(baseCurve.getName(), date, dayCounter, rolledDiscountDates, rolledDiscountFactors); | ||
} | ||
|
||
public double getRate(double maturity) { | ||
assert maturity >= 0.0 : "maturity must be positive"; | ||
return baseCurve.getZeroRate(maturity); | ||
} | ||
|
||
public double getRate(LocalDate date) { | ||
return baseCurve.getZeroRate(dayCounter.getDaycountFraction(referenceDate, date)); | ||
} | ||
|
||
public double getDiscountFactor(double maturity) { | ||
assert maturity >= 0.0 : "maturity must be positive"; | ||
return baseCurve.getDiscountFactor(maturity); | ||
} | ||
|
||
public double getForwardDiscountFactor(double start, double expiry) { | ||
assert start >= 0.0 : "start must be positive"; | ||
assert expiry >= start : "start must be before expiry"; | ||
return getDiscountFactor(expiry) / getDiscountFactor(start); | ||
} | ||
|
||
public double getDiscountFactor(LocalDate date) { | ||
return baseCurve.getDiscountFactor(dayCounter.getDaycountFraction(referenceDate, date)); | ||
} | ||
|
||
public double getForwardDiscountFactor(LocalDate startDate, LocalDate endDate) { | ||
assert !startDate.isBefore(referenceDate) : "start date must be after curve date"; | ||
assert !endDate.isBefore(startDate) : "end date must be after start date"; | ||
return getDiscountFactor(endDate) / getDiscountFactor(startDate); | ||
} | ||
} |
Oops, something went wrong.