Doxygen Files
-
template<typename T>
class Distribution - #include <Distribution.h>
Contains a distribution of values that can be operated on to perform Monte Carlo error propagation.
As an alternative to first- or second-order error propagation for a random variable with a given mean and uncertainty, one can generate sample data reflecting those values, operate on it element-wise, and find the resulting statistics.
To implement this, this class contains a
valarray
object of the sample data along with members with statistical information.- Template Parameters:
T – the data type (
double
,float
, etc.).
Public Functions
-
inline Distribution(std::valarray<T> __samples)
- Parameters:
__samples –
valarray
of sample data contained within the distribution.
-
inline int num_samples() const
Number of samples.
-
inline int size() const
-
inline int length() const
-
inline int len() const
-
inline const T &operator[](int i) const
Provides read-only access to the (i+1)-th sample in the distribution.
- Parameters:
i – the index.
- Returns:
_samples[i].
-
inline Distribution<T> apply(T func(T))
Applies a function to
samples
element-wise.- Parameters:
func – the function to be applied.
- Returns:
The resulting distribution.
-
inline T covariance(const Distribution<T> Dist2)
Computes the covariance of two
Distribution
s.- Parameters:
Dist2 – the second distribution.
- Returns:
The covariance of
this
andDist2
.
-
inline Distribution<T> operator+(const Distribution<T> Dist2)
Add
Distribution
s by adding their samples element-wise.- Parameters:
Dist2 – second summand.
- Returns:
The resulting distribution.
-
inline Distribution<T> operator-(const Distribution<T> Dist2)
Subtract
Distribution
s by subtracting their samples element-wise.- Parameters:
Dist2 – the subtrahend.
- Returns:
The resulting distribution.
-
inline Distribution<T> operator*(const Distribution<T> Dist2)
Multiply
Distribution
s by multiplying their samples element-wise.- Parameters:
Dist2 – the multiplier.
- Returns:
The resulting distribution.
-
inline Distribution<T> operator/(const Distribution<T> Dist2)
Divide
Distribution
s by dividing their samples element-wise.- Parameters:
Dist2 – the divisor.
- Returns:
The resulting distribution.
-
inline Distribution<T> operator+(const T scalar)
Sum of a
Distribution
and a scalar.- Parameters:
scalar – summand of type
T
.- Returns:
The resulting distribution.
-
inline Distribution<T> operator-(const T scalar)
Subtract a
Distribution
by a scalar.- Parameters:
scalar – subtrahend of type
T
.- Returns:
The resulting distribution.
-
inline Distribution<T> operator*(const T scalar)
Multiply a
Distribution
by a scalar.- Parameters:
scalar – multiplier of type
T
.- Returns:
The resulting distribution.
-
inline Distribution<T> operator/(const T scalar)
Divide a
Distribution
by a scalar.- Parameters:
scalar – divisor of type
T
.- Returns:
The resulting distribution.
-
inline Distribution<T> operator-()
Unary minus.
- Returns:
The negated
Distribution
.
Private Members
-
int _num_samples
Size of the data.
Friends
-
template<typename>
friend std::ostream &operator<<(std::ostream &os, const Distribution<T>)
-
template<typename>
friend Distribution<T> operator+(T, const Distribution<T>)
-
template<typename>
friend Distribution<T> operator-(T, const Distribution<T>)
-
template<typename>
friend Distribution<T> operator*(T, const Distribution<T>)
-
template<typename>
friend Distribution<T> operator/(T, const Distribution<T>)
- module conf
Variables
- shell
- project = 'Distributions'
- copyright = "2024, Sam Glosser"
- author = 'Sam Glosser'
- extensions = ["breathe", "sphinx.ext.autodoc"]
- templates_path = ['_templates']
- exclude_patterns = []
- html_theme = 'sphinx_rtd_theme'
- html_static_path = ['_static']
- breathe_projects = { "Distributions": "../doxy_files/xml/" }
- breathe_default_project = "Distributions"
- file conf.py
- file Distribution.h
- #include <cmath>#include <iostream>#include <functional>#include <valarray>
Functions
-
template<typename T>
std::ostream &operator<<(std::ostream &os, const Distribution<T> &Dist)
-
template<typename T>
Distribution<T> operator+(T scalar, const Distribution<T> &Dist) Add a
Distribution
to a scalar.- Template Parameters:
T – the data type of the scalar and
Distribution
.- Parameters:
scalar – the scalar of type
T
to multiply by.Dist – The
Distribution
to multiply by.
- Returns:
The resulting
Distribution
.
-
template<typename T>
Distribution<T> operator-(T scalar, const Distribution<T> &Dist) Subtract a scalar by a
Distribution
.- Template Parameters:
T – the data type of the scalar and
Distribution
.- Parameters:
scalar – the minuend of type
T
.Dist – The
Distribution
to subtract.
- Returns:
The resulting
Distribution
.
-
template<typename T>
Distribution<T> operator*(T scalar, const Distribution<T> &Dist) Multiply a scalar by a
Distribution
.- Template Parameters:
T – the data type of the scalar and the samples in
Distribution
.- Parameters:
scalar – the multiplicand of type
T
.Dist – The
Distribution
to multiply by.
- Returns:
The resulting
Distribution
-
template<typename T>
Distribution<T> operator/(T scalar, const Distribution<T> &Dist) Divide a scalar by a
Distribution
.- Template Parameters:
T – the data type of the scalar and the samples in
Distribution
.- Parameters:
scalar – the dividend of type
T
.Dist – The
Distribution
to divide by.
- Returns:
The resulting
Distribution
.
-
template<typename T>
- file RandomDistribution.h
- #include “Distribution.h”#include <random>
Functions
-
template<typename T, typename DT>
Distribution<T> RandomDistribution(DT dis, int _num_samples, std::mt19937 gen = default_rng) Helper function to generate
Distribution
objects using sources of random numbers.- Template Parameters:
T – the data type.
DT – the type of
dis
.
- Parameters:
dis – a random number distribution (see
<random>
)._num_samples – the number of samples to generate.
gen – the random seed (defaults to a random value).
- Returns:
A
Distribution
containing the generated data.
-
template<typename T>
Distribution<T> Normal(T mu, T sigma, int _num_samples = 1000, std::mt19937 gen = default_rng) Creates a normal
Distribution
of real numbers with meanmu
and standard deviationsigma
.- Template Parameters:
T – the data type. Must be
float
,double
, orlong double
.- Parameters:
mu – the mean of the distribution.
b – the standard deviation of the distribution.
_num_samples – the number of samples to generate (default 1000).
gen – the random seed (defaults to a random value).
- Returns:
A
Distribution
containing the generated data.
-
template<typename T>
Distribution<T> Uniform(T a, T b, int _num_samples = 1000, std::mt19937 gen = default_rng) Creates a uniform
Distribution
of real numbers on the interval [a, b).- Template Parameters:
T – the data type. Must be
float
,double
, orlong double
.- Parameters:
a – the minimum of the distribution (inclusive).
b – the maximum of the distribution (exclusive).
_num_samples – the number of samples to generate (default 1000).
gen – the random seed (defaults to a random value).
- Returns:
A
Distribution
containing the generated data.
Variables
-
std::random_device rd
-
template<typename T, typename DT>
- file README.md
- file Tests.cpp
- #include <cassert>#include <iostream>#include <sstream>#include <string>#include “../include/Distribution.h”#include “../include/RandomDistribution.h”
- dir /home/runner/work/Distributions/Distributions/include
- dir source
- dir /home/runner/work/Distributions/Distributions/tests
- page index
This is my final project for PHY 504 Spring 2024. This package implements a
Distribution
class that enables Monte Carlo error propagation by generating sample data from distributions and performing arithmetic or other functions on them element-wise.