SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
cmbdistribution.cpp
Go to the documentation of this file.
1// SedSAT3 – Sediment Source Assessment Tool, version 3
2// U.S. Geological Survey. See LICENSE.md and DISCLAIMER.md in the repository root.
3
4#include "cmbdistribution.h"
5
6#include <cmath>
7#include "math.h"
8
9double Distribution::pi = 4 * atan(1.0);
10
15
16double Distribution::Eval(const double &x) const
17{
18 double out = 0;
20 out = 1.0 / (sqrt(2*pi)*parameters[1])*exp(-pow(x - parameters[0], 2) / (2 * parameters[1]*parameters[1]));
22 out = 1.0 / (sqrt(2*pi)*parameters[1] * x)*exp(-pow(log(x) - parameters[0], 2) / (2 * parameters[1] * parameters[1]));
24 if (x<0 || x>1)
25 out = 1e-32;
26 return out;
27}
28
30{
31 double out = 0;
33 out = 1.0 / (sqrt(2*pi)*parameters[1])*exp(-pow(x - parameters[0], 2) / (2 * parameters[1]*parameters[1]));
35 out = 1.0 / (sqrt(2*pi)*parameters[1] * x)*exp(-pow(log(x) - parameters[0], 2) / (2 * parameters[1] * parameters[1]));
37 if (x<0 || x>1)
38 out = 1e-32;
39 return out;
40}
41
42double Distribution::EvalLog(const double &x)
43{
44 double out = 0;
46 out = -log(sqrt(2*pi)*parameters[1]) -pow(x - parameters[0], 2) / (2 * parameters[1]*parameters[1]);
48 out = -log(sqrt(2*pi)*parameters[1]*x) -pow(log(x) - parameters[0], 2) / (2 * parameters[1] * parameters[1]);
50 if (x<0 || x>1)
51 out = -1e6;
52
53 return out;
54}
55
56TimeSeries<double> Distribution::EvaluateAsTimeSeries(int numberofpoints, const double &stdcoeff)
57{
58 TimeSeries<double> out;
59 double x = NAN;
60 if (numberofpoints < 2)
61 return out; // the grid below divides by numberofpoints - 1
62 for (int i=0; i<numberofpoints; i++)
63 {
65 {
66 x = parameters[0] - stdcoeff*parameters[1] + i*2*stdcoeff*parameters[1]/(numberofpoints-1);
67 }
69 {
70 x = exp(parameters[0] - stdcoeff*parameters[1] + i*2*stdcoeff*parameters[1]/(numberofpoints-1));
71 }
72 else
73 {
74 // Uniform and Dirichlet are on [0, 1]; x used to stay NaN here
75 x = double(i)/double(numberofpoints-1);
76 }
77 out.append(x,Eval(x));
78 }
79 return out;
80}
81
82TimeSeries<double> Distribution::EvaluateAsTimeSeries(int numberofpoints, const double &stdcoeff, const vector<double>& parameters, distribution_type &dist_type)
83{
84 TimeSeries<double> out;
85 double x = NAN;
86 if (numberofpoints < 2)
87 return out; // the grid below divides by numberofpoints - 1
88 for (int i=0; i<numberofpoints; i++)
89 {
90 if (dist_type == distribution_type::normal)
91 {
92 x = parameters[0] - stdcoeff*parameters[1] + i*2*stdcoeff*parameters[1]/(numberofpoints-1);
93 }
94 else if (dist_type == distribution_type::lognormal)
95 {
96 x = exp(parameters[0] - stdcoeff*parameters[1] + i*2*stdcoeff*parameters[1]/(numberofpoints-1));
97 }
98 else
99 {
100 // Uniform and Dirichlet are on [0, 1]; x used to stay NaN here
101 x = double(i)/double(numberofpoints-1);
102 }
103 out.append(x,Eval(x,parameters,dist_type));
104 }
105 return out;
106}
107
108
110{
111 pi = 4 * atan(1.0);
112 parameters = dist.parameters;
114 mean_val = dist.mean_val;
115 std_val = dist.std_val;
116 SetType(dist.distribution);
117}
119{
120 pi = 4 * atan(1.0);
121 parameters = dist.parameters;
123 mean_val = dist.mean_val;
124 std_val = dist.std_val;
125 SetType(dist.distribution);
126 return *this;
127}
128
130{
131 distribution = typ;
133 parameters.resize(2);
134 if (typ == distribution_type::normal)
135 parameters.resize(2);
136}
137
138
140{
141 if (param_mode==parameter_mode::direct)
142 return mean_val;
143
145 return exp(parameters[0]+pow(parameters[1],2)/2);
147 return parameters[0];
148
149 return 0;
150}
151
152bool Distribution::operator==(const string &dist_type)
153{
154 if (distribution==distribution_type::normal && dist_type == "normal")
155 return true;
156 if (distribution==distribution_type::lognormal && dist_type == "log-normal")
157 return true;
158 if (distribution==distribution_type::uniform && dist_type == "uniform")
159 return true;
160 if (distribution==distribution_type::dirichlet && dist_type == "uniform")
161 return true;
162 if (distribution==distribution_type::dirichlet && dist_type == "dirichlet")
163 return true;
164
165 return false;
166
167}
168
double Eval(const double &x) const
Evaluate probability density function at a given value.
vector< double > parameters
Distribution parameters vector.
double EvalLog(const double &x)
Evaluate natural logarithm of probability density.
double std_val
Empirical standard deviation from original sample data.
TimeSeries< double > EvaluateAsTimeSeries(int numberofpoint=100, const double &stdcoeff=4)
Generate time series representation of the distribution for plotting.
distribution_type distribution
The type of probability distribution.
Distribution & operator=(const Distribution &dist)
Assignment operator.
void SetType(const distribution_type &typ)
Set the distribution type and resize parameters vector.
static double pi
Mathematical constant π (pi).
double mean_val
Empirical mean from original sample data.
double Mean(parameter_mode param_mode=parameter_mode::based_on_fitted_distribution)
Calculate the mean (expected value) of the distribution.
bool operator==(const string &dist_type)
Compare distribution type with a string identifier.
Distribution()
Default constructor.
STL class.
parameter_mode
Specifies whether to use direct data statistics or fitted distribution parameters.
@ direct
Use empirical statistics from data.
distribution_type
Enumeration of probability distribution types supported in SedSat3.
@ dirichlet
Dirichlet distribution (treated as uniform on simplex in current implementation).
@ lognormal
Lognormal distribution: ln(x) ~ N(μ, σ²), for strictly positive variables.
@ uniform
Uniform distribution on [0,1].
@ normal
Normal (Gaussian) distribution: p(x) = N(μ, σ²).