0% found this document useful (0 votes)
7 views4 pages

CppFinancialMaths 1699985217

Uploaded by

vzywd77rjf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

CppFinancialMaths 1699985217

Uploaded by

vzywd77rjf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ for Financial Mathematics

Amit Kumar Jha,UBS

1 Beginner-Friendly C++ Tips for Financial Mathematics


This sheet provides fundamental concepts and examples for beginners in financial mathematics using
C++. It covers basic techniques and best practices with Standard Template Library (STL) usage, offering
a strong foundation for further exploration in financial computing with C++.

2 Important Notes and Required Headers


Before we begin with the detailed examples and concepts, it is important to note the following prereq-
uisites and conventions used in this sheet.

2.1 Prerequisites
• Basic understanding of C++ syntax and concepts.

• Familiarity with the Standard Template Library (STL).

• An installed and configured C++ compiler (e.g., GCC, Clang, MSVC).

2.2 Required C++ Headers


For the examples presented in this cheat sheet, make sure to include the following standard library
headers at the beginning of your C++ programs:
# include < iostream > // for standard I / O
# include < vector > // for std :: vector
# include <map > // for std :: map
# include <set > // for std :: set
# include < algorithm > // for std :: sort , std :: accumulate
# include < functional > // for std :: function
# include < memory > // for smart pointers
# include < iterator > // for iterators
// Add any other headers as required by your specific examples

2.3 Understanding Vectors (std::vector)


What: Dynamic arrays that can change size.
Why: Ideal for storing lists of financial data (e.g., stock prices).
Tip: Always check the size before accessing elements.
Example:

1
std :: vector < double > stock_prices = {120.5 , 121.3 , 119.8};
for ( size_t i = 0; i < stock_prices . size () ; ++ i ) {
std :: cout << " Price : ␣ " << stock_prices [ i ] << std :: endl ;
}

2.4 Using Maps (std::map)


What: Collection of key-value pairs.
Why: Useful for storing and retrieving data by keys (e.g., price by date).
Tip: Use when you need to associate unique keys with values.
Example:
std :: map < std :: string , double > price_by_date = {{ " 2023 -03 -01 " , 120.5} , { "
,→ 2023 -03 -02 " , 121.3}};
std :: cout << " Price ␣ on ␣ 2023 -03 -01: ␣ " << price_by_date [ " 2023 -03 -01 " ] << std ::
,→ endl ;

2.5 Smart Pointers for Resource Management


What: Objects that automatically manage memory.
Why: Prevent memory leaks in dynamic allocation.
Types: std::unique ptr (exclusive ownership), std::shared ptr (shared ownership).
Example:
std :: unique_ptr < double > ptr ( new double (100.0) ) ;
std :: cout << " Value : ␣ " << * ptr << std :: endl ;

2.6 Algorithms for Common Operations


What: Pre-built functions for common tasks (e.g., sorting, searching).
Why: Efficient and reduces code redundancy.
Tip: Use std::sort, std::find, std::accumulate for basic operations.
Example:
std :: sort ( stock_prices . begin () , stock_prices . end () ) ;

2.7 Set Operations (std::set)


What: Collection of unique elements sorted by values.
Why: Useful for storing unique data like distinct stock tickers.
Tip: Use when duplicates are not allowed and order matters.
Example:
std :: set < std :: string > stock_tickers = { " AAPL " , " MSFT " , " GOOG " };
if ( stock_tickers . find ( " AAPL " ) != stock_tickers . end () ) {
std :: cout << " AAPL ␣ is ␣ in ␣ the ␣ set " << std :: endl ;
}

2
2.8 Using Iterators for Traversal
What: Objects that point to elements in a container.
Why: Universal way to traverse containers like vectors, sets, maps.
Tip: Use iterators in loops for accessing/modifying elements.
Example:
for ( auto it = stock_prices . begin () ; it != stock_prices . end () ; ++ it ) {
std :: cout << " Price : ␣ " << * it << std :: endl ;
}

2.9 Function Objects (std::function)


What: Wrapper for callable entities.
Why: Flexibility in passing functions, lambda expressions, or function pointers.
Tip: Use for custom operations in STL algorithms.
Example:
std :: function < bool ( double , double ) > compare = []( double a , double b ) { return
,→ a > b ; };
std :: sort ( stock_prices . begin () , stock_prices . end () , compare ) ;

2.10 Lambda Expressions for Short Inline Functions


What: Anonymous functions used for short snippets of code.
Why: Convenient for simple operations without defining a full function.
Example:
std :: for_each ( stock_prices . begin () , stock_prices . end () , []( double price ) {
std :: cout << " Price : ␣ " << price << std :: endl ;
}) ;

3 Mnemonics & Shortcuts


• ”VECTOR for Variable-sized Efficient Collections”: Remember std::vector for dynamic arrays.

• ”MAP for Matching Assets with Prices”: Use std::map to correlate dates with prices.

• ”SMART pointers for SAFE management”: Smart pointers for automatic memory handling.

• ”ALGO for Algorithmic shortcuts”: STL algorithms for streamlined operations.

• ”SET for Selective Elements”: Use std::set for unique, ordered elements.

• ”ITERATE with ITERators”: Remember to use iterators for traversing containers.

• ”FUNCTION for Flexible Operations”: std::function for diverse callable options.

• ”LAMBDA for Lightweight Anonymous Blocks”: Lambda expressions for concise code snippets.

3
4 Basic Financial Mathematics Examples in C++
4.1 Calculating Average Price with Vector
std :: vector < double > prices = {50.5 , 51.2 , 52.3};
double sum = std :: accumulate ( prices . begin () , prices . end () , 0.0) ;
double average = sum / prices . size () ;
std :: cout << " Average ␣ Price : ␣ " << average << std :: endl ;

4.2 Iterating Over a Map


for ( const auto & pair : price_by_date ) {
std :: cout << " Date : ␣ " << pair . first << " ,␣ Price : ␣ " << pair . second << std ::
,→ endl ;
}

4.3 Calculating Portfolio Variance


Context: Assume stock prices is a std::vector<double> containing historical prices. Calculate the
variance as an example of risk measurement.
Example:
double mean = std :: accumulate ( stock_prices . begin () , stock_prices . end () , 0.0) /
,→ stock_prices . size () ;
double variance = std :: accumulate ( stock_prices . begin () , stock_prices . end () ,
,→ 0.0 ,
[ mean ]( double total , double price ) {
return total + ( price - mean ) * ( price -
,→ mean ) ;
}) / stock_prices . size () ;
std :: cout << " Variance : ␣ " << variance << std :: endl ;

4.4 Using Sets for Unique Stock Tickers


Context: Demonstrate how to use std::set to store unique stock tickers.
Example:
std :: set < std :: string > tickers = { " AAPL " , " MSFT " , " GOOG " , " AAPL " };
std :: cout << " Number ␣ of ␣ unique ␣ tickers : ␣ " << tickers . size () << std :: endl ; // "
,→ AAPL " appears only once

5 Best Practices for Beginners


• Start with Simple Concepts: Begin with basic STL containers like vectors and maps.
• Practice with Real Data: Try implementing basic financial calculations using real stock prices.
• Consistent Practice: Regular coding will help in solidifying concepts.
• Seek Help from Communities: Engage with online forums or groups for learning and support.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy