Skip to content

Commit

Permalink
Added lognormal test; Initialize with sample; Improved histogram; Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
open-risk committed Feb 22, 2024
1 parent 01a8bbf commit 269cc49
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 98 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ A C++ library for the calculation of various tail risk measures
* Implemented [Risk Measures](RiskMeasures.md)
* Mathematical Documentation available [here](https://www.openriskmanual.org/wiki/Category:Tail_Risk)


## Dependencies

* Eigen (Data container and calculation library)
* Poco (For parsing JSON inputs)
* Eigen (Data container and linear algebra calculation library)
* Poco (For parsing JSON inputs and other utilities)
* Stats (A C++ header-only library of statistical distribution functions.)

## Example
The data directory contains sample datafiles with various sampled distributions

```c++
// Read in some data for a type 0 representation (discrete distribution)
// Read in some data for a type 0 representation (discrete distribution)
int LossGrid = 1000;
int DataType = 0;
RandomVar L(LossGrid, DataType);
Expand Down
20 changes: 10 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Copyright (C) 2020-2023 Open Risk (www.openriskmanagement.com)
## Copyright (C) 2020-2024 Open Risk (www.openriskmanagement.com)
##
## This file is part of the tailRisk C++ library.
##
Expand Down Expand Up @@ -36,7 +36,7 @@ include_directories(..)
include_directories(.)

# tailRisk version
set(VERSION 0.1.0)
set(VERSION 0.2.0)

set(SOURCE_FILES
random_var.cpp
Expand All @@ -49,10 +49,10 @@ set(TEST_FILES
../testing/rnd_statistics/test_random_var_normal.cpp
../testing/rnd_statistics/test_random_var_uniform.cpp
../testing/rnd_statistics/test_random_var_exponential.cpp
../testing/test_histogram.cpp
../testing.cpp)
../testing/rnd_statistics/test_random_var_lognormal.cpp
../testing/test_histogram.cpp)

find_package(Poco REQUIRED COMPONENTS Foundation Net NetSSL Util Data DataSQLite)
find_package(Poco REQUIRED COMPONENTS Foundation Net Util Data)
find_package(Catch2 REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(statslib REQUIRED)
Expand All @@ -65,8 +65,9 @@ target_include_directories(tailRisk PRIVATE ${statslib_INCLUDE_DIRS})
target_include_directories(tailRisk PRIVATE ${gcem_INCLUDE_DIRS})
target_link_libraries(tailRisk
Poco::Foundation
Poco::Data
Poco::JSON
Poco::Net
Poco::NetSSL
Poco::Util)

# CATCH2 TESTING
Expand All @@ -81,11 +82,10 @@ target_include_directories(unit_testing PRIVATE ${Catch2_INCLUDE_DIRS})
target_include_directories(unit_testing PRIVATE ${Eigen3_INCLUDE_DIRS})
target_include_directories(unit_testing PRIVATE ${statslib_INCLUDE_DIRS})
target_include_directories(unit_testing PRIVATE ${gcem_INCLUDE_DIRS})
target_link_libraries(unit_testing
target_link_libraries(unit_testing Catch2::Catch2WithMain
Poco::Data
Poco::DataSQLite
Poco::Foundation
Poco::Net
Poco::NetSSL
Catch2::Catch2WithMain)
Poco::Util
Poco::JSON)

6 changes: 6 additions & 0 deletions src/conanfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ statslib/3.2.0
gcem/1.16.0
catch2/3.4.0

[options]
poco*:enable_data_odbc=False
poco*:enable_data_mysql=False
poco*:enable_mongodb=False
poco*:enable_redis=False

[generators]
CMakeDeps
CMakeToolchain
26 changes: 23 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*################################################################################
##
## Copyright (C) 2020-2023 Open Risk (www.openriskmanagement.com)
## Copyright (C) 2020-2024 Open Risk (www.openriskmanagement.com)
##
## This file is part of the tailRisk C++ library.
##
Expand Down Expand Up @@ -30,11 +30,23 @@

int main(int argc, char *argv[]) {

// Example 1
int Grid = 1;
int DataType = 0;
RandomVar D(Grid, DataType);
std::string filename = "../data/example1.json";
D.ReadFromJSON(filename);
D.Print();
D.Cumulative();
D.Probability();

// Example 5
// Reading in some data for a type 0 representation (discrete distribution)
int LossGrid = 1000;
int DataType = 0;
DataType = 0;
RandomVar L(LossGrid, DataType);
std::string filename = "../../data/example5.json";
// ATTN: Make sure this points to the correct path versus your executable!
filename = "../data/example5.json";
L.ReadFromJSON(filename);
L.Print();

Expand All @@ -54,5 +66,13 @@ int main(int argc, char *argv[]) {
std::cout << "Exceedance Probability: " << L.ExceedanceProbability(threshold) << std::endl;
std::cout << "Mean Excess: " << L.MeanExcess(threshold ) << std::endl;

// Working with type 1 representations
int SampleSize = 10000;
DataType = 1;
RandomVar myR(SampleSize, DataType);
myR.Seed();
RandomVar H = myR.Histogram(10);
H.Print();

return 0;
}
29 changes: 24 additions & 5 deletions src/random_var.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*################################################################################
##
## Copyright (C) 2020-2023 Open Risk (www.openriskmanagement.com)
## Copyright (C) 2020-2024 Open Risk (www.openriskmanagement.com)
##
## This file is part of the tailRisk C++ library.
##
Expand Down Expand Up @@ -30,6 +30,7 @@


#include <Poco/JSON/Parser.h>
#include "stats.hpp"
#include "random_var.h"

using namespace Poco;
Expand Down Expand Up @@ -57,7 +58,7 @@ RandomVar &RandomVar::operator=(const RandomVar &R) {
};

/**
* Sort the sampling data
* Sort the sampling data if Type 1, else do nothing
*/
void RandomVar::Sort() {
if (m_type == 1) {
Expand All @@ -74,21 +75,27 @@ void RandomVar::Sort() {
* Bins are assumed equal and represented by their mid-point values
*/
RandomVar RandomVar::Histogram(int Bins) {
RandomVar H(Bins+1,0);
RandomVar H(Bins,0);
if (m_type == 1) {
std::sort(m_S.begin(), m_S.end());
double min_value = m_S[0];
double max_value = m_S[m_S.size()-1];
double bin_width = (max_value - min_value)/ (double) Bins;
double sample_p = 1.0 / m_size;
for (int i = 0; i < Bins + 1; i++) {
H.setX(i, min_value + bin_width / 2.0 + (double) (i) * bin_width);
for (int i = 0; i < Bins; i++) {
double x = min_value + bin_width / 2.0 + (double) (i) * bin_width;
H.setX(i, x);
H.setP(i, 0.0);
H.setC(i, 0.0);
}
for (int j = 0; j < m_size; j++) {
int observation_bin = (int) ( (m_S[j] - min_value) / bin_width );
if (observation_bin > Bins - 1) {
observation_bin = Bins - 1;
}
H.addP(observation_bin, sample_p);
}
H.Cumulative();
}
return H;
}
Expand Down Expand Up @@ -361,3 +368,15 @@ void RandomVar::ReadFromJSON(std::string &filename) {
this->setC(i, object->getValue<double>("cumulative"));
}
}

/**
* Seed with a sampled distribution
*/
void RandomVar::Seed() {
if (m_type == 1) {
stats::rand_engine_t engine(12376);
for (int i = 0; i < this->m_size; i++) {
this->setS(i, stats::runif(0.0, 1.0, engine));
}
}
}
59 changes: 34 additions & 25 deletions src/random_var.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*################################################################################
##
## Copyright (C) 2020-20223 Open Risk (www.openriskmanagement.com)
## Copyright (C) 2020-2024 Open Risk (www.openriskmanagement.com)
##
## This file is part of the tailRisk C++ library.
##
Expand Down Expand Up @@ -35,9 +35,9 @@ class RandomVar {

public:

// constructor with size and empirical distribution type
// Type 0 -> histogram storage
// Type 1 -> sample storage
// Constructor with size and empirical distribution type
// Type 0 -> Histogram type Storage
// Type 1 -> Sample type Storage

RandomVar(size_t S, int type) {
if (type == 0) {
Expand All @@ -51,12 +51,11 @@ class RandomVar {
m_S.resize(S);
m_size = S;
} else {
std::cout << "Error in random variable representation type" << std::endl;
std::cout << "Error in Random Variable representation type: Must be 0 or 1" << std::endl;
}
}

// constructor directly from existing data
// TODO generalize data type to accommodate different accuracy requirements

RandomVar(Eigen::ArrayXd x, Eigen::ArrayXd p, const int size) {
m_type = 0;
Expand Down Expand Up @@ -86,6 +85,8 @@ class RandomVar {
return m_type;
};

// Getters and Setters

[[nodiscard]] double getP(int index) const {
return m_P[index];
};
Expand All @@ -102,6 +103,30 @@ class RandomVar {
return m_S[index];
};

void setP(int index, double arg) {
m_P[index] = arg;
};

void setC(int index, double arg) {
m_C[index] = arg;
};

void setX(int index, double arg) {
m_X[index] = arg;
};

void setS(int index, double arg) {
m_S[index] = arg;
};

// Increment probability mass

void addP(int index, double arg) {
m_P[index] += arg;
};

// Statistical Measures

[[nodiscard]] double Average();

[[nodiscard]] double Mean();
Expand Down Expand Up @@ -130,25 +155,7 @@ class RandomVar {

[[nodiscard]] int Quantile_Index(double alpha);

void setP(int index, double arg) {
m_P[index] = arg;
};

void setC(int index, double arg) {
m_C[index] = arg;
};

void addP(int index, double arg) {
m_P[index] += arg;
};

void setX(int index, double arg) {
m_X[index] = arg;
};

void setS(int index, double arg) {
m_S[index] = arg;
};
// Various Operations

void Sort();

Expand All @@ -162,6 +169,8 @@ class RandomVar {

void Print();

void Seed();

private:
// 0 Type: exact representation (discrete probabilities view)
// 1 Type: sampling representation (distribution sampling view)
Expand Down
44 changes: 0 additions & 44 deletions testing.cpp

This file was deleted.

7 changes: 6 additions & 1 deletion testing/rnd_statistics/test_random_var_bernulli.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*################################################################################
##
## Copyright (C) 2020-2023 Open Risk (www.openriskmanagement.com)
## Copyright (C) 2020-2024 Open Risk (www.openriskmanagement.com)
##
## This file is part of the tailRisk C++ library.
##
Expand Down Expand Up @@ -41,6 +41,11 @@ double bernulli_var(size_t number, double param) {
return myR.Variance();
}

/*
* Numbers based on the Stats test
* https://github.com/kthohr/stats/blob/master/tests/rand/rbern.cpp
*/

double prob_par = 0.75;
double m_bernulli_mean = prob_par;
double m_bernulli_var = prob_par * (1.0 - prob_par);
Expand Down
Loading

0 comments on commit 269cc49

Please sign in to comment.