PandA-2024.02
Statistics.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
43 #include <boost/math/special_functions/fpclassify.hpp>
44 #include <cmath>
45 
46 #include "Statistics.hpp"
47 
48 boost::math::normal MultiplyVarForCoefficient(int coeff, boost::math::normal var)
49 {
50  double mean, st_dev;
51  mean = var.mean() * coeff;
52  st_dev = var.standard_deviation() * coeff;
53  boost::math::normal s(mean, st_dev);
54  return s;
55 }
56 
57 boost::math::normal VarSum(boost::math::normal x, double c)
58 {
59  boost::math::normal s((x.mean() + c), x.standard_deviation());
60  return s;
61 }
62 
63 boost::math::normal VarSum(boost::math::normal x, boost::math::normal y)
64 {
65  return VarSum(x, y, 0);
66 }
67 
68 boost::math::normal VarSum(boost::math::normal x, boost::math::normal y, double p)
69 {
70  double mean, st_dev;
71  mean = x.mean() + y.mean();
72 
73  // Mathematically, Cov(X,Y) = p * StandardVariation(x) * StandardVariation(y)
74  st_dev = sqrt(pow(x.standard_deviation(), 2) + pow(y.standard_deviation(), 2) +
75  2 * p * x.standard_deviation() * y.standard_deviation());
76  boost::math::normal s(mean, st_dev);
77  return s;
78 }
79 
80 boost::math::lognormal VarSum(std::vector<boost::math::lognormal> v)
81 {
82  if(!v.empty())
83  {
84  boost::math::lognormal s = v.at(0);
85  for(unsigned int i = 1; i < v.size(); i++)
86  {
87  s = VarSum(s, v.at(i));
88  }
89  return s;
90  }
91  return boost::math::lognormal();
92 }
93 
94 boost::math::lognormal VarSum(boost::math::lognormal x, boost::math::lognormal y)
95 {
96  return VarSum(x, y, 0);
97 }
98 
99 boost::math::lognormal VarSum(boost::math::lognormal s1, boost::math::lognormal s2, double p)
100 {
101  double mean, st_dev;
102  double u1, u2;
103 
104  u1 = exp(s1.location() + (pow(s1.scale(), 2)) / 2) + exp(s2.location() + (pow(s2.scale(), 2)) / 2);
105  u2 = exp(2 * s1.location() + 2 * pow(s1.scale(), 2)) + exp(2 * s2.location() + 2 * pow(s2.scale(), 2)) +
106  2 * exp(s1.location() + s2.location()) *
107  exp(0.5 * (pow(s1.scale(), 2) + pow(s2.scale(), 2) + 2 * p * s1.scale() * s2.scale()));
108 
109  mean = 2 * log(u1) - 0.5 * log(u2);
110  st_dev = sqrt(log(u2) - 2 * log(u1));
111 
112  boost::math::lognormal s(mean, st_dev);
113  return s;
114 }
115 
116 boost::math::normal VarMax(boost::math::normal x, boost::math::normal y, double p)
117 {
118  double mean = 0, st_dev = 0, var = 0;
119  double a = 0, alpha = 0;
120 
121  a = pow((pow(x.standard_deviation(), 2) + pow(y.standard_deviation(), 2) -
122  2 * p * x.standard_deviation() * y.standard_deviation()),
123  0.5);
124  alpha = (x.mean() + y.mean()) / a;
125 
126  mean = x.mean() * cdf(x, alpha) + y.mean() * cdf(y, -alpha) + a * pdf(x, alpha);
127  var = (pow(x.standard_deviation(), 2) + pow(x.mean(), 2)) * cdf(x, alpha) +
128  (pow(y.standard_deviation(), 2) + pow(y.mean(), 2)) * cdf(y, -alpha) +
129  (x.mean() + y.mean()) * a * pdf(x, alpha) - pow(mean, 2);
130 
131  st_dev = sqrt(var);
132  boost::math::normal s(mean, st_dev);
133  return s;
134 }
135 
136 boost::math::normal VarMax(std::vector<boost::math::normal> v)
137 {
138  if(!v.empty())
139  {
140  boost::math::normal s = v.at(0);
141  for(unsigned int i = 1; i < v.size(); i++)
142  {
143  s = VarMax(s, v.at(i), 0);
144  }
145  return s;
146  }
147  return boost::math::normal();
148 }
149 
150 boost::math::normal ComputeStatisticalDelay(double d, int n)
151 {
152  return CreateStatisticalAttribute(d, n);
153 }
154 
155 boost::math::lognormal ComputeStatisticalPower(double p, int n)
156 {
157  boost::math::normal s = CreateStatisticalAttribute(p, n);
158  boost::math::lognormal v(s.mean(), s.standard_deviation());
159  return v;
160 }
161 
162 boost::math::normal CreateStatisticalAttribute(double a, int n)
163 {
164  std::vector<int> v;
165  v.reserve(static_cast<size_t>(n));
166  for(int i = 0; i < n; i++)
167  {
168  v.push_back(1);
169  }
170 
171  return CreateStatisticalAttribute(a, v, 0, 1, 1, 0, 1);
172 }
173 
174 boost::math::normal CreateStatisticalAttribute(double a, std::vector<int> d, double mean, double st_dev, int d_rand,
175  double mean_rand, double st_dev_rand)
176 {
177  boost::math::normal ris;
178  boost::math::normal x_rand(mean_rand, st_dev_rand);
179  ris = MultiplyVarForCoefficient(d_rand, x_rand);
180 
181  for(int i : d)
182  {
183  boost::math::normal temp(mean, st_dev);
184  ris = VarSum(ris, MultiplyVarForCoefficient(i, temp));
185  }
186 
187  return VarSum(ris, a);
188 }
boost::math::normal MultiplyVarForCoefficient(int coeff, boost::math::normal var)
Definition: Statistics.cpp:48
boost::math::normal CreateStatisticalAttribute(double a, int n)
Definition: Statistics.cpp:162
boost::math::normal VarSum(boost::math::normal x, double c)
Definition: Statistics.cpp:57
boost::math::lognormal ComputeStatisticalPower(double p, int n)
Definition: Statistics.cpp:155
This file collects some algebric statistical functions.
boost::math::normal ComputeStatisticalDelay(double d, int n)
Definition: Statistics.cpp:150
boost::math::normal VarMax(boost::math::normal x, boost::math::normal y, double p)
Definition: Statistics.cpp:116
x
Return the smallest n such that 2^n >= _x.
uint32_t exp

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13