ebook img

C++ for financial mathematics PDF

411 Pages·2017·3.894 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ for financial mathematics

++ C for Financial Mathematics CHAPMAN & HALL/CRC Financial Mathematics Series Aims and scope: The field of financial mathematics forms an ever-expanding slice of the financial sector. This series aims to capture new developments and summarize what is known over the whole spectrum of this field. It will include a broad range of textbooks, reference works and handbooks that are meant to appeal to both academics and practitioners. The inclusion of numerical code and concrete real- world examples is highly encouraged. Series Editors M.A.H. Dempster Dilip B. Madan Rama Cont Centre for Financial Research Robert H. Smith School Department of Mathematics Department of Pure of Business Imperial College Mathematics and Statistics University of Maryland University of Cambridge Published Titles American-Style Derivatives; Valuation and Computation, Jerome Detemple Analysis, Geometry, and Modeling in Finance: Advanced Methods in Option Pricing, Pierre Henry-Labordère C++ for Financial Mathematics, John Armstrong Commodities, M. A. H. Dempster and Ke Tang Computational Methods in Finance, Ali Hirsa Counterparty Risk and Funding: A Tale of Two Puzzles, Stéphane Crépey and Tomasz R. Bielecki, With an Introductory Dialogue by Damiano Brigo Credit Risk: Models, Derivatives, and Management, Niklas Wagner Engineering BGM, Alan Brace Financial Mathematics: A Comprehensive Treatment, Giuseppe Campolieti and Roman N. Makarov The Financial Mathematics of Market Liquidity: From Optimal Execution to Market Making, Olivier Guéant Financial Modelling with Jump Processes, Rama Cont and Peter Tankov Interest Rate Modeling: Theory and Practice, Lixin Wu Introduction to Credit Risk Modeling, Second Edition, Christian Bluhm, Ludger Overbeck, and Christoph Wagner An Introduction to Exotic Option Pricing, Peter Buchen Introduction to Risk Parity and Budgeting, Thierry Roncalli Introduction to Stochastic Calculus Applied to Finance, Second Edition, Damien Lamberton and Bernard Lapeyre Monte Carlo Methods and Models in Finance and Insurance, Ralf Korn, Elke Korn, and Gerald Kroisandt Monte Carlo Simulation with Applications to Finance, Hui Wang Nonlinear Option Pricing, Julien Guyon and Pierre Henry-Labordère Numerical Methods for Finance, John A. D. Appleby, David C. Edelman, and John J. H. Miller Option Valuation: A First Course in Financial Mathematics, Hugo D. Junghenn Portfolio Optimization and Performance Analysis, Jean-Luc Prigent Quantitative Finance: An Object-Oriented Approach in C++, Erik Schlögl Quantitative Fund Management, M. A. H. Dempster, Georg Pflug, and Gautam Mitra Risk Analysis in Finance and Insurance, Second Edition, Alexander Melnikov Robust Libor Modelling and Pricing of Derivative Products, John Schoenmakers Stochastic Finance: An Introduction with Market Examples, Nicolas Privault Stochastic Finance: A Numeraire Approach, Jan Vecer Stochastic Financial Models, Douglas Kennedy Stochastic Processes with Applications to Finance, Second Edition, Masaaki Kijima Stochastic Volatility Modeling, Lorenzo Bergomi Structured Credit Portfolio Analysis, Baskets & CDOs, Christian Bluhm and Ludger Overbeck Understanding Risk: The Theory and Practice of Financial Risk Management, David Murphy Unravelling the Credit Crunch, David Murphy Proposals for the series should be submitted to one of the series editors above or directly to: CRC Press, Taylor & Francis Group 3 Park Square, Milton Park Abingdon, Oxfordshire OX14 4RN UK ++ C for Financial Mathematics John Armstrong King’s College London, Strand, UK CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2017 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed on acid-free paper Version Date: 20161202 International Standard Book Number-13: 978-1-4987-5005-9 (Hardback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information stor- age or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copy- right.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that pro- vides licenses and registration for a variety of users. For organizations that have been granted a photo- copy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents Introduction xvii 1 Getting Started 1 1.1 Installing your development environment . . . . . . . . . . 1 1.1.1 For Windows . . . . . . . . . . . . . . . . . . . . . . 1 1.1.2 For Unix . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.3 For MacOS X . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Running an example program . . . . . . . . . . . . . . . . . 2 1.3 Compiling and running the code . . . . . . . . . . . . . . . 3 1.3.1 Compiling on Windows . . . . . . . . . . . . . . . . . 4 1.3.2 Compiling on Unix . . . . . . . . . . . . . . . . . . . 6 1.4 Understanding the example code . . . . . . . . . . . . . . . 8 1.5 Configuring the compiler . . . . . . . . . . . . . . . . . . . . 12 1.6 Making decisions . . . . . . . . . . . . . . . . . . . . . . . . 13 1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 Basic Data Types and Operators 17 2.1 Memory terminology . . . . . . . . . . . . . . . . . . . . . . 17 2.2 Basic data types . . . . . . . . . . . . . . . . . . . . . . . . 18 2.2.1 Integers . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.2.2 Floating point numbers . . . . . . . . . . . . . . . . . 20 2.2.3 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.2.4 Characters . . . . . . . . . . . . . . . . . . . . . . . . 20 2.3 Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.4 Memory addresses . . . . . . . . . . . . . . . . . . . . . . . 26 2.5 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.5.1 The sizeof operator . . . . . . . . . . . . . . . . . . 28 2.5.2 Mathematical operations . . . . . . . . . . . . . . . . 28 2.5.3 Comparison operators . . . . . . . . . . . . . . . . . 29 2.5.4 Logical operators . . . . . . . . . . . . . . . . . . . . 29 2.5.5 Bitwise operators . . . . . . . . . . . . . . . . . . . . 29 2.5.6 Combining operators . . . . . . . . . . . . . . . . . . 30 2.5.7 Assignment operators . . . . . . . . . . . . . . . . . . 30 vii viii Contents 2.5.8 If statements revisited . . . . . . . . . . . . . . . . . 32 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3 Functions 37 3.1 The C++ function syntax . . . . . . . . . . . . . . . . . . . 37 3.2 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.3 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.4 Declaring and defining functions . . . . . . . . . . . . . . . 42 3.5 Functions that don’t return a value . . . . . . . . . . . . . . 44 3.6 Specifying default values . . . . . . . . . . . . . . . . . . . . 45 3.7 Overloading functions . . . . . . . . . . . . . . . . . . . . . 46 3.8 Global and local variables . . . . . . . . . . . . . . . . . . . 47 3.9 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4 Flow of Control 55 4.1 while loops . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.2 do-while loops . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.3 for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.4 break, continue, return . . . . . . . . . . . . . . . . . . . 60 4.5 throw statements . . . . . . . . . . . . . . . . . . . . . . . . 61 4.6 switch statements . . . . . . . . . . . . . . . . . . . . . . . 63 4.7 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.8 Flow of control in operators . . . . . . . . . . . . . . . . . . 65 4.8.1 Short circuit evaluation . . . . . . . . . . . . . . . . . 66 4.8.2 The ternary operator . . . . . . . . . . . . . . . . . . 66 4.8.3 The comma operator . . . . . . . . . . . . . . . . . . 67 4.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5 Working with Multiple Files 71 5.1 The project FMLib . . . . . . . . . . . . . . . . . . . . . . . 71 5.2 Header files . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.3 Creating our project . . . . . . . . . . . . . . . . . . . . . . 73 5.3.1 Creating the first header file . . . . . . . . . . . . . . 73 5.3.2 Some code that uses the functions . . . . . . . . . . . 75 5.3.3 Write the definitions . . . . . . . . . . . . . . . . . . 76 5.4 How header files work . . . . . . . . . . . . . . . . . . . . . 77 5.4.1 The meaning of include . . . . . . . . . . . . . . . . . 77 5.4.2 Pragma once. . . . . . . . . . . . . . . . . . . . . . . 77 5.4.3 Information hiding . . . . . . . . . . . . . . . . . . . 78 5.4.4 Inline . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.5 A complete example . . . . . . . . . . . . . . . . . . . . . . 81 Contents ix 5.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 6 Unit Testing 85 6.1 A testing framework for C++ . . . . . . . . . . . . . . . . . 86 6.2 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 6.3 The macros in testing.h . . . . . . . . . . . . . . . . . . . 87 6.3.1 The ASSERT macro . . . . . . . . . . . . . . . . . . . 87 6.3.2 The ASSERT_APPROX_EQUAL macro. . . . . . . . . . . 87 6.3.3 The INFO macro . . . . . . . . . . . . . . . . . . . . . 88 6.3.4 The DEBUG_PRINT macro . . . . . . . . . . . . . . . . 88 6.3.5 The TEST macro . . . . . . . . . . . . . . . . . . . . 89 6.4 Using testing.h . . . . . . . . . . . . . . . . . . . . . . . . 89 6.5 What have we gained? . . . . . . . . . . . . . . . . . . . . . 91 6.6 Testing normcdf . . . . . . . . . . . . . . . . . . . . . . . . 92 6.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 7 Using C++ Classes 97 7.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 7.2 Pass by reference and const . . . . . . . . . . . . . . . . . . 100 7.2.1 Pass by reference . . . . . . . . . . . . . . . . . . . . 101 7.2.2 The const keyword . . . . . . . . . . . . . . . . . . . 102 7.2.3 Pass by reference without const . . . . . . . . . . . . 104 7.3 Using ofstream . . . . . . . . . . . . . . . . . . . . . . . . . 104 7.4 Working with string . . . . . . . . . . . . . . . . . . . . . 106 7.5 Building strings efficiently . . . . . . . . . . . . . . . . . . . 107 7.6 Writing a pie chart . . . . . . . . . . . . . . . . . . . . . . . 108 7.6.1 A web-based chart . . . . . . . . . . . . . . . . . . . 109 7.6.2 Create a header file . . . . . . . . . . . . . . . . . . . 111 7.6.3 Write a source file . . . . . . . . . . . . . . . . . . . . 112 7.6.4 Enable testing in your files . . . . . . . . . . . . . . . 112 7.6.5 Write functions to generate the boiler plate . . . . . 112 7.6.6 Write a simple version of the chart data . . . . . . . 113 7.6.7 Write a test of what we’ve done so far . . . . . . . . 114 7.6.8 Write the interesting code . . . . . . . . . . . . . . . 114 7.6.9 Testing the interesting code . . . . . . . . . . . . . . 115 7.6.10 Wrap it all up into a single function. . . . . . . . . . 116 7.7 The architecture of the World Wide Web . . . . . . . . . . 117 7.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8 User-Defined Types 123 8.1 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 8.2 Writing your own class . . . . . . . . . . . . . . . . . . . . . 124

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.