MATLAB Seminar for South African Reserve Bank

Size: px
Start display at page:

Download "MATLAB Seminar for South African Reserve Bank"

Transcription

1 MATLAB Seminar for South African Reserve Bank Nicole Beevers Application Engineer 2017 The MathWorks, Inc. 1

2 Agenda Introduction: Welcome from Opti-Num Solutions (15 mins) Part 1: Introduction to MATLAB for Central Banks (60 mins) - Use of MATLAB in Central Banks - Data processing and visualization - Time-series modeling - Getting started with Machine Learning BREAK (15 mins) Part 2: Building Scalable, Sharable Economic Modeling Frameworks with MATLAB (60 mins) - Writing robust and sharable code - Speeding up operations with parallel computing - Scaling and sharing models across the enterprise Q & A (15 mins) 2

3 Agenda Introduction: Welcome from Opti-Num Solutions (15 mins) Part 1: Introduction to MATLAB for Central Banks (60 mins) - Use of MATLAB in Central Banks - Data processing and visualization - Time-series modeling - Getting started with Machine Learning BREAK (15 mins) Part 2: Building Scalable, Sharable Economic Modeling Frameworks with MATLAB (60 mins) - Writing robust and sharable code - Speeding up operations with parallel computing - Scaling and sharing models across the enterprise Q & A (15 mins) 3

4 MATLAB usage across the industry 5

5 What is MATLAB used for in Central Banks? Monetary Analysis & Macroeconomic Research Traditional linear approaches (Econometrics Toolbox) DSGE/simulation approaches (IRIS/ Dynare) Financial Stability Systemic Risk Prudential Assessment Conduct (market monitoring, fraud detection) Markets Currency/ Reserve/ Risk Management Debt Management Microeconomic Analysis 6

6 Computational Finance Workflow Access Files Research and Quantify Data Analysis & Visualization Share Reporting Databases Financial Modeling Applications Datafeeds Application Development Production Automate 7

7 Demo: Data Analysis and Visualization 8

8 What can you do with Econometrics Toolbox? Transform and difference data to ensure stationarity Select and fit ARIMA, GARCH, VAR and state space models Compute model diagnostics to test model sufficiency Forecast, simulate and shock models 9

9 Box Jenkins Methodology Steps to create a model for time series data Make the data stationary Specify models Estimate parameters Compute model diagnostics Forecast & simulate How can Econometrics Toolbox Help? - One-line tests for stationarity - Easily transform data to remove stationarity - Built-in functionality for ARIMA, GARCH and VAR models - Estimate parameters, infer residuals, simulate and forecast in 1 line 10

10 Selecting a Model Which do you choose? Univariate Model Series must be stationary One series at a time Conditional Mean ARIMA Autoregressive Integrated Moving Average GARCH Generalized Autoregressive conditional heteroskedasticity 11

11 1. Validation of stationarity Econometrics Toolbox Make the Is the series stationary? Augmented Dickey-Fuller test adftest Phillips-Perron test pptest KPSS test (trend stationarity) kpsstest Do ARCH/GARCH effects exist? Engle test archtest Ljung-Box Q-test lbqtest Hypothesis Tests: [h, pvalue] = XYZtest(y); data stationary h = 1: Reject null hypothesis h = 0: Fail to reject null hypothesis 12

12 Concepts What does it mean to be stationary? Make the data stationary Stationary: mean and standard deviation are finite and constant over time Trend stationary: The mean trend is deterministic. Difference stationary: The mean trend is stochastic (can difference the series to become stationary) What does it meant to be nonstationary but have unit root? Can difference N times to create stationary process Trend Stationary Difference Stationary Trend Line

13 2. Choose Model Lags Econometrics Toolbox Specify Models AUTOCORR/ PARCORR: autocorr(y) parcorr(y) 14

14 ARIMA model Specify Models AR(1) with constant term y t = δ + a 1 y t 1 + ε t ARIMA(1,1,1) I (integrated) means differential order y t y t 1 = δ +a 1 (y t 1 y t 2 ) + ε t + b 1 ε t 1 MA(1) = moving average y t = ε t + b 1 ε t 1 ARMA(1,1) with constant term y t = δ + a 1 y t 1 + ε t + b 1 ε t 1 15

