Giter Club home page Giter Club logo

compfinance's Introduction

Companion code for "Modern Computational Finance: AAD and Parallel Simulations" (Antoine Savine, Wiley, 2018)

Screenshot

This is the professional implementation in C++ of the book Modern Computational Finance: AAD and Parallel Simulations by Antoine Savine. The code is freely available to anyone. Any person who purchased a copy of the book is authorized to use, modify and distribute the code for any application, as long as the credits remain on the top of the files.

In this repository, readers will find:

The source code listed or referenced in the publication.

The files AAD*.* (with a dependency on gaussians.h for the analytic differentiation of Gaussian functions, and blocklist.h for memory management, both included) constitute a self contained, general purpose AAD library. The code builds on the advanced techniques exposed in this publication, in particular those of chapters 10, 14 and 15 to produce a particularly fast differentiation library, applicable to many contexts. The code is progressively built and explained in part III.

The files mc*.* (with dependency on various utility files, all included in the project) form a generic, parallel, financial simulation library. The code and its theoretical foundations are described in part II. Various files with support code for memory management, interpolation or concurrent data structures, such as threadPool.h, which is developed in part I and used throughout the book to execute tasks in parallel.

A file main.h that lists all the higher level functions that provide an entry point into the combined library.

A Visual Studio 2017 project wrapping all the source files, with project settings correctly set for maximum optimization. The code uses some C++ 17 constructs, so the project setting "C++ Language Standard" on the project property "C/C++ / Language / C++ Language Standard" must be set to "ISO C++ 17 standard". This setting is correctly set on the project file xlComp.vcxproj, but readers who compile the files by other means must be aware of this.

A number of xl*.* files that contain utilities and wrappers to export the main functions to Excel, as a particularly convenient front end for the library. The project file xlComp.vcxproj is set to build an xll, a file that is opened from Excel and makes the exported library functions callable from Excel like its standard functions. We wrote a tutorial that explains how to export C++ code to Excel. The tutorial ExportingCpp2xl.pdf is available in the the folder xlCpp along with the necessary source files. The wrapper xlExport.cpp file in our project precisely follows the directives of the tutorial and readers can inspect it to better understand these techniques.

Finally, we provide a pre-built xlComp.xll (to run xlComp.xll, readers may need to install Visual Studio redistributables VC_redist.x86.exe and VC_redist.x64.exe, also included in the repository) and a spreadsheet xlTest.xlsx that demonstrates the main functions of the library. All the figures and numerical results in this publication were obtained with this spreadsheet and this xll, so readers can reproduce them immediately. The computation times were measured on an iMac Pro (Xeon W 2140B, 8 cores, 3.20 GHz, 4.20 max) running Windows 10. We also carefully checked that we have \emph{consistent} calculation times on a recent quad core laptop (Surface Book 2, i7-8650U, 4 cores, 1.90 GHz, 4.20 max), that is, (virtually) identical time in single threaded mode, twice the time in multi-threaded mode.

The code is entirely written in standard C++, and compiles on Visual Studio 2017 out of the box, without any dependency to a third party library.

The branch 'AADBook' is frozen and reflects the code in the book as published. The master branch may evolve. Other branches contain specific implementations based on the library. For example, the 'MutliAssets' branch contains developments to extend the library to support multiple underlying assets, like basket options or autocalls. They will eventually merge into Master but not AADBook.

Comp Fin Lecture

Computational Finance and Machine Learning in Finance

The repo 'CompFinLecture' contains the slides and material for the lectures based on the book. This is a softer introduction to some of the themes covered in the book, which also introduces deep learning and its application to derivatives finance, and establishes parallels between the back-propagation algorithm, at the core of deep learning, and AAD. This parallel, in turn, provides further intuition into the mechanics of AAD.

These lectures are delivered as courses or workshops at Copenhagen University, Aarhus University and Kings College London. The lecture notes, C++ code and TensorFlow notebooks are freely available in the 'CompFinLectures' repo.

The lectures demonstrate AAD and its application to finance with "toy code", which is particularly easy to understand and reproduce, but is not efficient or scalable to professional standard, and not suitable for production. The code in this repo ('CompFinance'), on the contrary, is efficient, compliant with professional C++ development standards and designed to scale to production.

compfinance's People

Contributors

asavine avatar davidefichera avatar differential-machine-learning avatar huntonb avatar thomasndam avatar wegamekinglc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

compfinance's Issues

Excel crashes

Whenever I open xlTest.xlsx and try and to calculate the values, Excel crashes and restarts. I am using Microsoft Excel 2016 32-bit on a Intel Corei7 7700HQ GTX 1050Ti Windows 10 Laptop. I have generated the .xll file and can see the myOwnCppFunctions functions in Excel.

xll functions are not recognized by excel

I git-cloned the CompFinance repo, built the solution with visual studio 2022 without any issue -- I just had to upgrade the windows SDK etc. (I built in debug x64 and my office 365 excel is x64 as well.)

Then I added the XLL to a blank excel file, in which I opened the test spreadsheet xlTest.xlsx.

Then, I see "#NAME?" in any cell using a function of the XLL in a formula.

These functions do not seem to be recognized by excel. (The same problem happens when building the debug xll in x86.)

I then put a breakpoint L1261 in the xlLExport.cpp file (the "entry point" of the XLL) and attached visual studio to a blank excel spreadsheet process, spreadsheet to which I added the debug x64 XLL. It indeed broke at the breakpoint I put.

The L1263 call to Excel12f worked like a charm. The L1265 call to Excel12f led to a #ifdef _WIN64 when one return(xlretFailed); Clearly, the code is x86 designed ...

The explains why all the XLL functions are not recognized by Excel, for the x64 XLL.

The #else of the #ifdef _WIN64 seems to have valid code, so I have redone the very same that preceeds, but in x86. Here, I didn't even break in the entry point ...

As I am no IT nor an XLL expert, I would be glad would someone point to a valid correction.

[BUG] blocklist's mark function doesn't work correctly at the boundary of the last block

The following codes will replicate the problem:

#include "AADNumber.h"
#include <chrono>
#include <iomanip>
#include <iostream>

using namespace std;

template <class T_>
T_ f(T_ x[]) {
    T_ y = x[0] * 2;
    return y;
}

int main() {
    constexpr auto num_param = 1;

    size_t n_loops = 8192;

    // Using automatic adjoint differentiation
    auto start = std::chrono::high_resolution_clock::now();


    for (size_t i = 0; i < n_loops; ++i) {
        Number::tape->mark();
        Number x[num_param] = {1.0};
        Number y = f(x);
        y.value();
        y.propagateToMark();
    }
    auto finish = std::chrono::high_resolution_clock::now();
    std::cout << "AAD aprox. time: "
              << std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() / n_loops << "ns\n";
    Number::tape->mark();
    Number x[num_param] = {1.0};
    Number y = f(x);
    y.value();
    y.propagateToMark();
    cout << "y: " << setprecision(9) << y.value() << endl;
    for (size_t i = 0; i < num_param; ++i) `{`
        cout << "AAD a" << i << " = "
             << setprecision(9) << x[i].adjoint() << endl;
    }
    return 0;
}

The first for loop will run out correctly and quickly. But the last set of the codes won't run out. After long time of debug, I think the problem lies at the the following lines:

marked_space = next_space;

When next_space ponits to end (which is in the case we are at the last entry in the last block). Then when propagate to the mark, the following line will never be satisfied as eqau;:
while (it != propagateTo)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.