1© 2016 The MathWorks, Inc.
Solving Large Optimization Problems in Finance:
How MATLAB Can Help You
Jorge Paloschi
Consulting Services
MathWorks Spain
2
Optimization
3
Optimization in Financial Applications with MATLAB
Financial Optimization
Optimization Methods
Customized Optimization Models
4
Financial Optimization
5
Financial Applications and Optimization
Asset
Management
Trading
Risk
Management
Portfolio Optimization
Machine Learning
Regression
Maximum Likelihood Estimation
Distribution Fitting
Curve Fitting
ALM
Econometrics
Pricing &
Valuation
6
MATLAB – The Financial Development Platform
Financial
Statistics/Machine Learning Optimization
Financial Instruments Econometrics
MATLAB
Parallel Computing MATLAB Parallel Server
Files
Databases
Datafeeds
Access
Reporting
Applications
Production
Share
Data Analysis and Visualization
Financial Modeling
Application Development
Research and Quantify
Datafeed
Database
Spreadsheet Link EX
MATLAB
Compiler
SDK
MATLAB
Compiler
Rep
ort G
en
era
tor
Pro
du
ctio
n S
erv
er
Trading Toolbox
Deep Learning Risk management
7
– Mean-Variance Portfolio Optimization
– Conditional Value-at-Risk Portfolio Optimization
– Mean-Absolute Deviation Portfolios
– Time Series Regression Models
– Conditional Mean Variance Models
– Multivariate Models
– Linear/ Nonlinear Regression
– Probability disribution fitting
– Machine Learning, e.g., SVM, NN,...
– Nonlinear Regression, Convolutional Neural Networks
Financial Optimization in MATLAB
Financial
Statistics
Econometrics
Neural Network
Optimization
8
Optimization Methods
9
Optimization Problem
Objective Function
uxl
eqbxeqA
bAx
)(min xfx
Typically a linear or nonlinear function
Linear constraints inequalities equalities bounds
Nonlinear constraints inequalities equalities
Decision variables (can be discrete or integer)
0)(
0)(
xeq
c
xc
Subject to Constraints
10
How to solve an optimization problem ?
What do you know about your optimization problem ?
information
accuracy
information
runtime
11
Variables & Constraints
Variables
Continuous Discrete
0 0.2 0.4 0.6 0.8 10
10
20
30
40
50
60
70
80
90
100
0 5 10 15 200
10
20
30
40
50
60
70
80
90
100
Integer Programming
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
-1
-0.5
0
0.5
1
1.5
2
2.5
3
Constraints
Linear Nonlinear
12
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
-1
-0.5
0
0.5
1
1.5
2
2.5
3
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
-2
-1
0
1
2
-2
-1
0
1
2
-8
-6
-4
-2
0
2
4
6
8
10
Objective Function
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
20
40
60
80
100
120
Least-squares/curve fitting
Quadratic f(x) = xTAxLinear f(x) = ATx
General f(x)
f(c)=S[g(xk;c) – yk]2
13
Numerical Optimization
x
xxfxxfxf
2
)()()(
function [f,df] = objective(x)f = ... % function value
df = ... % gradient vector
Fewer function evaluations
More accurate
Whenever possible, provide gradient/hessian information!
14
Derivative-Free Optimization
?f fmincon
Repeatedly sample
several points
Direct Search
Genetic Algorithm
15
Approaches in MATLAB
Local Optimization – Finds local minima/maxima
Uses supplied gradients or estimates them
Applicable for large scale problems with smooth objective function
– Faster/fewer function evaluations
Global Optimization– No gradient information required
– Solve problems with non-smooth, discontinuous objective function
16
Solvers
Variables
Continuous
0 0.2 0.4 0.6 0.8 10
10
20
30
40
50
60
70
80
90
100
0 5 10 15 200
10
20
30
40
50
60
70
80
90
100
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
-1
-0.5
0
0.5
1
1.5
2
2.5
3
Constraints
Linear
NonlinearDiscrete
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
-1
-0.5
0
0.5
1
1.5
2
2.5
3
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
-2
-1
0
1
2
-2
-1
0
1
2
-8
-6
-4
-2
0
2
4
6
8
10
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
20
40
60
80
100
120
Least-squares
Quadratic f(x) = xTAx
Linear f(x) = ATx
General f(x)
f(c)=S[g(xk;c) – yk]2
Objective function Solver
linprog
quadprog
intlinprog
lsqlinlsqnonlin
fminuncfmincon
Optimization Toolbox
17
Solvers
Variables
Continuous
0 0.2 0.4 0.6 0.8 10
10
20
30
40
50
60
70
80
90
100
0 5 10 15 200
10
20
30
40
50
60
70
80
90
100
Constraints
Discrete
Nonlinearf(x)
Objective function Solver
GlobalSearch,
MultiStart
Non-smooth patternsearch,
ga,
simulannealbnd
ga
Global Optimization Toolbox
18
How to Improve Performance?
Derivatives calculations– Gradients
– Hessian matrix
Default approach– Approximation: Finite differences perturbations ( VERY EXPENSIVE! )
Alternative approaches– Cheaper approximations
LBFGS
Supply your own approximation
Analytical calculation
19
Hessian Analytical Calculation
Not easy, in general, to calculate analytical Hessian
Can MATLAB help?
– YES! Symbolic MathToolbox
20
Sample Problem
Problem: Volatility surface estimation for the pricing of
call options. Calibration using market options.
Objective: g(x ) = ||f(x)||2
– fj(x) = C(xj,Kj,Tj) – Cj
K strikes
T maturities
C closing prices
BSM model (Black Scholes and Merton)
21
Numerical results ( problem size 209)
Gradients Function evaluations Time (secs)
Numerical 100,056 1351
Numerical
(using parallel)
100,056 642
Analytical 52 3.41
22
MATLAB Code
%Define symbolic residuals
x = evalin(symengine,['n:=',num2str(n),';[x[j] $ j = 1..n]']);
f = evalin(symengine,['[f[j] $ j = 1..n]']);
for jj=1:n
d1 = (log(S(jj)/K(jj)) +
(r+x(jj)^2/2)*T(jj))/(x(jj)*sqrt(T(jj)));
d2 = d1 - x(jj)*sqrt(T(jj));
val = S(jj)*0.5*(1+erf(d1/sqrt(2))) - ...
K(jj)*exp(-r*T(jj)) * 0.5*(1+erf(d2/sqrt(2)));
f(jj) = (val-CP(jj));
end
obj = f*f.';
23
MATLAB Code
%Define objective gradient and hessian
grad = diff(obj,x);
hess = jacobian(grad,x);
%Transform symbolics into MATLAB function handles
symGrad = matlabFunction(grad);
symHess = matlabFunction(hess);
24
Example of Use
References
– Paloschi J and Krishnamurthy S – Improving MATLAB performance when solving
financial optimization problems – Wilmott Magazine – May 2011
– https://uk.mathworks.com/matlabcentral/fileexchange/33597-improving-matlab-
performance-when-solving-financial-optimization-problems
25
Customized Optimization Models
26
Supported Portfolio Optimization Models
Mean-Variance Portfolio Optimization
Conditional Value-at-Risk Portfolio Optimization
Mean-Absolute Deviation Portfolio Optimization
Financial Toolbox
27
MATLAB – The Financial Development Platform
Financial
Statistics/Machine Learning Optimization
Financial Instruments Econometrics
MATLAB
Parallel Computing MATLAB Parallel Server
Files
Databases
Datafeeds
Access
Reporting
Applications
Production
Share
Data Analysis and Visualization
Financial Modeling
Application Development
Research and Quantify
Datafeed
Database
Spreadsheet Link EX
MATLAB
Compiler
SDK
MATLAB
Compiler
Rep
ort G
en
era
tor
Pro
du
ctio
n S
erv
er
Trading Toolbox
Deep Learning Risk management
28
Customized Portfolio Optimization -
Deployment
Compile your MATLAB
optimization model for your
dedicated platform
Make it available for your
enterprise environment
29
Customer User Stories Involving Optimization in Finance
https://uk.mathworks.com/company/user_stories/search.html?q=&fq=produ
ct:OP%20marketing-industry:financial-services
30
31
32
33
Summary
Optimization for financial applications is built within MATLAB
toolboxes covering many standard applications
A large variety of optimization algorithms available in MATLAB®
Optimization Toolbox™ and Global Optimization Toolbox™
Customized optimization models made easy by
– quick modeling (Math to MATLAB)
– advanced optimization process diagnostics
– rapid deployment
34
Thank you !