15 16 GARCH Model Default model: GARCH(1,1) t t t t t A G C y ) (0, ~ t t Q j j t j P i i t i t t M j j t j R i i t i t N A G b y a y Generalized ARCH MA AR A time series technique used to model the serial dependence of volatility. Specify Models

16 Selecting a Model Which do you choose? Vector Autoregressive (VAR) Model multiple (vector) timeseries Captures the linear interdependencies of between timeseries For example, does GDP have an effect on Energy Demand? Specify Models 17

17 6 time series VAR(p) model Specify Models 1. Y t is time series vector. 2. a is constant. y1 y2 y5 y6 t t t t 3. W t is a vector whose average is 0 and covariance matrix Q is random numbers according to 6 dimensional normal distribution. 18

18 MATLAB functions Econometrics Toolbox ARIMA: garchmdl = garch(1,1); Model = arima( D, 1, Seasonality, 12, 'MALags', 1, 'SMALags', 1, 'Variance', garchmdl); Fit = estimate(model, Fin); FY = forecast(fit, 12, 'Y0', Fin); VAR: Model = varm('n', numel(yseries), 'Constant', true, 'nar', nar, 'Series', YSeries); Fit = estimate(model, Y); FY = forecast(fit, Horizon, [], Y, [], NumPaths); 19

19 Econometrics Toolbox Functionality 20

20 Other Toolboxes for Time Series Data Analysis Non-stationary SDE Regression NARX Single data Multiple data Machine Learning Decision tree etc.. Neural Net RNN Multiple linear regression Econometrics Toolbox Statistics & Machine Learning Toolbox Neural Network Toolbox Financial Toolbox Stationary ARIMA GARCH VAR Black:Financial Toolbox Blue:Statistics and Machine Learning Toolbox Brown:Neural Network Toolbox Purple:Econometrics Toolbox 21

21 What is Machine Learning? [Machine Learning] gives computers the ability to learn without being explicitly programmed Arthur Samuel, 1959 Example: Algorithmic Trading Standard Approach Machine Learning Approach Computer Program Buy Hold Sell Machine Learning Buy Hold Sell Hand Written Program If RSI > 70 then SELL If MACD > SIG and RSI <= 70 then HOLD Formula or Equation Y Trade = β 1 X RSI + β 2 X MACD + β 3 X TSMom + model: Inputs Outputs model = < Machine Learning >(factors, trade decision) Algorithm 22

22 Different Types of Machine Learning Machine Learning Supervised Learning Unsupervised Learning Discover a good internal representation E.g. Data Mining Classification Output is a choice between classes E.g. Generating trading signals Machine Learning (RMSE = 0.015) Regression Output is a real number (temperature, stock prices). E.g. Forecasting 23

23 Bonus Demo: Volatility Forecasting with Machine Learning Response is 128-day volatility Performance measured by RMSE Historical Volatility Regression Trees Econometrics Models Neural Networks 24

24 Bonus Demo: Volatility Forecasting with Machine Learning Response is 128-day volatility Performance measured by RMSE 25

25 BREAK Next Building Scalable, Sharable Economic Modeling Frameworks with MATLAB 27

26 Agenda Introduction: Welcome from Opti-Num Solutions (15 mins) Part 1: Introduction to MATLAB for Central Banks (60 mins) - Use of MATLAB in Central Banks - Data processing and visualization - Time-series modeling - Getting started with Machine Learning BREAK (15 mins) Part 2: Building Scalable, Sharable Economic Modeling Frameworks with MATLAB (60 mins) - Writing robust and sharable code - Speeding up operations with parallel computing - Scaling and sharing models across the enterprise Q & A (15 mins) 28

27 Example: Macro Stress Testing Application and Report 29

28 Model-Centered Research and Production Process Our Macro Stress Testing Example PROTOTYPING, MODELLING & ANALYSIS Risk Modeller; Quant; Analyst APPLICATION DEVELOPMENT Developer (ENTERPRISE) IMPLEMENTATION CRO; Risk Manager; Analyst TESTING: VALIDATION AND VERIFICATION 30

29 Challenges Business Challenges Many Use Cases Short Development Timelines Multiple Teams Transparency Reproducibility Technical Challenges Data Aggregation and alignment Scenarios and Models Model Management Validation and Verification Execution Speed Reporting and Access 31

30 Potential Solutions Business Challenges Many Use Cases Short Development Timelines Multiple Teams Transparency Reproducibility Technical Challenges Data Aggregation Scenarios and Models Model Management Validation and Verification Execution Speed Reporting and Access MATLAB Philosophy Single Stack; Easily Repurposed Rapid Development with Formal Methods Common and Bespoke Interfaces to Stack Readable, documented code Encapsulation and Objects, documented code & scripts MATLAB Capabilities Database, Tables, Reading from Multiple Formats and Feeds Proven pre-built tools, Build-Your-Own; Objects Object Oriented Programming Debugging Parallel Computing Report Generator, Interactive Analysis in Spreadsheets, apps Web, Databases 32

31 Technical Challenges Technical Challenges Data Aggregation Scenarios and Models Model Management Validation and Verification Execution Speed Reporting and Access Data Transformation Model Forecast 33

32 Technical Challenges Technical Challenges FEDFunds CPI CPI FEDFunds GDP FEDFunds FEDFunds UNRATE Data Aggregation Scenarios and Models Model Management Validation and Verification Seasonal Seasonal Log Diff Diff Aggregation Reporting and Access VAR Model V VAR Model Y VAR Model X VAR Model W VAR Model Y Forecast - Severe Forecast - Adverse Forecast - Baseline 34

33 Solutions Technical Challenges Data Timeseries Manager Data Aggregation Scenarios and Models Model Management Validation and Verification Execution Speed Transformation Timeseries Transforms Reporting and Access Model VARModels Data fx Actions Forecast Forecasts 35

34 Object-Oriented Programming with MATLAB Full Support for OOP Combines related data and algorithms Class definition files describe object behavior Build on existing classes with inheritance Control access to properties and methods with attributes Monitor object property changes and actions with events and listeners Use matrix-based aspects of MATLAB with objects 36

35 Technical Challenges Technical Challenges Data Aggregation Scenarios and Models Model Management Validation and Verification Execution Speed Reporting and Access Parallel Computing With MATLAB MATLAB client MATLAB workers Time Time 37

36 Technical Challenges Technical Challenges Data Aggregation Scenarios and Models Model Management Validation and Verification Execution Speed Reporting and Access Publish to create self-documented code Convert MATLAB scripts into formatted reports Include annotation, results and figures with code HTML, PDF, XML, DOC, LaTeX, PPT formats Generate custom template-driven reports Interactively lay out report structure Create dynamic content with inline variables, conditions & loops HTML, PDF, XML, DOC, RTF formats Automatic Report Generation with MATLAB 38

37 Enterprise implementation 40

38 Give access to the models to more people Technical Challenges Reporting and Access Excel add-ins Desktop Royalty-free Encryption to protect intellectual property MATLAB Production Server(s) Web Server(s) Web & Enterprise 41

39 Scale up with MATLAB Production Server Most efficient path for creating enterprise applications Deploy MATLAB programs into production Manage multiple MATLAB programs and versions Update programs without server restarts Reliably service large numbers of concurrent requests Integrate with web, database, and application servers MATLAB Production Server(s) HTML XML Java Script The example seen uses RESTful API for the request-response relationship with the server. JSON to represent MATLAB data types. Client code is written JavaScript and embedded in HTML pages. Web Server(s) 42

40 Apply models to more data Prototype Access Explore Share/Deploy Scale Work on the desktop MATLAB Parallel Computing Toolbox Scale capacity as needed MATLAB Distributed Computing Server (MDCS) 47

41 Potential Solutions Business Challenges Many Use Cases Short Development Timelines Multiple Teams Transparency Reproducibility Technical Challenges Data Aggregation Scenarios and Models Model Management Validation and Verification Execution Speed Reporting and Access MATLAB Philosophy Single Stack; Easily Repurposed Rapid Development with Formal Methods Common and Bespoke Interfaces to Stack Readable, documented code Encapsulation and Objects, documented code & scripts MATLAB Capabilities Database, Tables, Reading from Multiple Formats and Feeds Proven pre-built tools, Build-Your-Own; Objects Object Oriented Programming Debugging Parallel Computing Report Generator, Interactive Analysis in Spreadsheets, apps Web, Databases 48

42 Next Steps? 49

43 Training: MATLAB Programming Techniques After this 2-day course you will be able to: Ensure code correctness. Create robust and user-friendly application interfaces. Optimize performance by choosing appropriate function and data types. Create custom toolboxes. for more details 50

44 Agenda Introduction: Welcome from Opti-Num Solutions (15 mins) Part 1: Introduction to MATLAB for Central Banks (60 mins) - Use of MATLAB in Central Banks - Data processing and visualization - Time-series modeling - Getting started with Machine Learning BREAK (15 mins) Part 2: Building Scalable, Sharable Economic Modeling Frameworks with MATLAB (60 mins) - Writing robust and sharable code - Speeding up operations with parallel computing - Scaling and sharing models across the enterprise Q & A (15 mins) 51

45 Q & A 52

46 2017 The MathWorks, Inc. 53