In [1]:
import co.uk.bnikolic.qlw.{property_t, qlw, StringVector, LongVector, PropertyVector};
import java.util.UUID;
def UU(s: String) = s+UUID.randomUUID();

// Null value for optional parameters 
def ONull() = qlw.OH_NULL();
Starting Spark application
IDYARN Application IDKindStateSpark UIDriver logCurrent session?
1application_1544536230598_0002sparkidleLinkLink
SparkSession available as 'spark'.
import co.uk.bnikolic.qlw.{property_t, qlw, StringVector, LongVector, PropertyVector}
import java.util.UUID
UU: (s: String)String
ONull: ()co.uk.bnikolic.qlw.property_t
In [2]:
// Valuation of Interest Rate Derivatives. See the Excel spreadsheet
// for context.
def PV(sigma: Double) : Double = {

  var yc=qlw.qlFlatForward(UU("flatfowrard"),
    ONull(),
    new property_t("NullCalendar"),
    new property_t(0.044),
    ONull(),
    new property_t("Continuous"),
    new property_t("Annual"))

  var vc=qlw.qlConstantSwaptionVolatility(UU("vol"),
    new property_t(0),
    "TARGET", "f",
    new property_t(sigma),
    ONull()
  )

  var refDate = qlw.qlSettingsEvaluationDate();
  var calendar="TARGET"
  var pv=new PropertyVector()
  pv.push_back(new property_t(refDate))

  var adjustedRefDate=qlw.qlCalendarAdjust(calendar, pv, new property_t("f"))

  var pv2=new PropertyVector()
  pv2.push_back(new property_t("2d"))

  var effectiveDate=qlw.qlCalendarAdvance(calendar
    , new property_t(adjustedRefDate.get(0))
    , pv2
    , ONull()
    , ONull() ).get(0)

  var pv3=new PropertyVector()
  pv3.push_back(new property_t("10Y"))

  var terminationDate=qlw.qlCalendarAdvance(calendar
    , new property_t(effectiveDate)
    , pv3
    , new property_t("Unadjusted")
    , ONull() ).get(0)

  var sFixed=qlw.qlSchedule(UU("schedule")
    , new property_t(effectiveDate+365)
    , new property_t(terminationDate)
    , "1Y"
    , new property_t(calendar)
    , new property_t("Modified Following")
    , new property_t("Modified Following")
    , new property_t("Backward")
    , ONull()
    , ONull()
    , ONull())

  var sFloating=qlw.qlSchedule(UU("schedule")
    , new property_t(effectiveDate+365)
    , new property_t(terminationDate)
    , "6M"
    , new property_t(calendar)
    , new property_t("Modified Following")
    , new property_t("Modified Following")
    , new property_t("Backward")
    , ONull()
    , ONull()
    , ONull())

  var ibor=qlw.qlEuribor(UU("IBOR"),
    "6M",
    new property_t(yc))

  var vanswap=qlw.qlVanillaSwap(UU(""),
    new property_t("Payer"),
    new property_t(1e6),
    sFixed,
    new property_t(0.05),
    "30/360 (Bond Basis)",
    sFloating,
    ibor,
    new property_t(0.0),
    "Actual/360",
    ONull())


  var pe= qlw.qlBlackSwaptionEngine(UU("PE"), yc, vc)

  var ee=qlw.qlEuropeanExercise(UU(""), new property_t("1Y"))

  var ii=qlw.qlSwaption(UU(""),
    vanswap,
    ee,
    "Physical")

  qlw.qlInstrumentSetPricingEngine(ii, pe)

  var NPV=qlw.qlInstrumentNPV(ii)

  // Clean-up all created objects
  qlw.ohRepositoryDeleteAllObjects(ONull())
  NPV;
}
PV: (sigma: Double)Double
In [3]:
// Run one valuation as a demo
PV(0.1)
res6: Double = 2503.775164574369
In [7]:
//Use Spark to distribute and run 10k valuations for different volatilites
var result=spark.range(0,100).map(r => (r,PV(0.01*r))).toDF()
result.show(100, false)
result: org.apache.spark.sql.DataFrame = [_1: bigint, _2: double]
+---+--------------------+
|_1 |_2                  |
+---+--------------------+
|0  |-6.67773644191864E-4|
|1  |4.066717282080368E-4|
|2  |8.178940326908576E-4|
|3  |0.5940764660645943  |
|4  |18.097986664777903  |
|5  |107.76760838295829  |
|6  |322.50009441694874  |
|7  |680.5996257062834   |
|8  |1175.185844598492   |
|9  |1789.1879244523047  |
|10 |2503.7756741127837  |
|11 |3301.781807153612   |
|12 |4168.671583710467   |
|13 |5092.521344320405   |
|14 |6063.668698268604   |
|15 |7074.313860615015   |
|16 |8118.1374348211475  |
|17 |9190.011784519882   |
|18 |10285.735466535181  |
|19 |11401.853416483444  |
|20 |12535.499270201883  |
|21 |13684.284112225187  |
|22 |14846.196135847855  |
|23 |16019.537860439235  |
|24 |17202.86198881511   |
|25 |18394.925659372508  |
|26 |19594.66493558501   |
|27 |20801.152430449947  |
|28 |22013.584764377636  |
|29 |23231.253196602487  |
|30 |24453.540314166265  |
|31 |25679.89530026572   |
|32 |26909.83224998394   |
|33 |28142.91910965957   |
|34 |29378.761077211086  |
|35 |30617.015056944845  |
|36 |31857.36189639296   |
|37 |33099.517303330096  |
|38 |34343.219760482636  |
|39 |35588.23311290568   |
|40 |36834.34255374207   |
|41 |38081.34313678863   |
|42 |39329.05769801127   |
|43 |40577.31275995661   |
|44 |41825.95131401139   |
|45 |43074.82684167016   |
|46 |44323.80443455874   |
|47 |45572.75740406861   |
|48 |46821.562933325346  |
|49 |48070.11177080917   |
|50 |49318.29742804258   |
|51 |50566.022219756545  |
|52 |51813.18850108179   |
|53 |53059.70788326364   |
|54 |54305.49695148804   |
|55 |55550.47493578318   |
|56 |56794.56271547592   |
|57 |58037.68679177046   |
|58 |59279.777710665156  |
|59 |60520.76996353035   |
|60 |61760.59470085462   |
|61 |62999.19049122829   |
|62 |64236.49905395572   |
|63 |65472.459463212275  |
|64 |66707.01723562187   |
|65 |67940.12078415656   |
|66 |69171.71158028612   |
|67 |70401.7442832426    |
|68 |71630.16609368722   |
|69 |72856.93099063759   |
|70 |74081.99078973167   |
|71 |75305.29908166002   |
|72 |76526.81372420955   |
|73 |77746.49232185571   |
|74 |78964.28836174202   |
|75 |80180.16411193355   |
|76 |81394.08028636644   |
|77 |82605.99295445172   |
|78 |83815.86689855723   |
|79 |85023.66534137342   |
|80 |86229.34740617346   |
|81 |87432.87876214474   |
|82 |88634.22438228158   |
|83 |89833.34901413269   |
|84 |91030.21795907734   |
|85 |92224.79681936665   |
|86 |93417.05239919109   |
|87 |94606.95507366926   |
|88 |95794.46967354222   |
|89 |96979.56472743336   |
|90 |98162.2114751532    |
|91 |99342.37760089908   |
|92 |100520.03379585437  |
|93 |101695.15156877799  |
|94 |102867.69936980444  |
|95 |104037.65012232086  |
|96 |105204.97430931802  |
|97 |106369.64480371813  |
|98 |107531.6336073257   |
|99 |108690.91540760451  |
+---+--------------------+