https://mooseframework.inl.gov
Standardizer.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 "Standardizer.h"
11 
12 namespace StochasticTools
13 {
14 
15 void
16 Standardizer::set(const Real & n)
17 {
18  _mean.clear();
19  _stdev.clear();
20  for (unsigned int ii = 0; ii < n; ++ii)
21  {
22  _mean.push_back(0);
23  _stdev.push_back(1);
24  }
25 }
26 
27 void
28 Standardizer::set(const Real & mean, const Real & stdev)
29 {
30  _mean.clear();
31  _stdev.clear();
32  _mean.push_back(mean);
33  _stdev.push_back(stdev);
34 }
35 
36 void
37 Standardizer::set(const Real & mean, const Real & stdev, const Real & n)
38 {
39  _mean.clear();
40  _stdev.clear();
41  for (unsigned int ii = 0; ii < n; ++ii)
42  {
43  _mean.push_back(mean);
44  _stdev.push_back(stdev);
45  }
46 }
47 
48 void
49 Standardizer::set(const std::vector<Real> & mean, const std::vector<Real> & stdev)
50 {
51  mooseAssert(mean.size() == stdev.size(),
52  "Provided mean and standard deviation vectors are of differing size.");
53  _mean = mean;
54  _stdev = stdev;
55 }
56 
57 void
59 {
60  _mean.clear();
61  _stdev.clear();
62  unsigned int num_samples = input.rows();
63  unsigned int n = input.cols();
64  // comptue mean
65  RealEigenVector mean = input.colwise().mean();
66  // Compute standard deviation
67  RealEigenVector stdev =
68  ((input.rowwise() - mean.transpose()).colwise().squaredNorm() / num_samples)
69  .transpose()
70  .array()
71  .sqrt();
72  // Store in std:vector format
73  _mean.resize(n);
74  _stdev.resize(n);
75  RealEigenVector::Map(&_mean[0], n) = mean;
76  RealEigenVector::Map(&_stdev[0], n) = stdev;
77 }
78 
79 void
81 {
82  Eigen::Map<const RealEigenVector> mean(_mean.data(), _mean.size());
83  Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
84  input = (input.rowwise() - mean.transpose()).array().rowwise() / stdev.transpose().array();
85 }
86 
87 void
89 {
90  Eigen::Map<const RealEigenVector> mean(_mean.data(), _mean.size());
91  Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
92  input =
93  (input.array().rowwise() * stdev.transpose().array()).rowwise() + mean.transpose().array();
94 }
95 
96 void
98 {
99  Eigen::Map<const RealEigenVector> stdev(_stdev.data(), _stdev.size());
100  input = input.array().rowwise() * stdev.transpose().array();
101 }
102 
104 void
105 Standardizer::storeHelper(std::ostream & stream, void * context) const
106 {
107  unsigned int n = _mean.size();
108  dataStore(stream, n, context);
109  for (unsigned int ii = 0; ii < n; ++ii)
110  dataStore(stream, _mean[ii], context);
111  for (unsigned int ii = 0; ii < n; ++ii)
112  dataStore(stream, _stdev[ii], context);
113 }
114 
115 } // StochasticTools namespace
116 
117 template <>
118 void
119 dataStore(std::ostream & stream, StochasticTools::Standardizer & standardizer, void * context)
120 {
121  standardizer.storeHelper(stream, context);
122 }
123 
124 template <>
125 void
126 dataLoad(std::istream & stream, StochasticTools::Standardizer & standardizer, void * context)
127 {
128  unsigned int n;
129  dataLoad(stream, n, context);
130  std::vector<Real> mean(n);
131  std::vector<Real> stdev(n);
132  for (unsigned int ii = 0; ii < n; ++ii)
133  dataLoad(stream, mean[ii], context);
134  for (unsigned int ii = 0; ii < n; ++ii)
135  dataLoad(stream, stdev[ii], context);
136  standardizer.set(mean, stdev);
137 }
void storeHelper(std::ostream &stream, void *context) const
Helper for dataStore.
Definition: Standardizer.C:105
void getDescaled(RealEigenMatrix &input) const
De-scales the assumed scaled input.
Definition: Standardizer.C:97
void dataLoad(std::istream &stream, StochasticTools::Standardizer &standardizer, void *context)
Definition: Standardizer.C:126
void getStandardized(RealEigenMatrix &input) const
Returns the standardized (centered and scaled) of the provided input.
Definition: Standardizer.C:80
void computeSet(const RealEigenMatrix &input)
Methods for computing and setting mean and standard deviation.
Definition: Standardizer.C:58
Enum for batch type in stochastic tools MultiApp.
std::vector< Real > _stdev
Definition: Standardizer.h:58
void getDestandardized(RealEigenMatrix &input) const
De-standardizes (de-centered and de-scaled) the assumed standardized input.
Definition: Standardizer.C:88
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
std::vector< Real > _mean
Definition: Standardizer.h:57
void set(const Real &n)
Methods for setting mean and standard deviation directly Sets mean=0, std=1 for n variables...
Definition: Standardizer.C:16
Class for standardizing data (centering and scaling)
Definition: Standardizer.h:21
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealEigenVector
void dataStore(std::ostream &stream, StochasticTools::Standardizer &standardizer, void *context)
Definition: Standardizer.C:119