Introduction xiii
Why You Should Read This Book xiii
The Intended Reader xiv
Why MATLAB®? xiv
How to Use This Book xvi
Font Conventions xvi
About the Author xvii
MathWorks Information xviii
References xviii
Part I MATLAB Conventions and Basic Skills 1
Chapter 1 Working with MATLAB® Data 3
1.1 Introduction 3
1.2 Arrays 3
1.2.1 Numerical Arrays 4
1.2.2 Math Calculations with Scalars,Vectors, and Matrices 10
1.2.3 Statistical Calculations on Vectors and Matrices 16
1.2.4 Extracting Values from Numerical Vectors and Matrices 19
1.2.5 Counting Elements 26
1.2.6 Sorting Vectors and Matrices 28
1.2.7 Relational Expressions and Logical Arrays 31
1.2.8 Dealing with NaNs (Not a Number) 35
1.2.9 Dealing with Missing Data 39
1.3 Character Arrays 40
1.3.1 String Arrays 44
1.4 Flexible Data Structures 46
1.4.1 Cell Arrays 47
1.4.2 Structure (“struct”) Arrays 49
1.4.3 Tables 51
References 60
Further Reading 60
Chapter 2 Working with Dates and Times 61
2.1 Introduction 61
2.2 Finance Background: Why Dates and Times Matter 61
2.2.1 First Challenge: Day Count Conventions 62
2.2.2 Second Challenge: Date Formats 63
2.3 Dates and Times in MATLAB 64
2.3.1 Datetime Variables 64
2.3.2 Date Conversions 73
2.3.3 Date Generation Functions with Serial Number Outputs 79
2.3.4 Duration Arrays 83
2.3.5 Calendar Duration Variables 86
2.3.6 Date Calculations and Operations 89
2.3.7 Plotting Date Variables Introduction 94
References 95
Chapter 3 Basic Programming with MATLAB® 97
3.1 Introduction 97
3.1.1 Algorithms 101 97
3.1.2 Go DIY or Use Built-In Code? 98
3.2 MATLAB Scripts and Functions 99
3.2.1 Scripts 99
3.2.2 Developing Functions 106
3.2.3 If Statements 112
3.2.4 Modular Programming 115
3.2.5 User Message Formats 121
3.2.6 Testing and Debugging 124
References 127
Chapter 4 Working with Financial Data 129
4.1 Introduction 129
4.2 Accessing Financial Data 129
4.2.1 Closing Prices versus Adjusted Close Prices for Stocks 130
4.2.2 Data Download Examples 131
4.2.3 Importing Data Interactively 133
4.2.4 Automating Data Imports with a Script 138
4.2.5 Automating Data Imports with a Function 140
4.2.6 Importing Data Programmatically 147
4.3 Working with Spreadsheet Data 154
4.3.1 Importing Spreadsheet Data with Import Tool 154
4.3.2 Importing Spreadsheet Data Programmatically 154
4.4 Data Visualization 156
4.4.1 Built-In Plot Functions 156
4.4.2 Using the Plot Tools 158
4.4.3 Plotting with Commands 159
4.4.4 Other Plot Tools 162
4.4.5 Built-In Financial Charts 173
References 176
Part II Financial Calculations with MATLAB 177
Chapter 5 The Time Value of Money 179
5.1 Introduction 179
5.2 Finance Background 180
5.2.1 Future Value with Single Cash Flows 180
5.2.2 Future Value with Multiple Cash Flows 185
5.2.3 Present Value with Single Cash Flows 187
5.2.4 Present Value with Multiple Variable Cash Flows 188
5.3 MATLAB Time Value of Money Functions 189
5.3.1 Future Value of Fixed Periodic Payments 190
5.3.2 Future Value of Variable Payments 191
5.3.3 Present Value of Fixed Payments 193
5.3.4 Present Value of Variable Payments 194
5.4 Internal Rate of Return 197
5.5 Effective Interest Rates 198
5.6 Compound Annual Growth Rate 198
5.7 Continuous Interest 200
5.8 Loans 200
References 202
Chapter 6 Bonds 203
6.1 Introduction 203
6.2 Finance Background 204
6.2.1 Bond Classifications 204
6.2.2 Bond Terminology 205
6.3 MATLAB Bond Functions 206
6.3.1 US Treasury Bills 206
6.3.2 Bond Valuation Principles 208
6.3.3 Calculating Bond Prices 209
6.3.4 Calculating Bond Yields 212
6.3.5 Calculating a Bond’s Total Return 214
6.3.6 Pricing Discount Bonds 216
6.4 Bond Analytics 216
6.4.1 Interest Rate Risk 217
6.4.2 Measuring Rate Sensitivity 219
6.4.3 Yield Curves 227
6.5 Callable Bonds 229
References 231
Further Reading 231
Chapter 7 Dealing with Uncertainty and Risk 233
7.1 Introduction 233
7.2 Overview of Financial Risk 234
7.3 Data Insights 234
7.3.1 Visualizing Data 235
7.3.2 Basic Single Series Plots 237
7.3.3 Basic Multiple Series Plots 237
7.3.4 Adding Plot Customization 238
7.3.5 Histograms 239
7.3.6 Measures of Central Location 241
7.3.7 Measures of Data Dispersion 243
7.4 Data Relationships 249
7.4.1 Covariance and Correlation 251
7.4.2 Correlation Coefficients 252
7.5 Creating a Basic Simulation Model 253
7.6 Value at Risk (VaR) 258
References 261
Further Reading 262
Chapter 8 Equity Derivatives 263
8.1 Introduction 263
8.2 Options 264
8.2.1 Option Quotes 265
8.2.2 Market Mechanics 266
8.2.3 Factors in Option Valuation 267
8.3 Option Pricing Models 268
8.3.1 Arbitrage 269
8.3.2 Binomial Option Pricing 270
8.3.3 Black-Scholes 274
8.4 Options’ Uses 276
8.4.1 Hedging 277
8.4.2 Speculation and Leverage 277
8.4.3 Customizing Payoff Profiles 278
8.5 Appendix: Other Types of Derivatives 279
8.5.1 Commodity and Energy 279
8.5.2 Credit 279
8.5.3 Exotic Options 280
References 281
Further Reading 281
Chapter 9 Portfolios 283
9.1 Introduction 283
9.2 Finance Background 283
9.3 Portfolio Optimization 285
9.4 MATLAB Portfolio Object 286
9.4.1 Object-Oriented Programming (OOP) 286
9.4.2 A Basic Example 287
9.4.3 Using Data Stored in a Table
Format 294
References 296
Chapter 10 Regression and Time Series 297
10.1 Introduction 297
10.2 Basic Regression 297
10.2.1 Understanding Least Squares 300
10.2.2 Model Notation 301
10.2.3 Fitting a Polynomial with polyfit and polyval 303
10.2.4 Linear Regression Methods 305
10.3 Working with Time Series 308
10.3.1 Step 1: Load the Data (Single Series) 308
10.3.2 Step 2: Create the FTS Object 309
10.3.3 Step 3: Using FTS Tools 311
References 314
Appendix 1 Sharing Your Work 315
A1.1 Introduction 315
A1.2 Publishing a Script 316
A1.2.1 Publishing with Code Sections 317
A1.2.2 futureValueCalc3 319
A1.2.3 Formatting Options 321
A1.2.4 Working with Live Scripts 322
A1.2.5 Editing and Control 325
References 326
Appendix 2 Reference for Included MATLAB® Functions 327
Index 335