Quan%ta%ve	
  Finance	
  in	
  q/kdb+	
  
Pricing	
  Fixed	
  Income	
  Deriva%ves	
  
Mark	
  Lefevre	
  
Quan%ta%ve	
  Analyst	
  
Introduc%on	
  
•  q/kdb+	
  can	
  be	
  used	
  for	
  much	
  more	
  than	
  
lightning	
  fast	
  table	
  manipula%on	
  
•  Excellent	
  analy%cal	
  capabili%es	
  
•  Combining	
  these	
  strengths	
  with	
  3rd-­‐party	
  
soIware	
  and/or	
  linking	
  with	
  addi%onal	
  
libraries	
  provides	
  excep%onal	
  opportuni%es	
  to	
  
analyze,	
  model	
  and	
  predict	
  
Mo%va%on	
  
•  q/kdb+	
  is	
  oIen	
  used	
  in	
  combina%on	
  with	
  MatLab	
  and	
  R	
  for	
  heavy	
  
quan%ta%ve	
  finance	
  work	
  
•  q/kdb+	
  integrates	
  well	
  with	
  many	
  foreign	
  languages	
  and	
  
applica%ons	
  
–  C,	
  C#,	
  Java,	
  Python,	
  Perl,	
  Fortran,	
  Scala	
  
–  ODBC,	
  GPUs	
  (CUDA)	
  
–  Matlab,	
  R	
  
•  Moving	
  algorithmic	
  code	
  into	
  q/kdb+	
  is	
  not	
  always	
  straighWorward	
  
–  Out-­‐of-­‐the-­‐box	
  q/kdb+	
  lacks	
  high-­‐level	
  sta%s%cs,	
  op%miza%on,	
  linear	
  
algebra	
  
–  Algorithmic	
  code	
  from	
  other	
  languages/environments	
  usually	
  was	
  
developed	
  with	
  a	
  different	
  mindset	
  
•  Let’s	
  explore	
  this	
  topic	
  by	
  breaking	
  in	
  a	
  dyadic	
  manner	
  
1.  Mathema%cs	
  Fundamentals/Numerical	
  Analysis	
  
2.  Advanced,	
  Prac%cal	
  Applica%on:	
  Pricing	
  a	
  Fixed	
  Income	
  Deriva%ve	
  
Solving	
  Systems	
  of	
  Linear	
  Equa%ons	
  
Quick	
  Review	
  
	
  
	
  
q)A:3 3#1 3 -2 3 5 6 2 4
3f;
q)B:3 1#5 7 8f;
q)X:.qml.mls[A;B];
q)X
-15
8
2
	
  
MATHEMATICS	
  FUNDAMENTALS	
  
Mul%ple	
  Regression	
  
Least-­‐Squares	
  (LSQ)	
  Approxima%on	
  
Quick	
  Review	
  
	
  
	
  
	
  
q)ingredients:(“FFFF”;”,”)
0:`ingredients.csv;
q)heat:”F” $ read0`heat.csv;
q)ingredients:flip ingredients;
q)heat:13 1#heat;
q).qml.mm[.qml.mm[.qml.minv[.qml.
mm[flip
ingredients;ingredients]];flip
ingredients];heat]
2.193046
1.153326
0.7585091
0.4863193	
  
MATHEMATICS	
  FUNDAMENTALS	
  
Problema%c	
  Linear	
  Regression	
  
•  One	
  area	
  of	
  focus	
  in	
  Numerical	
  Analysis	
  is	
  numerical	
  
stability	
  
•  We	
  can	
  demonstrate	
  a	
  numerical	
  stability	
  problem	
  
using	
  a	
  simple	
  linear	
  regression	
  as	
  an	
  example	
  
•  Simple	
  prac%cal	
  example	
  
–  Car	
  loans	
  regressed	
  on	
  year	
  from	
  U.S	
  Fed	
  Reserve	
  
•  References	
  
•  R	
  tutorial	
  
	
  
hbp://www.cyclismo.org/tutorial/R/
linearLeastSquares.html	
  
MATHEMATICS	
  FUNDAMENTALS	
  
Reference	
  Matlab	
  Implementa%on	
  
MATHEMATICS	
  FUNDAMENTALS	
  
q/kdb+	
  Implementa%on	
  
•  Live	
  Demonstra%on	
  
MATHEMATICS	
  FUNDAMENTALS	
  
Demonstra%on	
  Results	
  
// Problematic Multiple Regression example
// Based on R Tutorial Data
// http://www.cyclismo.org/tutorial/R/linearLieastSquares.html
//
// Car Loans regressed on year from U.S. Federal Reserve
// http://www.federalreserve.gov/releases/g19/20050805
//
l qml.q
year:1f cross 2000f+til 5
rate:9.34 8.50 7.62 6.93 6.60f;
// Reorient the rate matrix
rate:5 1#rate;
// Run least squares approximation
a:(flip year) $ year;
b:inv a;
y:(b $ (flip year)) $ rate;
x:.qml.mm[.qml.mm[.qml.minv[.qml.mm[flip year;year]];flip
year];rate];
show "Year Matrix";
show year;
show "Intermediate Result a";
show a;
show "Matrix Inversion of a";
show "Ill-conditioned Matrix!";
show "a's 2-norm condition number is 8.0321e+12"
show b;
show "Results Using q"
show y;
show "Results Using QML"
show x;
Welcome	
  to	
  kdb+	
  32bit	
  edi%on	
  
