https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KernelDensity1D.C
Go to the documentation of this file.
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 "KernelDensity1D.h"
11#include "math.h"
12#include "libmesh/utility.h"
13#include "DelimitedFileReader.h"
14#include "SystemBase.h"
15#include "Assembly.h"
16#include "Normal.h"
17#include "Uniform.h"
18
19registerMooseObject("StochasticToolsApp", KernelDensity1D);
20
23{
25 MooseEnum bandwidth_rule("silverman standarddeviation userdefined");
26 MooseEnum kernel_function("gaussian uniform");
27 params.addClassDescription("KernelDensity1D distribution");
29 "bandwidth_rule", bandwidth_rule, "Bandwidth rule for evaluating the bandwith.");
31 "kernel_function",
32 kernel_function,
33 "Kernel function determines the shape of the underlying kernel for the kernel density.");
34 params.addRangeCheckedParam<Real>("bandwidth",
35 1.0,
36 "bandwidth > 0",
37 "Bandwidth controls the smoothness of the kernel density.");
38 params.addParam<std::vector<Real>>("data", "The data vector.");
39 params.addParam<FileName>("file_name", "Name of the CSV file.");
40 params.addParam<std::string>(
41 "file_column_name", "Name of column in csv file to use, by default first column is used.");
42 return params;
43}
44
46 : Distribution(parameters),
47 _bandwidth_rule(getParam<MooseEnum>("bandwidth_rule")),
48 _kernel_function(getParam<MooseEnum>("kernel_function")),
49 _bandwidth(getParam<Real>("bandwidth"))
50{
51 if (isParamValid("data") && isParamValid("file_name"))
52 paramError("data", "data and file_name both cannot be set at the same time.");
53 else if (isParamValid("file_name"))
54 {
55 MooseUtils::DelimitedFileReader reader(getParam<FileName>("file_name"));
56 reader.read();
57 if (isParamValid("file_column_name"))
58 _data = reader.getData(getParam<std::string>("file_column_name"));
59 else
60 _data = reader.getData(0);
61 }
62 else if (isParamValid("data"))
63 _data = getParam<std::vector<Real>>("data");
64 else
65 mooseError("Either 'data' or 'file_name' parameters must be specified to represent input data");
66 Real mu = 0;
67 Real sd = 0;
68 for (unsigned i = 0; i < _data.size(); ++i)
69 {
70 mu += _data[i] / _data.size();
71 }
72 for (unsigned i = 0; i < _data.size(); ++i)
73 {
74 sd += Utility::pow<2>((_data[i] - mu)) / _data.size();
75 }
76 sd = std::pow(sd, 0.5);
77 if (_bandwidth_rule == "silverman")
78 {
79 _bandwidth = 1.06 * sd * std::pow(_data.size(), -0.2);
80 }
81 else if (_bandwidth_rule == "standarddeviation")
82 {
83 _bandwidth = sd;
84 }
85}
86
87Real
89 const Real & bandwidth,
90 const std::vector<Real> & data,
91 const MooseEnum & kernel_function)
92{
93 Real value = 0;
94 if (kernel_function == "gaussian")
95 {
96 for (unsigned i = 0; i < data.size(); ++i)
97 {
98 value += 1 / (data.size() * bandwidth) * Normal::pdf(((x - data[i]) / bandwidth), 0.0, 1.0);
99 }
100 }
101 else if (kernel_function == "uniform")
102 {
103 for (unsigned i = 0; i < data.size(); ++i)
104 {
105 value += 1 / (data.size() * bandwidth) * Uniform::pdf(((x - data[i]) / bandwidth), -1.0, 1.0);
106 }
107 }
108 else
109 ::mooseError("Invalid kernel function type ", std::string(kernel_function));
110 return value;
111}
112
113Real
115 const Real & bandwidth,
116 const std::vector<Real> & data,
117 const MooseEnum & kernel_function)
118{
119 Real value = 0;
120 if (kernel_function == "gaussian")
121 {
122 for (unsigned i = 0; i < data.size(); ++i)
123 {
124 value += 1.0 / (data.size()) * Normal::cdf(x, data[i], bandwidth);
125 }
126 }
127 else if (kernel_function == "uniform")
128 {
129 for (unsigned i = 0; i < data.size(); ++i)
130 {
131 value += 1.0 / (data.size()) * Uniform::cdf((x - data[i]) / bandwidth, -1.0, 1.0);
132 }
133 }
134 else
135 ::mooseError("Invalid kernel function type ", std::string(kernel_function));
136 return value;
137}
138
139Real
141 const Real & bandwidth,
142 const std::vector<Real> & data,
143 const MooseEnum & kernel_function)
144{
145 Real value = 0;
146 if (kernel_function == "gaussian")
147 {
148 int index = std::round(p * (data.size() - 1));
149 value = (Normal::quantile(p, data[index], bandwidth));
150 }
151 else if (kernel_function == "uniform")
152 {
153 int index = std::round(p * (data.size() - 1));
154 value = (Uniform::quantile(p, -1.0, 1.0) * bandwidth + data[index]);
155 }
156 else
157 ::mooseError("Invalid kernel function type ", std::string(kernel_function));
158 return value;
159}
160
161Real
162KernelDensity1D::pdf(const Real & x) const
163{
165}
166
167Real
168KernelDensity1D::cdf(const Real & x) const
169{
171}
172
173Real
174KernelDensity1D::quantile(const Real & p) const
175{
177}
const std::vector< double > x
const double mu
const Real p
registerMooseObject("StochasticToolsApp", KernelDensity1D)
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
A class used to generate a KernelDensity1D distribution.
KernelDensity1D(const InputParameters &parameters)
virtual Real pdf(const Real &x) const override
std::vector< Real > _data
data helps get the vector data for building the kernel density
const MooseEnum & _kernel_function
kernel_function helps the user select between the different kernel functions that are available
Real _bandwidth
The bandwith parameter which controls the smoothness of the distribution.
const MooseEnum & _bandwidth_rule
bandwidth_rule helps the user select between the different ways to define the bandwith
virtual Real quantile(const Real &p) const override
virtual Real cdf(const Real &x) const override
static InputParameters validParams()
void paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
bool isParamValid(const std::string &name) const
const std::vector< std::vector< T > > & getData() const
virtual Real cdf(const Real &x) const override
Definition Normal.C:74
virtual Real pdf(const Real &x) const override
Definition Normal.C:68
virtual Real quantile(const Real &p) const override
Definition Normal.C:80
static Real quantile(const Real &y, const Real &lower_bound, const Real &upper_bound)
Definition Uniform.C:54
static Real pdf(const Real &x, const Real &lower_bound, const Real &upper_bound)
Definition Uniform.C:34
static Real cdf(const Real &x, const Real &lower_bound, const Real &upper_bound)
Definition Uniform.C:43