1#ifndef LIBTEDDY_PROBABILITIES_HPP
2#define LIBTEDDY_PROBABILITIES_HPP
4#include <libteddy/details/config.hpp>
5#include <libteddy/details/types.hpp>
16#ifdef LIBTEDDY_SYMBOLIC_RELIABILITY
17# include <ginac/ginac.h>
20namespace teddy::probs::details
35 auto operator[] (std::size_t
const value)
const ->
double
37 assert(value == 1 || value == 0);
38 double const prob = (*vec_)[index_];
39 return value == 1 ? prob : 1 - prob;
58 auto operator[] (std::size_t
const index)
const
74 auto set_t (
double const t) ->
void
89template<
class Probabilities>
90concept prob_vector =
requires(Probabilities probs, int32 index) {
93 } -> std::convertible_to<double>;
99template<
class Probabilities>
100concept prob_matrix =
requires(Probabilities probs, int32 index, int32 value) {
103 } -> std::convertible_to<double>;
116 [[nodiscard]]
operator double ()
const
118 return (*
this)(dist_base::t_);
121 [[nodiscard]]
auto operator() (
double const t)
const ->
double
123 return rate_ * std::exp(-rate_ * t);
136 weibull(
double const scale,
double const shape) :
142 [[nodiscard]]
operator double ()
const
144 return (*
this)(dist_base::t_);
147 [[nodiscard]]
auto operator() (
double const t)
const ->
double
149 return (shape_ / scale_) * std::pow(t / scale_, shape_ - 1)
150 * std::exp(-std::pow(t / scale_, shape_));
164 constant(
double const value) : value_(value)
168 [[nodiscard]]
operator double ()
const
173 [[nodiscard]]
auto operator() (
double const)
const ->
double
188 custom_dist(std::function<
double(
double)> dist) : dist_(dist)
192 [[nodiscard]]
operator double ()
const
194 return (*
this)(dist_base::t_);
197 [[nodiscard]]
auto operator() (
double const t)
const ->
double
203 std::function<double(
double)> dist_;
206using dist_variant = std::variant<exponential, weibull, constant, custom_dist>;
214 prob_dist(dist_variant dist) : dist_(dist)
218 auto set_t (
double const t) ->
void
220 std::visit([t] (
auto& d) { d.set_t(t); }, dist_);
223 [[nodiscard]]
operator double ()
const
226 [] (
auto const& dist) ->
double {
return dist; },
231 [[nodiscard]]
auto operator() (
double const t)
const ->
double
234 [t] (
auto const& dist) ->
double {
return dist(t); },
247 } -> std::convertible_to<prob_dist>;
256 } -> std::convertible_to<prob_dist>;
261template<dist_vector Ps>
262auto at_time (Ps& distVector,
double const t) -> Ps&
264 for (
auto& dist : distVector)
271template<dist_matrix Ps>
272auto at_time (Ps& distMatrix,
double const t) -> Ps&
274 for (
auto& dists : distMatrix)
276 for (
auto& dist : dists)
285#ifdef LIBTEDDY_SYMBOLIC_RELIABILITY
286namespace teddy::symprobs::details
288inline GiNaC::symbol& time_symbol ()
290 static GiNaC::realsymbol t(
"t");
295namespace teddy::symprobs
303 expression(GiNaC::ex ex) : ex_(ex)
307 auto evaluate (
double const t)
const ->
double
309 GiNaC::ex result = GiNaC::evalf(ex_.subs(details::time_symbol() == t));
310 return GiNaC::ex_to<GiNaC::numeric>(result).to_double();
313 auto as_underlying_unsafe () const -> GiNaC::ex
318 auto to_latex (std::ostream& ost)
const ->
void
320 ost << GiNaC::latex << ex_ << GiNaC::dflt;
324 auto to_matlab (std::ostream& ost)
const ->
void
336inline auto exponential (
double const rate) -> expression
338 return {rate * GiNaC::exp(-rate * details::time_symbol())};
344inline auto weibull (
double const shape,
double const scale) -> expression
347 (shape / scale) * GiNaC::pow(details::time_symbol() / scale, shape - 1)
348 * GiNaC::exp(-GiNaC::pow(details::time_symbol() / scale, shape))};
354inline auto constant (
double prob) -> expression
356 return GiNaC::ex(prob);
362inline auto complement (expression
const& other) -> expression
364 return GiNaC::ex(1) - other.as_underlying_unsafe();
371auto as_matrix (Ps
const& probs) -> std::vector<std::array<expression, 2>>
373 std::vector<std::array<expression, 2>> matrix;
374 for (expression
const& expr : probs)
376 matrix.push_back(std::array {complement(expr), expr});
Probability independent of time.
Definition probabilities.hpp:162
User-defined distribution.
Definition probabilities.hpp:186
Base class for probability distributions. Just holds time member that is common for all.
Definition probabilities.hpp:72
Helper for vector wrap.
Definition probabilities.hpp:27
Wraps prob. vector so that it can be used as matrix.
Definition probabilities.hpp:52
Exponential distribution.
Definition probabilities.hpp:110
"Interface" for distributions that manages variant access
Definition probabilities.hpp:212
Weibull distribution.
Definition probabilities.hpp:134
Definition probabilities.hpp:253
Definition probabilities.hpp:244
Matrix of probabilities for BSS and MSS.
Definition probabilities.hpp:100
Vector of probabilities for BSS.
Definition probabilities.hpp:90