|
|
| 1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2008 Timo Bingmann |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 2 as |
| 7 |
* published by the Free Software Foundation; |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
* |
| 18 |
* Author: Timo Bingmann <timo.bingmann@student.kit.edu> |
| 19 |
*/ |
| 20 |
#include "ns3/random-variable.h" |
| 21 |
#include "ns3/gnuplot.h" |
| 22 |
#include <map> |
| 23 |
#include <cmath> |
| 24 |
|
| 25 |
using namespace ns3; |
| 26 |
|
| 27 |
/// Round a double number to the given precision. e.g. dround(0.234, 0.1) = 0.2 |
| 28 |
/// and dround(0.257, 0.1) = 0.3 |
| 29 |
double dround(double number, double precision) |
| 30 |
{ |
| 31 |
number /= precision; |
| 32 |
if (number >= 0) |
| 33 |
number = std::floor(number + 0.5); |
| 34 |
else |
| 35 |
number = std::ceil(number - 0.5); |
| 36 |
number *= precision; |
| 37 |
return number; |
| 38 |
} |
| 39 |
|
| 40 |
static GnuplotDataset |
| 41 |
Histogramm (RandomVariable rndvar, unsigned int probes, double precision, const std::string& title, bool notcontinous = false) |
| 42 |
{ |
| 43 |
typedef std::map<double, unsigned int> histogramm_maptype; |
| 44 |
histogramm_maptype histogramm; |
| 45 |
|
| 46 |
for(unsigned int i = 0; i < probes; ++i) |
| 47 |
{ |
| 48 |
double val = dround( rndvar.GetValue(), precision ); |
| 49 |
|
| 50 |
++histogramm[val]; |
| 51 |
} |
| 52 |
|
| 53 |
Gnuplot2dDataset data; |
| 54 |
data.SetTitle(title); |
| 55 |
|
| 56 |
if (notcontinous) |
| 57 |
{ |
| 58 |
data.SetStyle(Gnuplot2dDataset::IMPULSES); |
| 59 |
} |
| 60 |
|
| 61 |
for(histogramm_maptype::const_iterator hi = histogramm.begin(); |
| 62 |
hi != histogramm.end(); ++hi) |
| 63 |
{ |
| 64 |
data.Add(hi->first, (double)hi->second / (double)probes / precision); |
| 65 |
} |
| 66 |
|
| 67 |
return data; |
| 68 |
} |
| 69 |
|
| 70 |
int main (int argc, char *argv[]) |
| 71 |
{ |
| 72 |
unsigned int probes = 1000000; |
| 73 |
double precision = 0.01; |
| 74 |
|
| 75 |
GnuplotCollection gnuplots("main-random-variables.pdf"); |
| 76 |
gnuplots.SetTerminal("pdf enhanced"); |
| 77 |
|
| 78 |
{ |
| 79 |
Gnuplot plot; |
| 80 |
plot.SetTitle("UniformVariable"); |
| 81 |
plot.AppendExtra("set yrange [0:]"); |
| 82 |
|
| 83 |
plot.AddDataset( Histogramm(UniformVariable(0.0, 1.0), probes, precision, |
| 84 |
"UniformVariable [0.0 .. 1.0)") ); |
| 85 |
|
| 86 |
plot.AddDataset( Gnuplot2dFunction("0.1", |
| 87 |
"0 <= x && x <= 1 ? 1.0 : 0") ); |
| 88 |
|
| 89 |
gnuplots.AddPlot(plot); |
| 90 |
} |
| 91 |
|
| 92 |
{ |
| 93 |
Gnuplot plot; |
| 94 |
plot.SetTitle("ExponentialVariable"); |
| 95 |
plot.AppendExtra("set xrange [0:8]"); |
| 96 |
plot.AppendExtra("ExpDist(x,l) = 1/l * exp(-1/l * x)"); |
| 97 |
|
| 98 |
plot.AddDataset( Histogramm(ExponentialVariable(0.5), probes, precision, |
| 99 |
"ExponentialVariable m=0.5") ); |
| 100 |
|
| 101 |
plot.AddDataset( Gnuplot2dFunction("ExponentialDistribution mean 0.5", |
| 102 |
"ExpDist(x, 0.5)") ); |
| 103 |
|
| 104 |
plot.AddDataset( Histogramm(ExponentialVariable(1.0), probes, precision, |
| 105 |
"ExponentialVariable m=1") ); |
| 106 |
|
| 107 |
plot.AddDataset( Gnuplot2dFunction("ExponentialDistribution mean 1.0", |
| 108 |
"ExpDist(x, 1.0)") ); |
| 109 |
|
| 110 |
plot.AddDataset( Histogramm(ExponentialVariable(1.5), probes, precision, |
| 111 |
"ExponentialVariable m=1.5") ); |
| 112 |
|
| 113 |
plot.AddDataset( Gnuplot2dFunction("ExponentialDistribution mean 1.5", |
| 114 |
"ExpDist(x, 1.5)") ); |
| 115 |
|
| 116 |
gnuplots.AddPlot(plot); |
| 117 |
} |
| 118 |
|
| 119 |
{ |
| 120 |
Gnuplot plot; |
| 121 |
plot.SetTitle("ParetoVariable"); |
| 122 |
plot.AppendExtra("set xrange [0:2]"); |
| 123 |
|
| 124 |
plot.AddDataset( Histogramm(ParetoVariable(1.0, 1.5), probes, precision, |
| 125 |
"ParetoVariable m=1.0 s=1.5") ); |
| 126 |
|
| 127 |
plot.AddDataset( Histogramm(ParetoVariable(1.0, 2.0), probes, precision, |
| 128 |
"ParetoVariable m=1.0 s=2.0") ); |
| 129 |
|
| 130 |
plot.AddDataset( Histogramm(ParetoVariable(1.0, 2.5), probes, precision, |
| 131 |
"ParetoVariable m=1.0 s=2.5") ); |
| 132 |
|
| 133 |
gnuplots.AddPlot(plot); |
| 134 |
} |
| 135 |
|
| 136 |
{ |
| 137 |
Gnuplot plot; |
| 138 |
plot.SetTitle("WeibullVariable"); |
| 139 |
plot.AppendExtra("set xrange [0:3]"); |
| 140 |
|
| 141 |
plot.AddDataset( Histogramm(WeibullVariable(1.0, 1.0), probes, precision, |
| 142 |
"WeibullVariable m=1.0 s=1.0") ); |
| 143 |
|
| 144 |
plot.AddDataset( Histogramm(WeibullVariable(1.0, 2.0), probes, precision, |
| 145 |
"WeibullVariable m=1.0 s=2.0") ); |
| 146 |
|
| 147 |
plot.AddDataset( Histogramm(WeibullVariable(1.0, 3.0), probes, precision, |
| 148 |
"WeibullVariable m=1.0 s=3.0") ); |
| 149 |
|
| 150 |
gnuplots.AddPlot(plot); |
| 151 |
} |
| 152 |
|
| 153 |
{ |
| 154 |
Gnuplot plot; |
| 155 |
plot.SetTitle("NormalVariable"); |
| 156 |
plot.AppendExtra("set xrange [-3:3]"); |
| 157 |
plot.AppendExtra("NormalDist(x,m,s) = 1 / (s * sqrt(2*pi)) * exp(-1.0 / 2.0 * ((x-m) / s)**2)"); |
| 158 |
|
| 159 |
plot.AddDataset( Histogramm(NormalVariable(0.0, 1.0), probes, precision, |
| 160 |
"NormalVariable m=0.0 v=1.0") ); |
| 161 |
|
| 162 |
plot.AddDataset( Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=1.0", |
| 163 |
"NormalDist(x,0.0,1.0)") ); |
| 164 |
|
| 165 |
plot.AddDataset( Histogramm(NormalVariable(0.0, 2.0), probes, precision, |
| 166 |
"NormalVariable m=0.0 v=2.0") ); |
| 167 |
|
| 168 |
plot.AddDataset( Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(2.0)", |
| 169 |
"NormalDist(x,0.0,sqrt(2.0))") ); |
| 170 |
|
| 171 |
plot.AddDataset( Histogramm(NormalVariable(0.0, 3.0), probes, precision, |
| 172 |
"NormalVariable m=0.0 v=3.0") ); |
| 173 |
|
| 174 |
plot.AddDataset( Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(3.0)", |
| 175 |
"NormalDist(x,0.0,sqrt(3.0))") ); |
| 176 |
|
| 177 |
gnuplots.AddPlot(plot); |
| 178 |
} |
| 179 |
|
| 180 |
{ |
| 181 |
Gnuplot plot; |
| 182 |
plot.SetTitle("EmpiricalVariable"); |
| 183 |
plot.AppendExtra("set xrange [*:*]"); |
| 184 |
|
| 185 |
EmpiricalVariable emp1; |
| 186 |
emp1.CDF(0.0, 0.0 / 15.0); |
| 187 |
emp1.CDF(0.2, 1.0 / 15.0); |
| 188 |
emp1.CDF(0.4, 3.0 / 15.0); |
| 189 |
emp1.CDF(0.6, 6.0 / 15.0); |
| 190 |
emp1.CDF(0.8, 10.0 / 15.0); |
| 191 |
emp1.CDF(1.0, 15.0 / 15.0); |
| 192 |
|
| 193 |
plot.AddDataset( Histogramm(emp1, probes, precision, |
| 194 |
"EmpiricalVariable (Stairs)") ); |
| 195 |
|
| 196 |
gnuplots.AddPlot(plot); |
| 197 |
} |
| 198 |
|
| 199 |
{ |
| 200 |
Gnuplot plot; |
| 201 |
plot.SetTitle("DeterministicVariable"); |
| 202 |
plot.AppendExtra("set xrange [*:*]"); |
| 203 |
|
| 204 |
double values[] = { 0.0, 0.2, 0.2, 0.4, 0.2, 0.6, 0.8, 0.8, 1.0 }; |
| 205 |
DeterministicVariable det1 (values, sizeof(values) / sizeof(values[0])); |
| 206 |
|
| 207 |
plot.AddDataset( Histogramm(det1, probes, precision, |
| 208 |
"DeterministicVariable", true) ); |
| 209 |
|
| 210 |
gnuplots.AddPlot(plot); |
| 211 |
} |
| 212 |
|
| 213 |
{ |
| 214 |
Gnuplot plot; |
| 215 |
plot.SetTitle("LogNormalVariable"); |
| 216 |
plot.AppendExtra("set xrange [0:3]"); |
| 217 |
|
| 218 |
plot.AppendExtra("LogNormalDist(x,m,s) = 1.0/x * NormalDist(log(x), m, s)"); |
| 219 |
|
| 220 |
plot.AddDataset( Histogramm(LogNormalVariable(0.0, 1.0), probes, precision, |
| 221 |
"LogNormalVariable m=0.0 s=1.0") ); |
| 222 |
|
| 223 |
plot.AddDataset( Gnuplot2dFunction("LogNormalDist(x, 0.0, 1.0)", |
| 224 |
"LogNormalDist(x, 0.0, 1.0)") ); |
| 225 |
|
| 226 |
plot.AddDataset( Histogramm(LogNormalVariable(0.0, 0.5), probes, precision, |
| 227 |
"LogNormalVariable m=0.0 s=0.5") ); |
| 228 |
|
| 229 |
plot.AddDataset( Histogramm(LogNormalVariable(0.0, 0.25), probes, precision, |
| 230 |
"LogNormalVariable m=0.0 s=0.25") ); |
| 231 |
|
| 232 |
plot.AddDataset( Gnuplot2dFunction("LogNormalDist(x, 0.0, 0.25)", |
| 233 |
"LogNormalDist(x, 0.0, 0.25)") ); |
| 234 |
|
| 235 |
plot.AddDataset( Histogramm(LogNormalVariable(0.0, 0.125), probes, precision, |
| 236 |
"LogNormalVariable m=0.0 s=0.125") ); |
| 237 |
|
| 238 |
plot.AddDataset( Histogramm(LogNormalVariable(0.0, 2.0), probes, precision, |
| 239 |
"LogNormalVariable m=0.0 s=2.0") ); |
| 240 |
|
| 241 |
plot.AddDataset( Gnuplot2dFunction("LogNormalDist(x, 0.0, 2.0)", |
| 242 |
"LogNormalDist(x, 0.0, 2.0)") ); |
| 243 |
|
| 244 |
plot.AddDataset( Histogramm(LogNormalVariable(0.0, 2.5), probes, precision, |
| 245 |
"LogNormalVariable m=0.0 s=2.5") ); |
| 246 |
|
| 247 |
gnuplots.AddPlot(plot); |
| 248 |
} |
| 249 |
|
| 250 |
{ |
| 251 |
Gnuplot plot; |
| 252 |
plot.SetTitle("TriangularVariable"); |
| 253 |
plot.AppendExtra("set xrange [*:*]"); |
| 254 |
|
| 255 |
plot.AddDataset( Histogramm(TriangularVariable(0.0, 1.0, 0.5), probes, precision, |
| 256 |
"TriangularVariable [0.0 .. 1.0) m=0.5") ); |
| 257 |
|
| 258 |
plot.AddDataset( Histogramm(TriangularVariable(0.0, 1.0, 0.4), probes, precision, |
| 259 |
"TriangularVariable [0.0 .. 1.0) m=0.4") ); |
| 260 |
|
| 261 |
plot.AddDataset( Histogramm(TriangularVariable(0.0, 1.0, 0.65), probes, precision, |
| 262 |
"TriangularVariable [0.0 .. 1.0) m=0.65") ); |
| 263 |
|
| 264 |
gnuplots.AddPlot(plot); |
| 265 |
} |
| 266 |
|
| 267 |
{ |
| 268 |
Gnuplot plot; |
| 269 |
plot.SetTitle("GammaVariable"); |
| 270 |
plot.AppendExtra("set xrange [0:10]"); |
| 271 |
plot.AppendExtra("set yrange [0:1]"); |
| 272 |
plot.AppendExtra("GammaDist(x,a,b) = x**(a-1) * 1/b**a * exp(-x/b) / gamma(a)"); |
| 273 |
|
| 274 |
plot.AppendExtra("set label 1 '{/Symbol g}(x,{/Symbol a},{/Symbol b}) = x^{/Symbol a-1} e^{-x {/Symbol b}^{-1}} ( {/Symbol b}^{/Symbol a} {/Symbol G}({/Symbol a}) )^{-1}' at 0.7, 0.9"); |
| 275 |
|
| 276 |
plot.AddDataset( Histogramm(GammaVariable(1.0, 1.0), probes, precision, |
| 277 |
"GammaVariable a=1.0 b=1.0") ); |
| 278 |
|
| 279 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 1.0, 1.0)", |
| 280 |
"GammaDist(x, 1.0, 1.0)") ); |
| 281 |
|
| 282 |
plot.AddDataset( Histogramm(GammaVariable(1.5, 1.0), probes, precision, |
| 283 |
"GammaVariable a=1.5 b=1.0") ); |
| 284 |
|
| 285 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 1.5, 1.0)", |
| 286 |
"GammaDist(x, 1.5, 1.0)") ); |
| 287 |
|
| 288 |
plot.AddDataset( Histogramm(GammaVariable(2.0, 1.0), probes, precision, |
| 289 |
"GammaVariable a=2.0 b=1.0") ); |
| 290 |
|
| 291 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.0, 1.0)", |
| 292 |
"GammaDist(x, 2.0, 1.0)") ); |
| 293 |
|
| 294 |
plot.AddDataset( Histogramm(GammaVariable(4.0, 1.0), probes, precision, |
| 295 |
"GammaVariable a=4.0 b=1.0") ); |
| 296 |
|
| 297 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 4.0, 1.0)", |
| 298 |
"GammaDist(x, 4.0, 1.0)") ); |
| 299 |
|
| 300 |
plot.AddDataset( Histogramm(GammaVariable(2.0, 2.0), probes, precision, |
| 301 |
"GammaVariable a=2.0 b=2.0") ); |
| 302 |
|
| 303 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.0, 2.0)", |
| 304 |
"GammaDist(x, 2.0, 2.0)") ); |
| 305 |
|
| 306 |
plot.AddDataset( Histogramm(GammaVariable(2.5, 3.0), probes, precision, |
| 307 |
"GammaVariable a=2.5 b=3.0") ); |
| 308 |
|
| 309 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.5, 3.0)", |
| 310 |
"GammaDist(x, 2.5, 3.0)") ); |
| 311 |
|
| 312 |
plot.AddDataset( Histogramm(GammaVariable(2.5, 4.5), probes, precision, |
| 313 |
"GammaVariable a=2.5 b=4.5") ); |
| 314 |
|
| 315 |
plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.5, 4.5)", |
| 316 |
"GammaDist(x, 2.5, 4.5)") ); |
| 317 |
|
| 318 |
gnuplots.AddPlot(plot); |
| 319 |
} |
| 320 |
|
| 321 |
{ |
| 322 |
Gnuplot plot; |
| 323 |
plot.SetTitle("ErlangVariable"); |
| 324 |
plot.AppendExtra("set xrange [0:10]"); |
| 325 |
plot.AppendExtra("ErlangDist(x,k,l) = x**(k-1) * 1/l**k * exp(-x/l) / (k-1)!"); |
| 326 |
|
| 327 |
plot.AppendExtra("set label 1 'Erlang(x,k,{/Symbol l}) = x^{k-1} e^{-x {/Symbol l}^{-1}} ( {/Symbol l}^k (k-1)! )^{-1}' at 0.7, 0.9"); |
| 328 |
|
| 329 |
plot.AddDataset( Histogramm(ErlangVariable(1, 1.0), probes, precision, |
| 330 |
"ErlangVariable k=1 {/Symbol l}=1.0") ); |
| 331 |
|
| 332 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 1, 1.0)", |
| 333 |
"ErlangDist(x, 1, 1.0)") ); |
| 334 |
|
| 335 |
plot.AddDataset( Histogramm(ErlangVariable(2, 1.0), probes, precision, |
| 336 |
"ErlangVariable k=2 {/Symbol l}=1.0") ); |
| 337 |
|
| 338 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 1.0)", |
| 339 |
"ErlangDist(x, 2, 1.0)") ); |
| 340 |
|
| 341 |
plot.AddDataset( Histogramm(ErlangVariable(3, 1.0), probes, precision, |
| 342 |
"ErlangVariable k=3 {/Symbol l}=1.0") ); |
| 343 |
|
| 344 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 3, 1.0)", |
| 345 |
"ErlangDist(x, 3, 1.0)") ); |
| 346 |
|
| 347 |
plot.AddDataset( Histogramm(ErlangVariable(5, 1.0), probes, precision, |
| 348 |
"ErlangVariable k=5 {/Symbol l}=1.0") ); |
| 349 |
|
| 350 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 5, 1.0)", |
| 351 |
"ErlangDist(x, 5, 1.0)") ); |
| 352 |
|
| 353 |
plot.AddDataset( Histogramm(ErlangVariable(2, 2.0), probes, precision, |
| 354 |
"ErlangVariable k=2 {/Symbol l}=2.0") ); |
| 355 |
|
| 356 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 2.0)", |
| 357 |
"ErlangDist(x, 2, 2.0)") ); |
| 358 |
|
| 359 |
plot.AddDataset( Histogramm(ErlangVariable(2, 3.0), probes, precision, |
| 360 |
"ErlangVariable k=2 {/Symbol l}=3.0") ); |
| 361 |
|
| 362 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 3.0)", |
| 363 |
"ErlangDist(x, 2, 3.0)") ); |
| 364 |
|
| 365 |
plot.AddDataset( Histogramm(ErlangVariable(2, 5.0), probes, precision, |
| 366 |
"ErlangVariable k=2 {/Symbol l}=5.0") ); |
| 367 |
|
| 368 |
plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 5.0)", |
| 369 |
"ErlangDist(x, 2, 5.0)") ); |
| 370 |
|
| 371 |
gnuplots.AddPlot(plot); |
| 372 |
} |
| 373 |
|
| 374 |
gnuplots.GenerateOutput(std::cout); |
| 375 |
|
| 376 |
return 0; |
| 377 |
} |