Quantum++  v0.8.6
C++11 quantum computing library
statistics.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef STATISTICS_H_
28 #define STATISTICS_H_
29 
30 // Collection of statistics-related functions
31 
32 namespace qpp
33 {
34 
41 inline std::vector<double> uniform(idx N)
42 {
43  // EXCEPTION CHECKS
44 
45  if (N == 0)
46  throw Exception("qpp::uniform", Exception::Type::ZERO_SIZE);
47  // END EXCEPTION CHECKS
48 
49  return std::vector<double>(N, 1. / N);
50 }
51 
60 inline std::vector<double> marginalX(const dmat& probXY)
61 {
62  // EXCEPTION CHECKS
63 
64  if (!internal::_check_nonzero_size(probXY))
65  throw Exception("qpp::marginalX", Exception::Type::ZERO_SIZE);
66  // END EXCEPTION CHECKS
67 
68  std::vector<double> result(probXY.rows(), 0);
69  for (idx i = 0; i < static_cast<idx>(probXY.rows()); ++i)
70  {
71  for (idx j = 0; j < static_cast<idx>(probXY.cols()); ++j)
72  {
73  result[i] += probXY(i, j);
74  }
75  }
76 
77  return result;
78 }
79 
88 inline std::vector<double> marginalY(const dmat& probXY)
89 {
90  // EXCEPTION CHECKS
91 
92  if (!internal::_check_nonzero_size(probXY))
93  throw Exception("qpp::marginalY", Exception::Type::ZERO_SIZE);
94  // END EXCEPTION CHECKS
95 
96  return marginalX(probXY.transpose());
97 }
98 
108 template<typename Container>
109 double avg(const std::vector<double>& prob, const Container& X)
110 {
111  // EXCEPTION CHECKS
112 
114  throw Exception("qpp:avg", Exception::Type::ZERO_SIZE);
115  if (!internal::_check_matching_sizes(prob, X))
116  throw Exception("qpp:avg", Exception::Type::SIZE_MISMATCH);
117  // END EXCEPTION CHECKS
118 
119  double result = 0;
120  for (idx i = 0; i < prob.size(); ++i)
121  result += prob[i] * X[i];
122 
123  return result;
124 }
125 
136 template<typename Container>
137 double cov(const dmat& probXY,
138  const Container& X,
139  const Container& Y)
140 {
141  // EXCEPTION CHECKS
142 
144  throw Exception("qpp:cov", Exception::Type::ZERO_SIZE);
145  if (static_cast<idx>(probXY.rows()) != X.size() ||
146  static_cast<idx>(probXY.cols()) != Y.size())
147  throw Exception("qpp:cov", Exception::Type::SIZE_MISMATCH);
148  // END EXCEPTION CHECKS
149 
150  std::vector<double> probX = marginalX(probXY); // marginals
151  std::vector<double> probY = marginalY(probXY); // marginals
152 
153  double result = 0;
154  for (idx i = 0; i < X.size(); ++i)
155  {
156  for (idx j = 0; j < Y.size(); ++j)
157  {
158  result += probXY(i, j) * X[i] * Y[j];
159  }
160  }
161 
162  return result - avg(probX, X) * avg(probY, Y);
163 }
164 
173 template<typename Container>
174 double var(const std::vector<double>& prob, const Container& X)
175 {
176  // EXCEPTION CHECKS
177 
179  throw Exception("qpp:var", Exception::Type::ZERO_SIZE);
180  if (!internal::_check_matching_sizes(prob, X))
181  throw Exception("qpp:var", Exception::Type::SIZE_MISMATCH);
182  // END EXCEPTION CHECKS
183 
184  Eigen::VectorXd diag(prob.size());
185  for (idx i = 0; i < prob.size(); ++i)
186  diag(i) = prob[i];
187  dmat probXX = diag.asDiagonal();
188 
189  return cov(probXX, X, X);
190 }
191 
200 template<typename Container>
201 double sigma(const std::vector<double>& prob, const Container& X)
202 {
203  // EXCEPTION CHECKS
204 
206  throw Exception("qpp:sigma", Exception::Type::ZERO_SIZE);
207  if (!internal::_check_matching_sizes(prob, X))
208  throw Exception("qpp:sigma", Exception::Type::SIZE_MISMATCH);
209  // END EXCEPTION CHECKS
210 
211  return std::sqrt(var(prob, X));
212 }
213 
224 template<typename Container>
225 double cor(const dmat& probXY,
226  const Container& X,
227  const Container& Y)
228 {
229  // EXCEPTION CHECKS
230 
232  throw Exception("qpp:cor", Exception::Type::ZERO_SIZE);
233  if (static_cast<idx>(probXY.rows()) != X.size() ||
234  static_cast<idx>(probXY.cols()) != Y.size())
235  throw Exception("qpp:cor", Exception::Type::SIZE_MISMATCH);
236  // END EXCEPTION CHECKS
237 
238  return cov(probXY, X, Y) / (sigma(marginalX(probXY), X) *
239  sigma(marginalX(probXY), Y));
240 }
241 
242 } /* namespace qpp */
243 
244 #endif /* STATISTICS_H_ */
245 
std::vector< double > uniform(idx N)
Uniform probability distribution vector.
Definition: statistics.h:41
bool _check_matching_sizes(const T1 &lhs, const T2 &rhs) noexcept
Definition: util.h:120
std::vector< double > marginalX(const dmat &probXY)
Marginal distribution.
Definition: statistics.h:60
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:71
double cov(const dmat &probXY, const Container &X, const Container &Y)
Covariance.
Definition: statistics.h:137
Quantum++ main namespace.
Definition: codes.h:30
double var(const std::vector< double > &prob, const Container &X)
Variance.
Definition: statistics.h:174
double avg(const std::vector< double > &prob, const Container &X)
Average.
Definition: statistics.h:109
double sigma(const std::vector< double > &prob, const Container &X)
Standard deviation.
Definition: statistics.h:201
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:113
std::vector< double > marginalY(const dmat &probXY)
Marginal distribution.
Definition: statistics.h:88
std::size_t idx
Non-negative integer index.
Definition: types.h:36
double cor(const dmat &probXY, const Container &X, const Container &Y)
Correlation.
Definition: statistics.h:225