For	
  support	
  please	
  see	
  hbp://groups.google.com/d/forum/personal-­‐kdbplus	
  
Tutorials	
  can	
  be	
  found	
  at	
  hbp://code.kx.com/wiki/Tutorials	
  
To	
  exit,	
  type	
  	
  
To	
  remove	
  this	
  startup	
  msg,	
  edit	
  q.q	
  
"Year	
  Matrix"	
  
1	
  2000	
  
1	
  2001	
  
1	
  2002	
  
1	
  2003	
  
1	
  2004	
  
"Intermediate	
  Result	
  a"	
  
5	
  	
  	
  	
  	
  10010	
  	
  	
  	
  	
  	
  	
  	
  
10010	
  2.004003e+07	
  
"Matrix	
  Inversion	
  of	
  a"	
  
"Ill-­‐condi%oned	
  Matrix!"	
  
"a's	
  2-­‐norm	
  condi%on	
  number	
  is	
  8.0321e+12"	
  
	
  	
  
	
  	
  
"Results	
  Using	
  q"	
  
	
  
	
  
"Results	
  Using	
  QML"	
  
1419.208	
  
-­‐0.705	
  	
  
MATHEMATICS	
  FUNDAMENTALS	
  
Problem	
  Avoidance	
  by	
  
Input	
  Transforma%on	
  
// Problematic Multiple Regression example
// Based on R Tutorial Data
// http://www.cyclismo.org/tutorial/R/linearLieastSquares.html
//
// Car Loans regressed on year from U.S. Federal Reserve
// http://www.federalreserve.gov/releases/g19/20050805
//
l qml.q
year:1f cross 0f+til 5
rate:9.34 8.50 7.62 6.93 6.60f;
// Reorient the rate matrix
rate:5 1#rate;
// Run least squares approximation
a:(flip year) $ year;
b:inv a;
y:(b $ (flip year)) $ rate;
x:.qml.mm[.qml.mm[.qml.minv[.qml.mm[flip year;year]];flip
year];rate];
show "Year Matrix";
show year;
show "Intermediate Result a";
show a;
show "Matrix Inversion of a";
show "a's 2-norm condition number is 22.4555"
show b;
show "Results Using q"
show y;
show "Results Using QML"
show x;
Welcome	
  to	
  kdb+	
  32bit	
  edi%on	
  
For	
  support	
  please	
  see	
  hbp://groups.google.com/d/forum/personal-­‐kdbplus	
  
Tutorials	
  can	
  be	
  found	
  at	
  hbp://code.kx.com/wiki/Tutorials	
  
To	
  exit,	
  type	
  	
  
To	
  remove	
  this	
  startup	
  msg,	
  edit	
  q.q	
  
"Year	
  Matrix"	
  
1	
  0	
  
1	
  1	
  
1	
  2	
  
1	
  3	
  
1	
  4	
  
"Intermediate	
  Result	
  a"	
  
5	
  	
  10	
  
10	
  30	
  
"Matrix	
  Inversion	
  of	
  a"	
  
"a's	
  2-­‐norm	
  condi%on	
  number	
  is	
  22.4555"	
  
0.6	
  	
  -­‐0.2	
  
-­‐0.2	
  0.1	
  	
  
"Results	
  Using	
  q"	
  
9.208	
  	
  
-­‐0.705	
  
"Results	
  Using	
  QML"	
  
9.208	
  	
  
-­‐0.705	
  
MATHEMATICS	
  FUNDAMENTALS	
  
Q	
  Math	
  Library	
  
•  qml	
  is	
  a	
  free	
  library	
  for	
  q/kdb+	
  that	
  links	
  into	
  various	
  
numerical	
  libraries	
  including	
  LAPACK	
  
–  Linear	
  algebra	
  
–  Sta%s%cs	
  
–  Op%miza%on	
  
•  Compiling	
  qml	
  with	
  32-­‐bit	
  q/kdb+	
  on	
  a	
  64-­‐bit	
  machine	
  
requires	
  pa%ence	
  and	
  the	
  correct	
  compilers,	
  but	
  works	
  
–  LAPACK	
  is	
  wriben	
  in	
  Fortran	
  90,	
  so	
  you	
  will	
  need	
  a	
  Fortran	
  compiler,	
  as	
  
well	
  as	
  the	
  usual	
  suspects	
  
References
LAPACK http://www.netlib.org/lapack/
qml http://althenia.net/qml
	
  
Fixed	
  Income	
  Deriva%ves	
  
Source:	
  Bank	
  of	
  Interna%onal	
  
Seblements	
  (BIS)	
  Quarterly	
  Review,	
  
June	
  2015	
  
