www.mooseframework.org
NormalDistribution.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "NormalDistribution.h"
11 #include "math.h"
12 #include "libmesh/utility.h"
13 
14 registerMooseObject("StochasticToolsApp", NormalDistribution);
15 
17 
18 const std::array<Real, 6> NormalDistribution::_a = {
19  {-0.322232431088, -1.0, -0.342242088547, -0.0204231210245, -0.0000453642210148}};
20 
21 const std::array<Real, 6> NormalDistribution::_b = {
22  {0.099348462606, 0.588581570495, 0.531103462366, 0.10353775285, 0.0038560700634}};
23 
24 InputParameters
26 {
27  InputParameters params = Distribution::validParams();
28  params.addClassDescription("Normal distribution");
29  params.addRequiredParam<Real>("mean", "Mean (or expectation) of the distribution.");
30  params.addRequiredRangeCheckedParam<Real>(
31  "standard_deviation", "standard_deviation > 0", "Standard deviation of the distribution ");
32  return params;
33 }
34 
35 NormalDistribution::NormalDistribution(const InputParameters & parameters)
36  : Distribution(parameters),
37  _mean(getParam<Real>("mean")),
38  _standard_deviation(getParam<Real>("standard_deviation"))
39 {
40 }
41 
42 Real
43 NormalDistribution::pdf(const Real & x, const Real & mean, const Real & std_dev)
44 {
45  return 1.0 / (std_dev * std::sqrt(2.0 * M_PI)) *
46  std::exp(-0.5 * Utility::pow<2>((x - mean) / std_dev));
47 }
48 
49 Real
50 NormalDistribution::cdf(const Real & x, const Real & mean, const Real & std_dev)
51 {
52  return 0.5 * (1.0 + std::erf((x - mean) / (std_dev * std::sqrt(2.0))));
53 }
54 
55 Real
56 NormalDistribution::quantile(const Real & p, const Real & mean, const Real & std_dev)
57 {
58  Real x = (p < 0.5 ? p : 1.0 - p);
59  Real y = std::sqrt(-2.0 * std::log(x));
60  Real sgn = (p - 0.5 < 0.0 ? -1.0 : 1.0);
61  Real Zp = sgn * (y + (_a[0] + _a[1] * y + _a[2] * Utility::pow<2>(y) +
62  _a[3] * Utility::pow<3>(y) + _a[4] * Utility::pow<4>(y)) /
63  (_b[0] + _b[1] * y + _b[2] * Utility::pow<2>(y) +
64  _b[3] * Utility::pow<3>(y) + _b[4] * Utility::pow<4>(y)));
65  return Zp * std_dev + mean;
66 }
67 
68 Real
69 NormalDistribution::pdf(const Real & x) const
70 {
71  TIME_SECTION(_perf_pdf);
72  return pdf(x, _mean, _standard_deviation);
73 }
74 
75 Real
76 NormalDistribution::cdf(const Real & x) const
77 {
78  TIME_SECTION(_perf_cdf);
79  return cdf(x, _mean, _standard_deviation);
80 }
81 
82 Real
83 NormalDistribution::quantile(const Real & p) const
84 {
85  TIME_SECTION(_perf_quantile);
87 }
defineLegacyParams
defineLegacyParams(NormalDistribution)
NormalDistribution.h
registerMooseObject
registerMooseObject("StochasticToolsApp", NormalDistribution)
NormalDistribution::cdf
virtual Real cdf(const Real &x) const override
Definition: NormalDistribution.C:76
NormalDistribution::_a
static const std::array< Real, 6 > _a
Definition: NormalDistribution.h:40
NormalDistribution::_standard_deviation
const Real & _standard_deviation
The standard deviation of the distribution (sigma)
Definition: NormalDistribution.h:48
NormalDistribution::validParams
static InputParameters validParams()
Definition: NormalDistribution.C:25
validParams
InputParameters validParams()
NormalDistribution::NormalDistribution
NormalDistribution(const InputParameters &parameters)
Definition: NormalDistribution.C:35
NormalDistribution::quantile
virtual Real quantile(const Real &p) const override
Definition: NormalDistribution.C:83
NormalDistribution::_b
static const std::array< Real, 6 > _b
Definition: NormalDistribution.h:41
NormalDistribution::_mean
const Real & _mean
The mean (or expectation) of the distribution (mu)
Definition: NormalDistribution.h:45
NormalDistribution
A class used to generate a normal distribution.
Definition: NormalDistribution.h:22
NormalDistribution::pdf
virtual Real pdf(const Real &x) const override
Definition: NormalDistribution.C:69