11// Copyright 2025 Matt Borland
22// Distributed under the Boost Software License, Version 1.0.
33// https://www.boost.org/LICENSE_1_0.txt
4+ //
5+ // This example demonstrates how to perform statistics using boost.math
46
57// Needed for operations with boost math
68#define BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS
79
810#include " where_file.hpp"
9- #include < boost/decimal.hpp>
11+ #include < boost/decimal/decimal64_t.hpp> // For type decimal64_t
12+ #include < boost/decimal/charconv.hpp> // For from_chars
13+ #include < boost/decimal/iostream.hpp> // Decimal support to <iostream> and <iomanip>
14+ #include < boost/decimal/cmath.hpp> // For sqrt of decimal types
1015#include < iostream>
1116#include < iomanip>
1217#include < string>
1520#include < sstream>
1621
1722// Warning suppression for boost.math
23+ // Boost.decimal is tested with -Werror -Wall -Wextra and a few other additional flags
1824#if defined(__clang__)
1925# pragma clang diagnostic push
2026# pragma clang diagnostic ignored "-Wfloat-equal"
3541# pragma GCC diagnostic pop
3642#endif
3743
38- using namespace boost ::decimal ;
39-
44+ // This struct holds all the information that is provided
45+ // for a single trading day
4046struct daily_data
4147{
4248 std::string date;
43- decimal64_t open;
44- decimal64_t high;
45- decimal64_t low;
46- decimal64_t close;
47- decimal64_t volume;
49+ boost::decimal:: decimal64_t open;
50+ boost::decimal:: decimal64_t high;
51+ boost::decimal:: decimal64_t low;
52+ boost::decimal:: decimal64_t close;
53+ boost::decimal:: decimal64_t volume;
4854};
4955
50- // Function to split a CSV line into daily_data
5156auto parse_csv_line (const std::string& line) -> daily_data
5257{
5358 std::stringstream ss (line);
@@ -76,10 +81,13 @@ auto parse_csv_line(const std::string& line) -> daily_data
7681
7782int main ()
7883{
84+ // The first few lines of this file are similar to the previous example
85+ // in that we parse a single year of AAPL stock data before we can do anything useful with
86+
7987 std::vector<daily_data> stock_data;
8088
8189 // Open and read the CSV file
82- std::ifstream file (where_file (" AAPL.csv" ));
90+ std::ifstream file (boost::decimal:: where_file (" AAPL.csv" ));
8391 std::string line;
8492
8593 // Skip header line
@@ -98,23 +106,23 @@ int main()
98106 closing_prices.emplace_back (day.close );
99107 }
100108
101- const auto mean_closing_price = boost::math::statistics::mean (closing_prices);
102- const auto median_closing_price = boost::math::statistics::median (closing_prices);
103- const auto variance_closing_price = boost::math::statistics::variance (closing_prices);
104- const auto std_dev_closing_price = sqrt (variance_closing_price);
109+ // Here we use Boost.Math's statistics facilities
110+ // As shown at the top of the file you will need to define BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS,
111+ // and suppress a few warnings to make this build cleanly
112+ const decimal64_t mean_closing_price = boost::math::statistics::mean (closing_prices);
113+ const decimal64_t median_closing_price = boost::math::statistics::median (closing_prices);
114+ const decimal64_t variance_closing_price = boost::math::statistics::variance (closing_prices);
115+ const decimal64_t std_dev_closing_price = boost::decimal::sqrt (variance_closing_price);
105116
106117 // 2-Sigma Bollinger Bands
107- const auto upper_band = mean_closing_price + 2 * std_dev_closing_price;
108- const auto lower_band = mean_closing_price - 2 * std_dev_closing_price;
118+ // These are of a single point in time rather than making a plot over time for simplicity
119+ const decimal64_t upper_band = mean_closing_price + 2 * std_dev_closing_price;
120+ const decimal64_t lower_band = mean_closing_price - 2 * std_dev_closing_price;
109121
110122 std::cout << std::fixed << std::setprecision (2 )
111- << " Mean Closing Price: " << mean_closing_price << ' \n '
112- << " Standard Deviation: " << std_dev_closing_price << ' \n '
113- << " Upper Bollinger Band: " << upper_band << ' \n '
114- << " Lower Bollinger Band: " << lower_band << std::endl;
115-
116- // Mean = 207.21
117- // Median = 214.27
118- return mean_closing_price > median_closing_price;
123+ << " Mean Closing Price: $" << mean_closing_price << ' \n '
124+ << " Median Closing Price: $" << median_closing_price << ' \n '
125+ << " Standard Deviation: $" << std_dev_closing_price << ' \n '
126+ << " Upper Bollinger Band: $" << upper_band << ' \n '
127+ << " Lower Bollinger Band: $" << lower_band << std::endl;
119128}
120-
0 commit comments