•  Most	
  common	
  fixed	
  
income	
  deriva%ves	
  
–  Op5ons	
  
•  Caps,	
  floors,	
  swaps	
  
–  Futures/Forwards	
  
–  Credit	
  Default	
  Swaps	
  
(CDS)	
  
–  Interest	
  Rate	
  Swaps	
  
–  Cross	
  Currency	
  Swaps	
  
–  Total	
  Return	
  Swaps	
  
0	
  
5000	
  
10000	
  
15000	
  
20000	
  
25000	
  
30000	
  
35000	
  
40000	
  
Jun.05	
  
Jun.06	
  
Jun.07	
  
Jun.08	
  
Jun.09	
  
Jun.10	
  
Jun.11	
  
Jun.12	
  
Jun.13	
  
Jun.14	
  
USD	
  Billions	
  
OTC	
  Deriva5ves	
  (No5onal	
  Outstanding)	
  
Unallocated	
  
Credit	
  default	
  swaps	
  
Commodity	
  contracts	
  
Equity-­‐linked	
  contracts	
  
Foreign	
  exchange	
  
contracts	
  
ADVANCED	
  APPLICATION	
  
Pricing	
  Caps	
  and	
  Caplets	
  
•  A	
  cap	
  is	
  an	
  op%on	
  on	
  an	
  
underlying	
  floa%ng	
  
interest	
  rate,	
  such	
  as	
  
LIBOR,	
  that	
  provides	
  
investors	
  with	
  a	
  hedge	
  
against	
  the	
  interest	
  rate	
  
rising	
  above	
  the	
  cap	
  
rate	
  
•  The	
  cap	
  is	
  composed	
  of	
  
individual	
  caplets	
  
•  If	
  the	
  interest	
  rate	
  is	
  
above	
  the	
  strike,	
  the	
  
caplet	
  is	
  in-­‐the-­‐money	
  
•  Rate	
  set	
  at	
  %me	
  t	
  
•  Payoff	
  	
  at	
  %me	
  t+1	
  
	
  
•  Present	
  Value	
  
max​(​ 𝑟↓𝑡 − 𝑘,0)× 𝜏× 𝑁	
  
​​ 𝐷 𝐹↓𝑂𝐼𝑆 ↓𝑡+1 ×max​(​ 𝑟↓𝑡 − 𝑘,0)× 𝜏× 𝑁	
  
ADVANCED	
  APPLICATION	
  
Heath-­‐Jarrow-­‐Morton	
  Model	
  
•  HJM	
  models	
  the	
  dynamics	
  of	
  the	
  
en%re	
  instantaneous	
  forward	
  rate	
  
curve	
  
•  Purple	
  is	
  instantaneous	
  rate	
  agreed	
  
at	
  t	
  for	
  %me	
  s	
  
•  Blue	
  is	
  a	
  ZCB	
  at	
  that	
  future	
  %me	
  
over	
  the	
  future	
  %me	
  period	
  
•  Green	
  is	
  differen%al	
  and	
  integral	
  
forms	
  of	
  the	
  dynamics.	
  Wt	
  is	
  GBM	
  
•  Red	
  is	
  key	
  rela%onship	
  between	
  
driI	
  (α)	
  and	
  vola%lity	
  (σ)!	
  
•  Widely	
  used	
  in	
  prac%ce	
  
•  Uses	
  Monte-­‐Carlo	
  methods	
  and	
  
discrete	
  %me	
  finance	
  
•  This	
  presenta%on	
  glosses	
  over	
  
many	
  important	
  implementa%on	
  
details	
  in	
  the	
  interest	
  of	
  %me	
  
References
hbp//www.columbia.edu/~mh2078/
HJM_models.pdf	
  
𝑓(𝑡, 𝑠)=−​ 𝜕/𝜕𝑠 ​log⁠​ 𝑍↓𝑡↑𝑠  	
  
​ 𝑍↓𝑡↑𝑠 =−∫𝑡↑𝑠▒𝑓(𝑡, 𝑢)𝑑𝑢 	
  
𝑑𝑓(𝑡, 𝑠)= 𝛼(𝑡, 𝑠)𝑑𝑡+ 𝜎​( 𝑡, 𝑠)↑𝑇 𝑑​ 𝑊↓𝑡 	
  
𝛼(𝑡, 𝑠)= 𝜎​( 𝑡, 𝑠)↑𝑇 ∫𝑡↑𝑠▒𝜎(𝑡, 𝑢)𝑑𝑢 	
  
𝑓(𝑡, 𝑠)= 𝑓(0, 𝑠)+∫0↑𝑡▒𝛼(𝑢, 𝑠)𝑑𝑢 
+∫0↑𝑡▒𝜎​( 𝑢, 𝑠)↑𝑇 𝑑​ 𝑊↓𝑢  	
  
ADVANCED	
  APPLICATION	
  
Historical	
  Data	
  Analysis	
  
ADVANCED	
  APPLICATION	
  
Principal	
  Component	
  Analysis	
  (PCA)	
  
•  PCA	
  is	
  a	
  sta%s%cal	
  procedure	
  
