20 #include "ns3/random-variable-stream.h"
22 #include "ns3/double.h"
23 #include "ns3/string.h"
24 #include "ns3/integer.h"
25 #include "ns3/gnuplot.h"
48 double dround (
double number,
double precision)
52 number = std::floor (number + 0.5);
54 number = std::ceil (number - 0.5);
70 unsigned int probes,
double precision,
71 const std::string& title,
bool impulses =
false)
73 typedef std::map<double, unsigned int> histogram_maptype;
74 histogram_maptype histogram;
76 for(
unsigned int i = 0; i < probes; ++i)
91 for(histogram_maptype::const_iterator hi = histogram.begin ();
92 hi != histogram.end (); ++hi)
94 data.
Add (hi->first, (
double)hi->second / (
double)probes / precision);
103 int main (
int argc,
char *argv[])
105 unsigned int probes = 1000000;
106 double precision = 0.01;
109 gnuplots.SetTerminal (
"pdf enhanced");
113 plot.
SetTitle (
"UniformRandomVariable");
121 "UniformRandomVariable [0.0 .. 1.0)") );
123 "0 <= x && x <= 1 ? 1.0 : 0") );
125 gnuplots.AddPlot (plot);
130 plot.
SetTitle (
"ExponentialRandomVariable");
132 plot.
AppendExtra (
"ExpDist(x,l) = 1/l * exp(-1/l * x)");
138 "ExponentialRandomVariable m=0.5") );
141 "ExpDist(x, 0.5)") );
147 "ExponentialRandomVariable m=1") );
150 "ExpDist(x, 1.0)") );
156 "ExponentialRandomVariable m=1.5") );
159 "ExpDist(x, 1.5)") );
161 gnuplots.AddPlot (plot);
166 plot.
SetTitle (
"ParetoRandomVariable");
174 "ParetoRandomVariable scale=1.0 shape=1.5") );
181 "ParetoRandomVariable scale=1.0 shape=2.0") );
188 "ParetoRandomVariable scale=1.0 shape=2.5") );
190 gnuplots.AddPlot (plot);
195 plot.
SetTitle (
"WeibullRandomVariable");
203 "WeibullRandomVariable scale=1.0 shape=1.0") );
210 "WeibullRandomVariable scale=1.0 shape=2.0") );
217 "WeibullRandomVariable scale=1.0 shape=3.0") );
219 gnuplots.AddPlot (plot);
224 plot.
SetTitle (
"NormalRandomVariable");
226 plot.
AppendExtra (
"NormalDist(x,m,s) = 1 / (s * sqrt(2*pi)) * exp(-1.0 / 2.0 * ((x-m) / s)**2)");
233 "NormalRandomVariable m=0.0 v=1.0") );
236 "NormalDist(x,0.0,1.0)") );
243 "NormalRandomVariable m=0.0 v=2.0") );
246 "NormalDist(x,0.0,sqrt(2.0))") );
253 "NormalRandomVariable m=0.0 v=3.0") );
256 "NormalDist(x,0.0,sqrt(3.0))") );
258 gnuplots.AddPlot (plot);
263 plot.
SetTitle (
"EmpiricalRandomVariable");
267 emp1->
CDF (0.0, 0.0 / 15.0);
268 emp1->
CDF (0.2, 1.0 / 15.0);
269 emp1->
CDF (0.4, 3.0 / 15.0);
270 emp1->
CDF (0.6, 6.0 / 15.0);
271 emp1->
CDF (0.8, 10.0 / 15.0);
272 emp1->
CDF (1.0, 15.0 / 15.0);
275 "EmpiricalRandomVariable (Stairs)") );
277 gnuplots.AddPlot (plot);
282 plot.
SetTitle (
"DeterministicRandomVariable");
285 double values[] = { 0.0, 0.2, 0.2, 0.4, 0.2, 0.6, 0.8, 0.8, 1.0 };
288 det1->
SetValueArray (values,
sizeof(values) /
sizeof(values[0]));
291 "DeterministicRandomVariable",
true) );
293 gnuplots.AddPlot (plot);
298 plot.
SetTitle (
"LogNormalRandomVariable");
301 plot.
AppendExtra (
"LogNormalDist(x,m,s) = 1.0/x * NormalDist(log(x), m, s)");
308 "LogNormalRandomVariable m=0.0 s=1.0") );
311 "LogNormalDist(x, 0.0, 1.0)") );
318 "LogNormalRandomVariable m=0.0 s=0.5") );
325 "LogNormalRandomVariable m=0.0 s=0.25") );
328 "LogNormalDist(x, 0.0, 0.25)") );
335 "LogNormalRandomVariable m=0.0 s=0.125") );
342 "LogNormalRandomVariable m=0.0 s=2.0") );
345 "LogNormalDist(x, 0.0, 2.0)") );
352 "LogNormalRandomVariable m=0.0 s=2.5") );
354 gnuplots.AddPlot (plot);
359 plot.
SetTitle (
"TriangularRandomVariable");
368 "TriangularRandomVariable [0.0 .. 1.0) m=0.5") );
376 "TriangularRandomVariable [0.0 .. 1.0) m=0.4") );
384 "TriangularRandomVariable [0.0 .. 1.0) m=0.65") );
386 gnuplots.AddPlot (plot);
391 plot.
SetTitle (
"GammaRandomVariable");
394 plot.
AppendExtra (
"GammaDist(x,a,b) = x**(a-1) * 1/b**a * exp(-x/b) / gamma(a)");
396 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");
403 "GammaRandomVariable a=1.0 b=1.0") );
406 "GammaDist(x, 1.0, 1.0)") );
413 "GammaRandomVariable a=1.5 b=1.0") );
416 "GammaDist(x, 1.5, 1.0)") );
423 "GammaRandomVariable a=2.0 b=1.0") );
426 "GammaDist(x, 2.0, 1.0)") );
433 "GammaRandomVariable a=4.0 b=1.0") );
436 "GammaDist(x, 4.0, 1.0)") );
443 "GammaRandomVariable a=2.0 b=2.0") );
446 "GammaDist(x, 2.0, 2.0)") );
453 "GammaRandomVariable a=2.5 b=3.0") );
456 "GammaDist(x, 2.5, 3.0)") );
463 "GammaRandomVariable a=2.5 b=4.5") );
466 "GammaDist(x, 2.5, 4.5)") );
468 gnuplots.AddPlot (plot);
473 plot.
SetTitle (
"ErlangRandomVariable");
475 plot.
AppendExtra (
"ErlangDist(x,k,l) = x**(k-1) * 1/l**k * exp(-x/l) / (k-1)!");
477 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");
484 "ErlangRandomVariable k=1 {/Symbol l}=1.0") );
487 "ErlangDist(x, 1, 1.0)") );
494 "ErlangRandomVariable k=2 {/Symbol l}=1.0") );
497 "ErlangDist(x, 2, 1.0)") );
504 "ErlangRandomVariable k=3 {/Symbol l}=1.0") );
507 "ErlangDist(x, 3, 1.0)") );
514 "ErlangRandomVariable k=5 {/Symbol l}=1.0") );
517 "ErlangDist(x, 5, 1.0)") );
524 "ErlangRandomVariable k=2 {/Symbol l}=2.0") );
527 "ErlangDist(x, 2, 2.0)") );
534 "ErlangRandomVariable k=2 {/Symbol l}=3.0") );
537 "ErlangDist(x, 2, 3.0)") );
544 "ErlangRandomVariable k=2 {/Symbol l}=5.0") );
547 "ErlangDist(x, 2, 5.0)") );
549 gnuplots.AddPlot (plot);
552 gnuplots.GenerateOutput (std::cout);
void AppendExtra(const std::string &extra)
Class to represent a 2D points plot.
void SetTitle(const std::string &title)
Change line title.
Abstract class to store a plot line to be used by ns3::Gnuplot.
Hold a signed integer type.
void AddDataset(const GnuplotDataset &dataset)
static double dround(double number, double precision)
Round a double number to the given precision.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
a simple class to group together multiple gnuplots into one file, e.g.
void SetTitle(const std::string &title)
void CDF(double v, double c)
Specifies a point in the empirical distribution.
void Add(double x, double y)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetValueArray(double *values, uint64_t length)
Sets the array of values that holds the predetermined sequence.
void SetStyle(enum Style style)
Class to represent a 2D function expression plot.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Class used to store data and make an histogram of the data frequency.