9#include <gsl/gsl_cdf.h>
10#include <gsl/gsl_randist.h>
22 : vector<double>(other),
41 vector<double>::operator=(other);
57 insert(end(), other.begin(), other.end());
62 if (other !=
nullptr) {
63 insert(end(), other->begin(), other->end());
76 for (
const double value : *
this) {
80 return sum /
static_cast<double>(size());
89 if (mean_value == -999) {
94 for (
const double value : *
this) {
95 sum += pow(value - mean_value, 2);
98 return sqrt(sum /
static_cast<double>(size() - 1));
107 if (mean_value == -999) {
112 for (
const double value : *
this) {
113 sum += pow(
log(value) - mean_value, 2);
116 return sqrt(sum /
static_cast<double>(size() - 1));
121 if (mean_value == -999) {
126 for (
const double value : *
this) {
127 sum += pow(value - mean_value, 2);
135 if (mean_value == -999) {
140 for (
const double value : *
this) {
141 sum += pow(
log(value) - mean_value, 2);
154 for (
const double value : *
this) {
158 return sum /
static_cast<double>(size());
164 for (
const double value : *
this) {
165 sum += pow(value,
power);
174 for (
const double value : *
this) {
185 for (
size_t i = 0; i < size(); i++) {
186 transformed[i] =
log(at(i));
194 return aquiutils::Min(*
this);
199 return aquiutils::Max(*
this);
207 vector<double> parameters;
239 const vector<double>& params,
242 double log_likelihood = 0.0;
246 for (
const double value : *
this) {
256 for (
const double value : *
this) {
257 log_likelihood +=
log(dist.
Eval(value));
261 return log_likelihood;
284 vector<double> sorted_data = QSort(*
this);
287 double probability_increment = 1.0 /
static_cast<double>(sorted_data.size());
289 for (
size_t i = 0; i < sorted_data.size(); i++) {
290 double cumulative_probability = (
static_cast<double>(i) + 0.5) * probability_increment;
291 cdf.append(sorted_data[i], cumulative_probability);
310 double x_min = comparison[0].mint();
311 double x_max = comparison[0].maxt();
312 double step_size = (x_max - x_min) / 50.0;
314 for (
double x = x_min;
x <= x_max;
x += step_size) {
315 double cumulative_prob = 0.0;
326 fitted_cdf.append(
x, cumulative_prob);
329 comparison.append(fitted_cdf,
"Fitted");
330 comparison.append(comparison[0] - comparison[1],
"Error");
347 vector<double> parameters;
359 double x_min = result[0].mint();
360 double x_max = result[0].maxt();
361 double step_size = (x_max - x_min) / 50.0;
363 for (
double x = x_min;
x <= x_max;
x += step_size) {
371 const double observed_height = fitted_pdf.maxC() / 2.0;
372 for (
auto& point : result[
"Observed"])
373 point.c = observed_height;
374 result.append(fitted_pdf,
"Fitted");
385 if (!observed_fitted.Contains(
"Error"))
386 return std::numeric_limits<double>::quiet_NaN();
389 double max_positive_diff = fabs(observed_fitted[2].maxC());
390 double max_negative_diff = fabs(observed_fitted[2].minC());
392 return std::max(max_positive_diff, max_negative_diff);
406 if (std::fabs(lambda) < 0.01)
407 return std::expm1(lambda * std::log(
x)) / lambda;
408 return (std::pow(
x, lambda) - 1.0) / lambda;
419 return std::numeric_limits<double>::quiet_NaN();
428 double variance_term = 0.0;
429 double jacobian_term = 0.0;
431 for (
size_t i = 0; i < size(); i++) {
432 variance_term += 1.0 /
static_cast<double>(size()) *
433 pow(transformed[i] - transformed_mean, 2);
434 jacobian_term += (lambda - 1.0) *
log(at(i) / std_dev);
437 return -
static_cast<double>(size()) / 2.0 *
log(variance_term) + jacobian_term;
442 bool normalize)
const
448 for (
size_t i = 0; i < size(); i++) {
449 scaled[i] = at(i) / std_dev;
461 for (
size_t i = 0; i < size(); i++) {
471 int n_intervals)
const
481 std::cerr <<
"Warning: non-finite value in concentration data" << std::endl;
486 if (fabs(min_lambda - max_lambda) < 1e-6) {
487 return (min_lambda + max_lambda) / 2.0;
491 vector<double> ks_statistics;
492 double lambda_step = (max_lambda - min_lambda) /
static_cast<double>(n_intervals);
494 for (
double lambda = min_lambda; lambda <= max_lambda; lambda += lambda_step) {
499 ks_statistics.push_back(ks_stat);
503 int min_index = aquiutils::MinElement(ks_statistics);
506 int lower_index = std::max(min_index - 1, 0);
507 int upper_index = std::min(min_index + 1,
static_cast<int>(ks_statistics.size() - 1));
509 double refined_min = min_lambda + lower_index * lambda_step;
510 double refined_max = min_lambda + upper_index * lambda_step;
519 return aquiutils::Rank(*
this);
Collection of time series with labels and observed values.
Time series data container with Interface support.
double CalculateSSE(double mean_value=-999) const
Calculate sum of squared errors from mean.
ConcentrationSet CreateLogTransformed() const
Create log-transformed copy.
double CalculateMean() const
Calculate arithmetic mean.
double CalculateLogLikelihood(const vector< double > ¶ms=vector< double >(), distribution_type dist_type=distribution_type::none) const
Calculate log-likelihood of data given distribution parameters.
CMBTimeSeriesSet CreateCDFComparison(distribution_type dist_type) const
Create comparison of empirical vs fitted CDF.
double CalculateStdDevLog(double mean_value=-999) const
Calculate standard deviation of log-transformed values.
vector< double > EstimateDistributionParameters(distribution_type dist_type=distribution_type::none)
Estimate distribution parameters from data.
Distribution estimated_distribution_
Distribution being optimized (e.g., in MCMC).
CMBTimeSeries CreateDataCDF() const
Create empirical CDF from data.
double CalculateMeanLog() const
Calculate mean of log-transformed values.
double CalculateBoxCoxLogLikelihood(double lambda) const
Box-Cox profile log-likelihood for given lambda.
double CalculateNormLog(double power=2.0) const
Calculate sum of log-transformed values raised to a power.
double CalculateKolmogorovSmirnovStatistic(distribution_type dist_type) const
Calculate Kolmogorov-Smirnov statistic.
bool CanBoxCoxTransform() const
Whether the Box-Cox transformation applies: all values positive.
vector< unsigned int > CalculateRanks() const
Calculate ranks of values (1 = smallest).
double GetMaximum() const
Get maximum value.
static double BoxCoxTransformValue(double x, double lambda)
Box-Cox transform of a single positive value.
distribution_type SelectBestDistribution()
Select best-fitting distribution (normal vs lognormal).
bool CanFitDistribution() const
Whether a distribution can be fitted to the data.
void AppendSet(const ConcentrationSet &other)
Append all values from another set.
double GetMinimum() const
Get minimum value.
double FindOptimalBoxCoxParameter(double min_lambda, double max_lambda, int n_intervals) const
Find optimal Box-Cox lambda parameter.
ConcentrationSet ApplyBoxCoxTransform(double lambda, bool normalize) const
Apply Box-Cox transformation.
double CalculateNorm(double power=2.0) const
Calculate sum of values raised to a power.
void AppendValue(double value)
Append a single concentration value.
CMBTimeSeriesSet CreateFittedDistribution(distribution_type dist_type) const
Create fitted distribution visualization.
ConcentrationSet & operator=(const ConcentrationSet &other)
double CalculateSSELog(double mean_value=-999) const
Calculate sum of squared errors from log mean.
double CalculateStdDev(double mean_value=-999) const
Calculate standard deviation.
Distribution fitted_distribution_
Distribution fitted to observed data.
Represents a parametric probability distribution for uncertainty quantification.
double Eval(const double &x) const
Evaluate probability density function at a given value.
vector< double > parameters
Distribution parameters vector.
distribution_type distribution
The type of probability distribution.
distribution_type
Enumeration of probability distribution types supported in SedSat3.
@ none
No distribution assigned (uninitialized state).
@ lognormal
Lognormal distribution: ln(x) ~ N(μ, σ²), for strictly positive variables.
@ normal
Normal (Gaussian) distribution: p(x) = N(μ, σ²).
The one place where data values entering SedSAT3 are checked.
bool AllFinite(const std::vector< double > &values)
Whether every value is finite. Data imported through the functions above always is.