that	
  u%lizes	
  orthogonal	
  
transforma%ons	
  to	
  convert	
  a	
  
set	
  of	
  observa%ons	
  of	
  possibly	
  
correlated	
  variables	
  into	
  a	
  set	
  
of	
  values	
  of	
  linearly	
  
uncorrelated	
  variables	
  called	
  
principal	
  components.	
  
•  In	
  a	
  word,	
  decorrela(on	
  
•  The	
  principal	
  components	
  are	
  
the	
  eigenvectors	
  of	
  a	
  
symmetric	
  variance-­‐
covariance	
  matrix	
  
•  Eigenvectors	
  are	
  ordered	
  by	
  
their	
  corresponding	
  
eigenvalues,	
  which	
  is	
  also	
  the	
  
amount	
  of	
  variance	
  explained	
  
by	
  the	
  component	
  
•  If	
  we	
  take	
  just	
  a	
  few	
  of	
  the	
  
first	
  principal	
  components,	
  we	
  
can	
  achieve	
  
–  dimensionality	
  reduc5on	
  
–  work	
  in	
  linear	
  parameter	
  
space	
  
•  This	
  is	
  very	
  useful	
  for	
  
simula%ng	
  high	
  dimensionality	
  
problems,	
  such	
  as	
  
instantaneous	
  forward	
  curve	
  
evolu%ons	
  
ADVANCED	
  APPLICATION	
  
PCA	
  in	
  q/kdb+	
  with	
  qml	
  
//	
  Load	
  q	
  Math	
  Library	
  (qml)	
  
l	
  qml.q	
  
	
  
//	
  Read	
  in	
  historical	
  JPY	
  LIBOR	
  data	
  
histLIBOR:	
  ("SFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";enlist	
  ",")	
  
0:`QProject.csv;	
  
	
  
//	
  Calculate	
  daily	
  differences	
  
histDeltas:	
  deltas	
  delete	
  Tenor	
  from	
  histLIBOR;	
  
delete	
  from	
  `histDeltas	
  where	
  i	
  =	
  0;	
  
	
  
//	
  Define	
  a	
  func%on	
  to	
  calculate	
  variance-­‐covariance	
  matrix	
  
varCov:{a	
  cov/::	
  a:flip	
  x};	
  
	
  
//	
  Calculate	
  an	
  annualize	
  variance-­‐covariance	
  matrix	
  
histCov:varCov[histDeltas]*252%10000;	
  
	
  
//	
  Extract	
  eigenvalues	
  and	
  eigenvectors	
  sorted	
  by	
  decreasing	
  modulus	
  
eigen:.qml.mev[value	
  each	
  value	
  histCov];	
  
	
  
//	
  Write	
  out	
  data	
  for	
  graphing	
  elsewhere	
  
graphData1:(1	
  51)#raze	
  eigen[0];	
  
save	
  `graphData1.csv	
  
graphData2:(1	
  51)#sums	
  eigen[0]%sum	
  eigen[0];	
  
save	
  `graphData2.csv	
  
graphData3:(3	
  51)#eigen[1][%l	
  3];	
  
save	
  `graphData3.csv	
  
	
  
ADVANCED	
  APPLICATION	
  
PCA	
  Results	
  
ADVANCED	
  APPLICATION	
  
Vola%lity	
  Fi•ng	
  
•  In	
  a	
  mul%-­‐factor	
  HJM	
  
framework,	
  we	
  need	
  to	
  
integrate	
  over	
  vola%lity	
  
func%ons	
  of	
  a	
  form	
  like	
  
	
  
	
  
•  What	
  this	
  means	
  in	
  
English,	
  is	
  we	
  have	
  
several	
  eigenvalue	
  /
eigenvector	
  func%ons	
  
driving	
  the	
  dynamics	
  at	
  
par%cular	
  tenors	
  
•  Consistent	
  with	
  Musiela	
  
parameteriza%on	
  
•  This	
  permits	
  a	
  rich	
  
evolu%on	
  of	
  forward	
  rates	
  
exhibi%ng	
  
–  Level	
  shiIs	
  
–  Twists	
  
–  Buberfly-­‐like	
  inflec%on	
  
about	
  specific	
  tenors	
  
•  Subject	
  to	
  regimes	
  
ADVANCED	
  APPLICATION	
  
Monte	
  Carlo	
  Simula%on	
  
Random	
  Number	
  
Genera%on	
  
•  For	
  each	
  %mestep,	
  we	
  
need	
  3	
  normally	
  
distributed	
  random	
  
values	
  
•  Use	
  the	
  Box_Muller	
  
transform	
  as	
  
implemented	
  in	
  Nick	
  
Psaris’	
  Q	
  Tips	
  
•  Computa%onally	
  more	
  
intense,	
  however	
  beber	
  
accuracy	
  
/ box-muller
bm:{
if[count[x] mod
2;'`length];
x:2 0N#x;
r:sqrt -2f*log first x;
theta:2f*acos[-1f]*last
x;
x: r*cos theta;
x,:r*sin theta;
x
ADVANCED	
  APPLICATION	
  
