MUQ  0.4.3
SampleEstimator.cpp
Go to the documentation of this file.
3 
4 #include <Eigen/Core>
5 
6 using namespace muq::Modeling;
7 using namespace muq::SamplingAlgorithms;
8 
9 Eigen::VectorXd SampleEstimator::CentralMoment(unsigned int order,
10  int blockNum) const
11 {
12  return CentralMoment(order, Mean(blockNum), blockNum);
13 }
14 
15 
16 Eigen::VectorXd SampleEstimator::CentralMoment(unsigned int order,
17  Eigen::VectorXd const& mean,
18  int blockNum) const
19 {
20  class Integrand : public muq::Modeling::ModPiece
21  {
22  public:
23  Integrand(Eigen::VectorXd const& meanIn,
24  unsigned int orderIn,
25  int blockIndIn,
26  Eigen::VectorXi const& blockSizes) : muq::Modeling::ModPiece(blockSizes, GetBlockSize(blockSizes,blockIndIn)*Eigen::VectorXi::Ones(1)),
27  localMean(meanIn), localOrder(orderIn), localBlockInd(blockIndIn){};
28 
29  virtual ~Integrand() = default;
30 
31  virtual void EvaluateImpl(ref_vector<Eigen::VectorXd> const& inputs) override
32  {
33  if(localBlockInd<0){
34  outputs.resize(1);
35  outputs.at(0).resize(inputSizes.sum());
36  unsigned int cumSum = 0;
37 
38  for(unsigned int i=0; i<inputSizes.size(); ++i){
39  outputs.at(0).segment(cumSum,inputSizes(i)) = (inputs.at(i).get()-localMean.segment(cumSum,inputSizes(i))).array().pow(localOrder);
40  cumSum += inputSizes(i);
41  }
42 
43  }else{
44  outputs.resize(1);
45  outputs.at(0) = (inputs.at(localBlockInd).get()-localMean).array().pow(localOrder);
46  }
47  };
48 
49  static int GetBlockSize(Eigen::VectorXi const& blockSizes, int blockInd){
50  if(blockInd<0){
51  return blockSizes.sum();
52  }else{
53  return blockSizes(blockInd);
54  }
55  };
56 
57  private:
58  Eigen::VectorXd const& localMean;
59  unsigned int localOrder;
60  int localBlockInd;
61 
62  }; // class integrand
63 
64  // Get the size of each block
65  Eigen::VectorXi blockSizes(NumBlocks());
66  for(int i=0; i<NumBlocks(); ++i)
67  blockSizes(i) = BlockSize(i);
68 
69  // Create an integrand for the central moment
70  auto integrand = std::make_shared<Integrand>(mean, order, blockNum, blockSizes);
71 
72  return ExpectedValue(integrand);
73 }
74 
75 
76 Eigen::VectorXd SampleEstimator::Mean(int blockInd) const
77 {
78  assert(BlockSize(blockInd)>0);
79  return CentralMoment(1, Eigen::VectorXd::Zero(BlockSize(blockInd)), blockInd);
80 }
81 
82 Eigen::VectorXd SampleEstimator::Variance(int blockInd) const
83 {
84  return Variance(Mean(blockInd), blockInd);
85 }
86 
87 Eigen::VectorXd SampleEstimator::Variance(Eigen::VectorXd const& mean,
88  int blockInd) const
89 {
90  return CentralMoment(2, mean, blockInd);
91 }
92 
93 Eigen::VectorXd SampleEstimator::StandardizedMoment(unsigned int order, int blockInd) const
94 {
95  return StandardizedMoment(order, Mean(blockInd), blockInd);
96 }
97 
98 Eigen::VectorXd SampleEstimator::StandardizedMoment(unsigned int order,
99  Eigen::VectorXd const& mean,
100  int blockInd) const
101 {
102  return StandardizedMoment(order, mean, Variance(mean,blockInd).array().sqrt(), blockInd);
103 }
104 
105 Eigen::VectorXd SampleEstimator::StandardizedMoment(unsigned int order,
106  Eigen::VectorXd const& mean,
107  Eigen::VectorXd const& stdDev,
108  int blockInd) const
109 {
110  Eigen::VectorXd moment = CentralMoment(order, mean, blockInd);
111  return moment.array() / stdDev.array().pow(order);
112 }
113 
114 
115 Eigen::VectorXd SampleEstimator::Skewness(int blockInd) const
116 {
117  return StandardizedMoment(3,blockInd);
118 }
119 
120 Eigen::VectorXd SampleEstimator::Skewness(Eigen::VectorXd const& mean,
121  int blockInd) const
122 {
123  return StandardizedMoment(3,mean, blockInd);
124 }
125 
126 Eigen::VectorXd SampleEstimator::Skewness(Eigen::VectorXd const& mean,
127  Eigen::VectorXd const& stdDev,
128  int blockInd) const
129 {
130  return StandardizedMoment(3,mean, stdDev, blockInd);
131 }
132 
133 Eigen::VectorXd SampleEstimator::Kurtosis(int blockInd) const
134 {
135  return StandardizedMoment(4,blockInd);
136 }
137 
138 Eigen::VectorXd SampleEstimator::Kurtosis(Eigen::VectorXd const& mean,
139  int blockInd) const
140 {
141  return StandardizedMoment(4,mean, blockInd);
142 }
143 
144 Eigen::VectorXd SampleEstimator::Kurtosis(Eigen::VectorXd const& mean,
145  Eigen::VectorXd const& stdDev,
146  int blockInd) const
147 {
148  return StandardizedMoment(4,mean, stdDev, blockInd);
149 }
150 
151 
152 Eigen::MatrixXd SampleEstimator::Covariance(int blockInd) const
153 {
154  return Covariance(Mean(blockInd),blockInd);
155 }
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37