MATLAB for Use in Finance
Portfolio Optimization (Mean Variance, CVaR & MAD)
Market, Credit, Counterparty Risk Analysis and beyond
Marshall Alphonso [Link]@[Link]
Senior Application Engineer
MathWorks
© 2013 The MathWorks, Inc.1
Agenda
Introduction: Knowing your risk
Overview of the MATLAB Solution
– Connect to financial data sources
– Perform financial modeling & analysis
– Share results & deploy applications
Finance Application Examples
– Portfolio Construction using Frameworks
– Evaluating Risks
– Constructing your own Portfolio Management Framework
2
Challenges in Financial Analysis
Financial
Volatile markets Engineer
Quant Group
– Ever-changing needs
Lack of computing power
Traders
– Large data, large models Management
Increased transparency Other groups
– More auditing and regulation
– More sharing with colleagues
Regulators Clients Partners 3
Challenges Faced During Model Development
Traditional Approach Challenge
Off-the-shelf software Inability to customize
Third-party consulting Lack of transparency
Subpar computational
Spreadsheets, Excel
speed, limited data size
In-house development with
Long development time
traditional languages
Inefficiencies in
Combination of the above
Integration & Automation
4
Who uses MATLAB in Financial Services?
The top 15 asset-
management companies
9 of the top 10 U.S.
commercial banks
12 of the top 15 hedge funds
The reserve banks of all
OECD member countries
The top 3 credit rating
agencies
5
Agenda
Introduction: Knowing your risk
Overview of the MATLAB Solution
– Connect to financial data sources
– Perform financial modeling & analysis
– Share results & deploy applications
Finance Application Examples
– Macroeconomic modeling & forecasting
– Cash flow hedging & scenario analysis
– Credit risk assessment
6
Computational Finance Workflow
Access Research and Quantify Share
Files Reporting
Data Analysis
& Visualization
Financial
Databases Applications
Modeling
Application
Development
Datafeeds Production
Automate
7
Portfolio Optimization
-3
x 10 Efficient Frontier
3.5
Mean of Portfolio Returns
3
2.5
1.5
1
0.005 0.01 0.015 0.02
Standard Deviation of Portfolio Returns
Efficient Frontier
0.35
Mean of Portfolio Returns
0.3
0.25
0.2
0.15
0.1
0.005 0.01 0.015 0.02
Standard Deviation of Portfolio Returns
8
Modeling Market Risk Factor Time Series
Need model that captures:
Correlation & overall volatilities
Time-varying volatility
Fat-tailed distributions
Candidate model: Geometric Brownian Motion
Correlation & overall volatilities
Time-varying volatility
Fat-tailed distributions
9
Market Risk Using Copulas, GARCH & EVT
“Forecasting risk factors”
Need model that captures:
Time-varying volatility GARCH
Fat-tailed distributions Generalized Pareto
Factor Correlations Copulas
10
Market Risk Using Copulas & Extreme Value Theory
“Asymmetric GARCH using a GJR Model”
Model returns using a asymmetric GARCH Model
Estimate a Marginal CDF – Kernel + Pareto Tails
Fit a Student-T Copula and induce correlation amongst
the simulated residuals
Estimate a VaR & CVaR
11
The GARCH Model
“Non-symmetric, Non-Gaussian residuals”
R M
yt C i yt i j t j t
AR i 1 j 1 ARMA
P Q
Gi
t
2 2
t i A j t2 j
i 1 j 1
t t zt
zt ~ N (0,1) GARCH ARCH
12
Credit Classification
“Logistic Regression Vs. TreeBagger”
Logistic: [Training Set] Actual Vs Estimated Credit Ratings
CCC 2.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 15.00%
B 1.43% 3.71% 2.29% 0.00% 0.00% 0.00% 0.00%
BB 0.00% 4.29% 16.29% 5.14% 0.29% 0.00% 0.00% 10.00%
BBB 0.00% 0.00% 3.71% 12.86% 3.71% 0.00% 0.00%
A 0.00% 0.00% 0.00% 1.71% 9.14% 4.00% 0.00%
5.00%
AA 0.00% 0.00% 0.00% 0.00% 2.86% 9.14% 1.14%
AAA 0.00% 0.00% 0.00% 0.00% 0.00% 1.43% 14.86%
0.00%
C
A
BB
AA
B
A
CC
BB
AA
Logistic: [Testing Set] Actual Vs Estimated Credit Ratings
CCC 2.65% 0.39% 0.00% 0.00% 0.00% 0.00% 0.00%
15.00%
B 1.09% 2.40% 1.06% 0.00% 0.00% 0.00% 0.00%
BB 0.00% 5.70% 17.06% 5.42% 0.03% 0.00% 0.00%
10.00%
BBB 0.00% 0.00% 3.07% 14.57% 3.38% 0.00% 0.00%
A 0.00% 0.00% 0.00% 2.21% 10.36% 1.62% 0.00%
5.00%
AA 0.00% 0.00% 0.00% 0.00% 2.18% 10.16% 1.01%
AAA 0.00% 0.00% 0.00% 0.00% 0.00% 1.76% 13.90%
0.00%
C
A
BB
AA
B
A
CC
BB
AA
>> doc mnrfit Popular in classifying the credit quality of instruments is the use of logistic
>> doc mnrval regression. This example illustrates this classification but takes a step
>> doc treebagger further by using a Tree Bagger classifier.
>> doc confusionmat
13
Credit Value Adjustment – Expected loss on portfolio
Yield Curve Evolution
0.1
0.08
Rates
0.06
0.04
Jan15 300
Jul12 200
Jan10 100
Jul07 Tenor (Months)
Observation Date
[Section 3|EX 1] 14
Evaluate Counterparty Exposures
>> doc IRDataCurve
>> doc hwv
CVA for each counterparty
6000
5000
4000
CVA $
3000
2000
1000
0
1 2 3 4 5
Counterparty
[Section 3 | EX 1] 15
Connect to Data from Various Sources
Excel spreadsheets and flat
files
ODBC or JDBC compliant
databases
Data feeds including
Bloomberg, Reuters,
Factset®, eSignal® and
others
Web services (SOAP)
16
Leverage Built-in Functions to Save Time
Mathematics
Regression (Linear, Non-Linear)
Curve Fitting
Probability Distributions, RNG
Clustering
Multivariate & Factor Analysis
Predictive Modeling, AI
Optimization, Parameter Estimation
Financial Modeling
Portfolio Optimization & Analysis
Derivative Pricing & Hedging
Yield Curve Modeling
Monte Carlo Simulation
Risk Quantification
ARMA/GARCH Analysis
17
Sharing MATLAB Applications
MATLAB
MATLAB MATLAB
Compiler Compiler SDK
MATLAB
MATLAB .exe Excel
Hadoop Production
C/C++ Java .NET
Server
Share applications with those who do not need MATLAB
Royalty free
MATLAB Production Server provides most efficient path
for secure and scalable enterprise applications
18
Financial Modeling Workflow
Access Research and Quantify Share
Files
Files Data Analysis and Visualization Reporting
Reporting
Databases Data Analysis
Financial Modeling Applications
& Visualization
Datafeeds Application Development Production
Financial
Databases Applications
Modeling
Spreadsheet Link EX Financial Instruments Econometrics
Report Generator
Production Server
Database Financial
MATLAB Compiler
Application SDK
Datafeed Statistics & Machine
Development Optimization
Datafeeds Learning Production
MATLAB Compiler
Trading
MATLAB
Parallel Computing MATLAB Distributed Computing Server
Automate
19
Deploying MATLAB Applications to Excel
3
Toolboxes
1 MATLAB End-User
Desktop Machine
MATLAB Compiler
2
MATLAB
Builder EX
.bas/
.dll .xla
20
Production Deployment Workflow
Development MATLAB Algorithm
Developer
MATLAB
Compiler
CTF
Enterprise
Application Web
Developer Application MATLAB Production Server
Client Function Call
Library
Production
MATLAB Production Server
Web
.. Application
.
Client
Library
21
Value of MATLAB Production Server
Directly deploy MATLAB programs into production
– Supports multiple MATLAB programs and MCR versions
Scalable & reliable MATLAB Production Server(s)
– Service large numbers of concurrent requests
– Add capacity or redundancy with additional servers
Use with web, database & application servers HTML
XML
– Lightweight client library isolates MATLAB processing Web
Java Script
Server(s)
22
CAMRADATA Models Dependencies for
Quantitative Risk Assessment with
MathWorks Tools
Challenge
Rapidly develop quantitative tools for factor analysis,
risk analysis, and defensive asset allocation Risk-assessment model developed in
MATLAB.
Solution
Use MATLAB to model complex non-linear dependencies
between assets, liabilities, and economic variables using
copulas
Results
Development time reduced by 90 percent “Using MATLAB we can build a
Risk calculated in hours, not weeks model in one morning. It would
Diverse skill sets leveraged
take two weeks to write the
equivalent code in Visual Basic.”
Martyn Dorey
CAMRADATA
Link to user story
23
Capgemini Helps Clients Achieve Basel II
Compliance and Deliver Economic Capital,
Risk, and Valuation Models with MATLAB
Challenge
Enable banking clients to meet Basel II regulatory
guidelines and perform other risk management tasks
Scatterplots showing 500,000 simulations
drawn from bivariate t-copulas with the same
Solution correlation coefficient but differing degrees
Use MATLAB to develop risk management models and of freedom.
to perform valuations of complex products
Results
Strong competitive advantage established “With its computational power, matrix
Scalable solution delivered infrastructure, and ability to perform
Customer portfolio revalued Monte Carlo simulations, MATLAB gives
us a competitive advantage in
performing complex risk analyses."
Dr. Marco Folpmers
Capgemini
Link to user story
24
Intuitive Analytics Uses MATLAB to Build
Quantitative Tools to Help Bond Issuers
Manage Risk
Challenge
Build and market a quantitative tool for reducing Using MATLAB technical computing
software to provide visual representations
expected cost and risk for municipal bond issuers of interest rate models.
Solution
Use MathWorks tools to develop algorithms, visualize
results, and simplify deployment of an advanced
analytical tool
“Because MATLAB enables us to
Results build and distribute applications to
Development productivity increased by 90%
analysts that are accessible from
Deployment simplified
Excel, we are quickly bringing to
Visual environment created
market products that are adopted
and deployed by investment banks.”
Peter Orr
Intuitive Analytics
Link to user story
25
IPD Develops and Deploys Real
Estate Cash Flow Models with
MathWorks Tools 25%
20%
15% '06
10%
5% '07
0%
'08
2
Challenge 4 6 8 10 12 14 16 18
Create cash flow models of real estate investment 0-0.05 0.05-0.1 0.1-0.15 0.15-0.2 0.2-0.25
portfolios and project returns using Monte Carlo MATLAB graph generated to indicate how total returns
simulations from industrial property are likely to behave.
Solution
Use MATLAB and MATLAB Builder NE to develop “The only other approach we
optimization algorithms, build financial models, and seriously considered involved
deploy solutions developing a class library in
Results .NET and C#. Development,
Development time cut by 16 weeks debugging, and testing would
Updates completed in hours have taken us 37 weeks. Using
Deployment simplified MATLAB, we completed the
project in 21 weeks.”
Peter McAnena
Investment Property Databank
Link to user story
26
Nykredit Develops Risk Management and
Portfolio Analysis Applications to Minimize
Operational Risk
Challenge
Nykredit’s tool for calculating and
Enable financial analysts to make rapid, fact-based visualizing risk statistics. The plot
shows portfolio expected tracking
decisions by providing them with direct access to risk error broken out by industry.
management and portfolio analysis information
Solution “Data handling, programming, debugging,
Develop and deploy easy-to-use graphical financial and plotting are much easier in MATLAB,
analysis applications using MATLAB and MATLAB where everything is in one environment.
Compiler For performance calculation GUIs,
Results MATLAB provides a real error-checked
Productivity increased threefold application that makes cool customized
Operational risk mitigated plots for client reports. This has turned a
Analysis time reduced from days to hours several-hour task in a spreadsheet into a
two-minute no-brainer.”
Peter Ahlgren
Nykredit Asset Management
Link to user story
27
Macroeconomic Modeling and Inflation
Rate Forecasting at the Reserve Bank of
New Zealand
Challenge
Support New Zealand monetary policy with a
Sample fancharts produced by RBNZ’s
theoretically well-founded model macroeconomic model.
Solution
Use MATLAB to analyze and forecast macroeconomic
variables, and communicate results to stakeholders
Results
Entire workflow completed in a single environment “With all RBNZ models now
Code shared with other central banks and implemented in MATLAB, the
financial institutions RBNZ has a common platform
Technical rigor of macroeconomic forecasting for evaluating the economy and
increased making informed decisions.”
Jaromir Benes
International Monetary Fund
Link to article
28
Robeco Develops Quantitative Stock
Selection and Portfolio Optimization
Models with MathWorks Tools
Challenge
Develop, distribute, and maintain quantitative
tools for portfolio construction and management
Interest rate paths for the risk analysis of a
Solution savings product.
Use MATLAB and MATLAB Builder NE to develop
algorithms, build quantitative models, and deploy
solutions “Unlike companies that rely on off-the-
Results shelf quantitative analysis solutions, we
Applications updated faster can see our process improving all the
Black-box solutions eliminated time. We have the flexibility to
Scalability and flexibility increased continuously improve our algorithms
and models in MATLAB—and that is a
big advantage.”
Willem Jellema
Robeco
Link to user story
29
MATLAB Solutions
Challenge Solution
Flexible modeling
Inability to customize
Complete development environment
White-box modeling
Lack of transparency
Viewable-source functions
Subpar computational Powerful computation engine
speeds Run fast Monte-Carlo simulations
Quick prototyping
Long development time
Focus on modeling not programming
Inefficiencies in Easy to Integrate & Deploy
Integration Point-and-click workflow
30
Questions
31