# How to use Mupad MATLAB and Symbolic Math Toolbox to Develop and Analyze Financial Models

(Last Updated On: May 1, 2014)

How to use Mupad MATLAB and Symbolic Math Toolbox to Develop and Analyze Financial Models

These are two webinars that are proving to be vital to this algo development

http://www.mathworks.com/videos/using-symbolic-computations-to-develop-efficient-algorithms-and-system-models-81703.html?form_seq=conf798&confirmation_page&wfsid=5563955

http://www.mathworks.com/videos/using-matlab-and-symbolic-math-toolbox-to-develop-and-analyze-financial-models-81684.html

Notes in the first webinar: (this is all part of the Matlab Symbolic Math Toolbox)

Solving symbolically is 1700% speed up numerically. Bond pricing example at 5:15

At 9:15 demos yield curves for bond pricing

18:05 demos the ability to import Excel data into a MU and M script with plots

30:48 demos the HUGE speed improvement using symbollic analysis vs numeric (i.e. back testing in finance)

Can this be done as fast as in R or Python?

Notes for Second webinar:

If you have followed me, the most successful quant/trader researcher that I know did exactly this at 2:31

4:29 explains how you gain the insight of the math vs using Financial Toolbox functions. You understand how the interaction of variables impact your results! You solve the problem once where you solve numerically with a slow iterative approach. So you basically have to start again with each new approach and recompute as well.

Many financial models are expressed analytically but symbolic math can be applied because of that. At 5:25, you can use these as building blocks to create custom models. At 7:25, he explains how easily you can go from symbols (Mupad) to numeric (Matlab interactive in the command line)

16:32 shows ability create a symbolic function using –> in Mupad language 16:39 explains how you can make general expression as functions with Mupad edit from within the Notepad editor. This a Mupad procedure or library (17:09)

18:37 shoes you how to plot in 3D. 19:26 shows animation. 20:47 shows how to add text regions to the Notepad or notebook document.

27:50 introduces solvers to optimize the equations which is part of the Optimization toolbox

32:59 displays the various Symbolic Math Toolbox libraries

Don’t forget you can convert Mupad code into custom Matlab M functions which can be imported into both Simulink and Stateflow.

The R support to do this is VERY LIMITED as explained here: http://cran.r-project.org/web/packages/Ryacas/Ryacas.pdf

It does not include the Notepad editor which Matlab has so it is light years ahead of R.

As for Python, all I could find is : https://code.google.com/p/sympy/

Eeek here are the open issues so this could be severely limited as compared to Matlab Symbolic Toolbox.

https://github.com/sympy/sympy/issues

This also moves into C code generation:

# Generate C Code from MATLAB Functions Using the Embedded MATLAB Subset

http://www.mathworks.com/videos/generate-c-code-from-matlab-functions-using-the-embedded-matlab-subset-81679.html?form_seq=conf1302

Some notes for this webinar:

Uses Kalman Filter to track an object, generate C, and apply the algorithm into Simulink for System level modelling.

At 6:07 he mentioned you can deploy even into Assembler.

At 8:15, he explains how handcoding from Matlab to C/C++ is not easy nor is it scalable. It is also introduces many errors. It minimizes the translation with advantages of visualization and other Matlab features. It is the Embedded Matlab workflow at 10:18.

I do believe this is old but renamed to the Matlab Coder so this webinar is old but there are equivalent functions to handle after Matlab 2009b

27:36 explains Kalman filter M function.

19:01 explains how Kalman Filter could be used to track something like price movements and estimate direction within this webinar.

32:39 explains how you can dynamically create C with elmex function. You can dynamically create C functions based what is in specific Matlab workspace structures. This generates a DLL automatically.

*** 36:59 he shows a trick to embed a Matlab M function C but turning off all functions not supported during the compile process. This includes visualizations function like figure. You declare the function extrinsic. Very cool!

41:32 shows a code generation to C. 43:00 shows entry point to the C file for Kalman. 45:07 proves that the C runs faster than the code generated MEX file.

49:00 described how to change input size of source data. How do you generate C code for this at 51:32? Use emlcoder.egs

51:10 explains how to use fixed point which is needed for Simulink models.  Use fi prefix or sfi signed fi)

1:00.900 explains globalfimath function

1:01:57 shows how to integrate the Kalman into a Simulink model

** You can use Video Viewer Simulink block to watch the animation of the Kalman filter function

Another webinar for HDL for FPGA deployment which is ultra lowest latency HFT deployment:

I am not going to explore at this point but will leave it to Mr FPGA

Join my FREE newsletter on my march learn how to do this algo development for financial models

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!