Pricing	
  
•  Live	
  Demonstra%on	
  
ADVANCED	
  APPLICATION	
  
Conclusion	
  
•  q/kdb+	
  can	
  be	
  used	
  for	
  much	
  more	
  than	
  table	
  
manipula%on	
  
•  Demonstrated	
  some	
  basic	
  mathema%cs	
  and	
  an	
  
advanced,	
  prac%cal	
  applica%on	
  to	
  pricing	
  caplets	
  
•  Used	
  3rd-­‐party	
  soIware	
  for	
  visualiza%on,	
  however	
  the	
  
new	
  HTML5	
  capabili%es	
  hold	
  lots	
  of	
  promise	
  
•  Linked	
  to	
  and	
  leveraged	
  industrial-­‐strength	
  
mathema%cs	
  libraries	
  
•  Hopefully,	
  this	
  has	
  provide	
  a	
  glimpse	
  at	
  the	
  excep%onal	
  
opportuni%es	
  to	
  analyze,	
  model	
  and	
  predict	
  that	
  q/kdb
+	
  can	
  provide	
  
About	
  Me	
  
•  Mark	
  is	
  currently	
  consul%ng	
  at	
  one	
  of	
  the	
  largest	
  banks	
  in	
  Tokyo	
  as	
  a	
  quan%ta%ve	
  
analyst	
  developing	
  high-­‐performance	
  algorithmic	
  trading	
  systems	
  on	
  the	
  e-­‐FX	
  
desk.	
  Prior	
  to	
  moving	
  to	
  Japan,	
  he	
  worked	
  in	
  London	
  for	
  Unicredit	
  on	
  the	
  Equity-­‐
Linked	
  Origina%on	
  desk	
  crea%ng	
  conver%ble	
  bonds	
  for	
  European	
  corporates,	
  
consulted	
  in	
  the	
  US	
  on	
  e-­‐commerce	
  analy%cs	
  and	
  worked	
  for	
  several	
  high-­‐tech	
  
soIware	
  companies.	
  
•  Earlier	
  in	
  his	
  career,	
  he	
  worked	
  for	
  Mitsubishi	
  Semiconductor	
  America	
  designing	
  
semiconductors	
  and	
  a	
  startup	
  developing	
  a	
  DSP.	
  He	
  then	
  moved	
  into	
  applica%ons	
  
engineering	
  for	
  an	
  Electronic	
  Design	
  Automa%on	
  (EDA)	
  company	
  and,	
  
subsequently,	
  internet	
  soIware	
  companies	
  in	
  CA	
  and	
  Europe.	
  	
  	
  
•  Mark	
  has	
  a	
  bachelors	
  degree	
  in	
  Electrical	
  Engineering	
  and	
  Computer	
  Science	
  from	
  
Duke	
  University,	
  a	
  masters	
  degree	
  in	
  Computer	
  Engineering	
  from	
  North	
  Carolina	
  
State	
  University	
  and	
  an	
  MBA	
  in	
  Quan%ta%ve	
  Finance	
  from	
  the	
  Wharton	
  School	
  of	
  
Business.	
  He	
  recently	
  completed	
  a	
  Cer%ficate	
  in	
  Quan%ta%ve	
  Finance	
  (CQF).	
  
•  He	
  dreams	
  of	
  the	
  day	
  when	
  he	
  can	
  create	
  soIware	
  without	
  encountering	
  a	
  single	
  
type	
  error	
  

