Line data Source code
1 : //* This file is part of the MOOSE framework
2 : //* https://mooseframework.inl.gov
3 : //*
4 : //* All rights reserved, see COPYRIGHT for full restrictions
5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 : //*
7 : //* Licensed under LGPL 2.1, please see LICENSE for details
8 : //* https://www.gnu.org/licenses/lgpl-2.1.html
9 :
10 : #include "TruncatedNormal.h"
11 :
12 : registerMooseObject("StochasticToolsApp", TruncatedNormal);
13 :
14 : InputParameters
15 100 : TruncatedNormal::validParams()
16 : {
17 100 : InputParameters params = Normal::validParams();
18 100 : params.addClassDescription("Truncated normal distribution");
19 200 : params.addParam<Real>(
20 200 : "lower_bound", -std::numeric_limits<Real>::max(), "Lower bound of the distribution ");
21 200 : params.addParam<Real>(
22 200 : "upper_bound", std::numeric_limits<Real>::max(), "Upper bound of the distribution ");
23 100 : return params;
24 0 : }
25 :
26 50 : TruncatedNormal::TruncatedNormal(const InputParameters & parameters)
27 : : Normal(parameters),
28 50 : _lower_bound(getParam<Real>("lower_bound")),
29 150 : _upper_bound(getParam<Real>("upper_bound"))
30 : {
31 50 : if (_lower_bound >= _upper_bound)
32 0 : mooseError("lower_bound in truncated normal distribution must be less than upper_bound.");
33 50 : }
34 :
35 : Real
36 83 : TruncatedNormal::pdf(const Real & x,
37 : const Real & mean,
38 : const Real & std_dev,
39 : const Real & lower_bound,
40 : const Real & upper_bound)
41 : {
42 83 : if (x <= lower_bound || x >= upper_bound)
43 : return 0.0;
44 : else
45 83 : return (Normal::pdf(x, mean, std_dev)) /
46 83 : (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev));
47 : }
48 :
49 : Real
50 17 : TruncatedNormal::cdf(const Real & x,
51 : const Real & mean,
52 : const Real & std_dev,
53 : const Real & lower_bound,
54 : const Real & upper_bound)
55 : {
56 :
57 17 : if (x <= lower_bound || x >= upper_bound)
58 : return 0.0;
59 : else
60 17 : return (Normal::cdf(x, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev)) /
61 17 : (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev));
62 : }
63 :
64 : Real
65 647 : TruncatedNormal::quantile(const Real & p,
66 : const Real & mean,
67 : const Real & std_dev,
68 : const Real & lower_bound,
69 : const Real & upper_bound)
70 : {
71 647 : return Normal::quantile(
72 647 : Normal::cdf(lower_bound, mean, std_dev) +
73 647 : p * (Normal::cdf(upper_bound, mean, std_dev) - Normal::cdf(lower_bound, mean, std_dev)),
74 : mean,
75 647 : std_dev);
76 : }
77 :
78 : Real
79 17 : TruncatedNormal::pdf(const Real & x) const
80 : {
81 17 : return pdf(x, _mean, _standard_deviation, _lower_bound, _upper_bound);
82 : }
83 :
84 : Real
85 17 : TruncatedNormal::cdf(const Real & x) const
86 : {
87 17 : return cdf(x, _mean, _standard_deviation, _lower_bound, _upper_bound);
88 : }
89 :
90 : Real
91 647 : TruncatedNormal::quantile(const Real & p) const
92 : {
93 647 : return quantile(p, _mean, _standard_deviation, _lower_bound, _upper_bound);
94 : }
|