Quantitative finance in q

  • 1.
    Quan%ta%ve  Finance  in  q/kdb+   Pricing  Fixed  Income  Deriva%ves   Mark  Lefevre   Quan%ta%ve  Analyst  
  • 2.
    Introduc%on   •  q/kdb+  can  be  used  for  much  more  than   lightning  fast  table  manipula%on   •  Excellent  analy%cal  capabili%es   •  Combining  these  strengths  with  3rd-­‐party   soIware  and/or  linking  with  addi%onal   libraries  provides  excep%onal  opportuni%es  to   analyze,  model  and  predict  
  • 3.
    Mo%va%on   •  q/kdb+  is  oIen  used  in  combina%on  with  MatLab  and  R  for  heavy   quan%ta%ve  finance  work   •  q/kdb+  integrates  well  with  many  foreign  languages  and   applica%ons   –  C,  C#,  Java,  Python,  Perl,  Fortran,  Scala   –  ODBC,  GPUs  (CUDA)   –  Matlab,  R   •  Moving  algorithmic  code  into  q/kdb+  is  not  always  straighWorward   –  Out-­‐of-­‐the-­‐box  q/kdb+  lacks  high-­‐level  sta%s%cs,  op%miza%on,  linear   algebra   –  Algorithmic  code  from  other  languages/environments  usually  was   developed  with  a  different  mindset   •  Let’s  explore  this  topic  by  breaking  in  a  dyadic  manner   1.  Mathema%cs  Fundamentals/Numerical  Analysis   2.  Advanced,  Prac%cal  Applica%on:  Pricing  a  Fixed  Income  Deriva%ve  
  • 4.
    Solving  Systems  of  Linear  Equa%ons   Quick  Review       q)A:3 3#1 3 -2 3 5 6 2 4 3f; q)B:3 1#5 7 8f; q)X:.qml.mls[A;B]; q)X -15 8 2   MATHEMATICS  FUNDAMENTALS  
  • 5.
    Mul%ple  Regression   Least-­‐Squares  (LSQ)  Approxima%on   Quick  Review         q)ingredients:(“FFFF”;”,”) 0:`ingredients.csv; q)heat:”F” $ read0`heat.csv; q)ingredients:flip ingredients; q)heat:13 1#heat; q).qml.mm[.qml.mm[.qml.minv[.qml. mm[flip ingredients;ingredients]];flip ingredients];heat] 2.193046 1.153326 0.7585091 0.4863193   MATHEMATICS  FUNDAMENTALS  
  • 6.
    Problema%c  Linear  Regression   •  One  area  of  focus  in  Numerical  Analysis  is  numerical   stability   •  We  can  demonstrate  a  numerical  stability  problem   using  a  simple  linear  regression  as  an  example   •  Simple  prac%cal  example   –  Car  loans  regressed  on  year  from  U.S  Fed  Reserve   •  References   •  R  tutorial     hbp://www.cyclismo.org/tutorial/R/ linearLeastSquares.html   MATHEMATICS  FUNDAMENTALS  
  • 7.
    Reference  Matlab  Implementa%on   MATHEMATICS  FUNDAMENTALS  
  • 8.
    q/kdb+  Implementa%on   • Live  Demonstra%on   MATHEMATICS  FUNDAMENTALS  
  • 9.
    Demonstra%on  Results   //Problematic Multiple Regression example // Based on R Tutorial Data // http://www.cyclismo.org/tutorial/R/linearLieastSquares.html // // Car Loans regressed on year from U.S. Federal Reserve // http://www.federalreserve.gov/releases/g19/20050805 // l qml.q year:1f cross 2000f+til 5 rate:9.34 8.50 7.62 6.93 6.60f; // Reorient the rate matrix rate:5 1#rate; // Run least squares approximation a:(flip year) $ year; b:inv a; y:(b $ (flip year)) $ rate; x:.qml.mm[.qml.mm[.qml.minv[.qml.mm[flip year;year]];flip year];rate]; show "Year Matrix"; show year; show "Intermediate Result a"; show a; show "Matrix Inversion of a"; show "Ill-conditioned Matrix!"; show "a's 2-norm condition number is 8.0321e+12" show b; show "Results Using q" show y; show "Results Using QML" show x; Welcome  to  kdb+  32bit  edi%on   For  support  please  see  hbp://groups.google.com/d/forum/personal-­‐kdbplus   Tutorials  can  be  found  at  hbp://code.kx.com/wiki/Tutorials   To  exit,  type     To  remove  this  startup  msg,  edit  q.q   "Year  Matrix"   1  2000   1  2001   1  2002   1  2003   1  2004   "Intermediate  Result  a"   5          10010                 10010  2.004003e+07   "Matrix  Inversion  of  a"   "Ill-­‐condi%oned  Matrix!"   "a's  2-­‐norm  condi%on  number  is  8.0321e+12"           "Results  Using  q"       "Results  Using  QML"   1419.208   -­‐0.705     MATHEMATICS  FUNDAMENTALS  
  • 10.
    Problem  Avoidance  by   Input  Transforma%on   // Problematic Multiple Regression example // Based on R Tutorial Data // http://www.cyclismo.org/tutorial/R/linearLieastSquares.html // // Car Loans regressed on year from U.S. Federal Reserve // http://www.federalreserve.gov/releases/g19/20050805 // l qml.q year:1f cross 0f+til 5 rate:9.34 8.50 7.62 6.93 6.60f; // Reorient the rate matrix rate:5 1#rate; // Run least squares approximation a:(flip year) $ year; b:inv a; y:(b $ (flip year)) $ rate; x:.qml.mm[.qml.mm[.qml.minv[.qml.mm[flip year;year]];flip year];rate]; show "Year Matrix"; show year; show "Intermediate Result a"; show a; show "Matrix Inversion of a"; show "a's 2-norm condition number is 22.4555" show b; show "Results Using q" show y; show "Results Using QML" show x; Welcome  to  kdb+  32bit  edi%on   For  support  please  see  hbp://groups.google.com/d/forum/personal-­‐kdbplus   Tutorials  can  be  found  at  hbp://code.kx.com/wiki/Tutorials   To  exit,  type     To  remove  this  startup  msg,  edit  q.q   "Year  Matrix"   1  0   1  1   1  2   1  3   1  4   "Intermediate  Result  a"   5    10   10  30   "Matrix  Inversion  of  a"   "a's  2-­‐norm  condi%on  number  is  22.4555"   0.6    -­‐0.2   -­‐0.2  0.1     "Results  Using  q"   9.208     -­‐0.705   "Results  Using  QML"   9.208     -­‐0.705   MATHEMATICS  FUNDAMENTALS  
  • 11.
    Q  Math  Library   •  qml  is  a  free  library  for  q/kdb+  that  links  into  various   numerical  libraries  including  LAPACK   –  Linear  algebra   –  Sta%s%cs   –  Op%miza%on   •  Compiling  qml  with  32-­‐bit  q/kdb+  on  a  64-­‐bit  machine   requires  pa%ence  and  the  correct  compilers,  but  works   –  LAPACK  is  wriben  in  Fortran  90,  so  you  will  need  a  Fortran  compiler,  as   well  as  the  usual  suspects   References LAPACK http://www.netlib.org/lapack/ qml http://althenia.net/qml  
  • 12.
    Fixed  Income  Deriva%ves   Source:  Bank  of  Interna%onal   Seblements  (BIS)  Quarterly  Review,   June  2015   •  Most  common  fixed   income  deriva%ves   –  Op5ons   •  Caps,  floors,  swaps   –  Futures/Forwards   –  Credit  Default  Swaps   (CDS)   –  Interest  Rate  Swaps   –  Cross  Currency  Swaps   –  Total  Return  Swaps   0   5000   10000   15000   20000   25000   30000   35000   40000   Jun.05   Jun.06   Jun.07   Jun.08   Jun.09   Jun.10   Jun.11   Jun.12   Jun.13   Jun.14   USD  Billions   OTC  Deriva5ves  (No5onal  Outstanding)   Unallocated   Credit  default  swaps   Commodity  contracts   Equity-­‐linked  contracts   Foreign  exchange   contracts   ADVANCED  APPLICATION  
  • 13.
    Pricing  Caps  and  Caplets   •  A  cap  is  an  op%on  on  an   underlying  floa%ng   interest  rate,  such  as   LIBOR,  that  provides   investors  with  a  hedge   against  the  interest  rate   rising  above  the  cap   rate   •  The  cap  is  composed  of   individual  caplets   •  If  the  interest  rate  is   above  the  strike,  the   caplet  is  in-­‐the-­‐money   •  Rate  set  at  %me  t   •  Payoff    at  %me  t+1     •  Present  Value   max​(​ 𝑟↓𝑡 − 𝑘,0)× 𝜏× 𝑁   ​​ 𝐷 𝐹↓𝑂𝐼𝑆 ↓𝑡+1 ×max​(​ 𝑟↓𝑡 − 𝑘,0)× 𝜏× 𝑁   ADVANCED  APPLICATION  
  • 14.
    Heath-­‐Jarrow-­‐Morton  Model   • HJM  models  the  dynamics  of  the   en%re  instantaneous  forward  rate   curve   •  Purple  is  instantaneous  rate  agreed   at  t  for  %me  s   •  Blue  is  a  ZCB  at  that  future  %me   over  the  future  %me  period   •  Green  is  differen%al  and  integral   forms  of  the  dynamics.  Wt  is  GBM   •  Red  is  key  rela%onship  between   driI  (α)  and  vola%lity  (σ)!   •  Widely  used  in  prac%ce   •  Uses  Monte-­‐Carlo  methods  and   discrete  %me  finance   •  This  presenta%on  glosses  over   many  important  implementa%on   details  in  the  interest  of  %me   References hbp//www.columbia.edu/~mh2078/ HJM_models.pdf   𝑓(𝑡, 𝑠)=−​ 𝜕/𝜕𝑠 ​log⁠​ 𝑍↓𝑡↑𝑠     ​ 𝑍↓𝑡↑𝑠 =−∫𝑡↑𝑠▒𝑓(𝑡, 𝑢)𝑑𝑢    𝑑𝑓(𝑡, 𝑠)= 𝛼(𝑡, 𝑠)𝑑𝑡+ 𝜎​( 𝑡, 𝑠)↑𝑇 𝑑​ 𝑊↓𝑡    𝛼(𝑡, 𝑠)= 𝜎​( 𝑡, 𝑠)↑𝑇 ∫𝑡↑𝑠▒𝜎(𝑡, 𝑢)𝑑𝑢    𝑓(𝑡, 𝑠)= 𝑓(0, 𝑠)+∫0↑𝑡▒𝛼(𝑢, 𝑠)𝑑𝑢  +∫0↑𝑡▒𝜎​( 𝑢, 𝑠)↑𝑇 𝑑​ 𝑊↓𝑢     ADVANCED  APPLICATION  
  • 15.
    Historical  Data  Analysis   ADVANCED  APPLICATION  
  • 16.
    Principal  Component  Analysis  (PCA)   •  PCA  is  a  sta%s%cal  procedure   that  u%lizes  orthogonal   transforma%ons  to  convert  a   set  of  observa%ons  of  possibly   correlated  variables  into  a  set   of  values  of  linearly   uncorrelated  variables  called   principal  components.   •  In  a  word,  decorrela(on   •  The  principal  components  are   the  eigenvectors  of  a   symmetric  variance-­‐ covariance  matrix   •  Eigenvectors  are  ordered  by   their  corresponding   eigenvalues,  which  is  also  the   amount  of  variance  explained   by  the  component   •  If  we  take  just  a  few  of  the   first  principal  components,  we   can  achieve   –  dimensionality  reduc5on   –  work  in  linear  parameter   space   •  This  is  very  useful  for   simula%ng  high  dimensionality   problems,  such  as   instantaneous  forward  curve   evolu%ons   ADVANCED  APPLICATION  
  • 17.
    PCA  in  q/kdb+  with  qml   //  Load  q  Math  Library  (qml)   l  qml.q     //  Read  in  historical  JPY  LIBOR  data   histLIBOR:  ("SFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";enlist  ",")   0:`QProject.csv;     //  Calculate  daily  differences   histDeltas:  deltas  delete  Tenor  from  histLIBOR;   delete  from  `histDeltas  where  i  =  0;     //  Define  a  func%on  to  calculate  variance-­‐covariance  matrix   varCov:{a  cov/::  a:flip  x};     //  Calculate  an  annualize  variance-­‐covariance  matrix   histCov:varCov[histDeltas]*252%10000;     //  Extract  eigenvalues  and  eigenvectors  sorted  by  decreasing  modulus   eigen:.qml.mev[value  each  value  histCov];     //  Write  out  data  for  graphing  elsewhere   graphData1:(1  51)#raze  eigen[0];   save  `graphData1.csv   graphData2:(1  51)#sums  eigen[0]%sum  eigen[0];   save  `graphData2.csv   graphData3:(3  51)#eigen[1][%l  3];   save  `graphData3.csv     ADVANCED  APPLICATION  
  • 18.
  • 19.
    Vola%lity  Fi•ng   • In  a  mul%-­‐factor  HJM   framework,  we  need  to   integrate  over  vola%lity   func%ons  of  a  form  like       •  What  this  means  in   English,  is  we  have   several  eigenvalue  / eigenvector  func%ons   driving  the  dynamics  at   par%cular  tenors   •  Consistent  with  Musiela   parameteriza%on   •  This  permits  a  rich   evolu%on  of  forward  rates   exhibi%ng   –  Level  shiIs   –  Twists   –  Buberfly-­‐like  inflec%on   about  specific  tenors   •  Subject  to  regimes   ADVANCED  APPLICATION  
  • 20.
    Monte  Carlo  Simula%on   Random  Number   Genera%on   •  For  each  %mestep,  we   need  3  normally   distributed  random   values   •  Use  the  Box_Muller   transform  as   implemented  in  Nick   Psaris’  Q  Tips   •  Computa%onally  more   intense,  however  beber   accuracy   / box-muller bm:{ if[count[x] mod 2;'`length]; x:2 0N#x; r:sqrt -2f*log first x; theta:2f*acos[-1f]*last x; x: r*cos theta; x,:r*sin theta; x ADVANCED  APPLICATION  
  • 21.
    Pricing   •  Live  Demonstra%on   ADVANCED  APPLICATION  
  • 22.
    Conclusion   •  q/kdb+  can  be  used  for  much  more  than  table   manipula%on   •  Demonstrated  some  basic  mathema%cs  and  an   advanced,  prac%cal  applica%on  to  pricing  caplets   •  Used  3rd-­‐party  soIware  for  visualiza%on,  however  the   new  HTML5  capabili%es  hold  lots  of  promise   •  Linked  to  and  leveraged  industrial-­‐strength   mathema%cs  libraries   •  Hopefully,  this  has  provide  a  glimpse  at  the  excep%onal   opportuni%es  to  analyze,  model  and  predict  that  q/kdb +  can  provide  
  • 23.
    About  Me   • Mark  is  currently  consul%ng  at  one  of  the  largest  banks  in  Tokyo  as  a  quan%ta%ve   analyst  developing  high-­‐performance  algorithmic  trading  systems  on  the  e-­‐FX   desk.  Prior  to  moving  to  Japan,  he  worked  in  London  for  Unicredit  on  the  Equity-­‐ Linked  Origina%on  desk  crea%ng  conver%ble  bonds  for  European  corporates,   consulted  in  the  US  on  e-­‐commerce  analy%cs  and  worked  for  several  high-­‐tech   soIware  companies.   •  Earlier  in  his  career,  he  worked  for  Mitsubishi  Semiconductor  America  designing   semiconductors  and  a  startup  developing  a  DSP.  He  then  moved  into  applica%ons   engineering  for  an  Electronic  Design  Automa%on  (EDA)  company  and,   subsequently,  internet  soIware  companies  in  CA  and  Europe.       •  Mark  has  a  bachelors  degree  in  Electrical  Engineering  and  Computer  Science  from   Duke  University,  a  masters  degree  in  Computer  Engineering  from  North  Carolina   State  University  and  an  MBA  in  Quan%ta%ve  Finance  from  the  Wharton  School  of   Business.  He  recently  completed  a  Cer%ficate  in  Quan%ta%ve  Finance  (CQF).   •  He  dreams  of  the  day  when  he  can  create  soIware  without  encountering  a  single   type  error