1 | #region License Information
|
---|
2 | /* SimSharp - A .NET port of SimPy, discrete event simulation framework
|
---|
3 | Copyright (C) 2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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 as published by
|
---|
7 | the Free Software Foundation, either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.*/
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | using System;
|
---|
20 |
|
---|
21 | namespace SimSharp {
|
---|
22 | public sealed class ContinuousStatistics {
|
---|
23 | private readonly Environment env;
|
---|
24 |
|
---|
25 | public int Count { get; private set; }
|
---|
26 | public double TotalTimeD { get; private set; }
|
---|
27 | public TimeSpan TotalTime { get { return env.ToTimeSpan(TotalTimeD); } }
|
---|
28 |
|
---|
29 | public double Min { get; private set; }
|
---|
30 | public double Max { get; private set; }
|
---|
31 | public double Area { get; private set; }
|
---|
32 | public double Mean { get; private set; }
|
---|
33 | public double StdDev { get { return Math.Sqrt(Variance); } }
|
---|
34 | public double Variance { get { return (TotalTimeD > 0) ? variance / TotalTimeD : 0.0; } }
|
---|
35 |
|
---|
36 | private double lastUpdateTime;
|
---|
37 | private double lastValue;
|
---|
38 | private double variance;
|
---|
39 |
|
---|
40 | private bool firstSample;
|
---|
41 |
|
---|
42 |
|
---|
43 | public ContinuousStatistics(Environment env) {
|
---|
44 | this.env = env;
|
---|
45 | lastUpdateTime = env.NowD;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void Update(double value) {
|
---|
49 | Count++;
|
---|
50 |
|
---|
51 | if (!firstSample) {
|
---|
52 | Min = Max = Mean = value;
|
---|
53 | firstSample = true;
|
---|
54 | } else {
|
---|
55 | if (value < Min) Min = value;
|
---|
56 | if (value > Max) Max = value;
|
---|
57 |
|
---|
58 | var duration = env.NowD - lastUpdateTime;
|
---|
59 | if (duration > 0) {
|
---|
60 | Area += (lastValue * duration);
|
---|
61 | var oldMean = Mean;
|
---|
62 | Mean = oldMean + (lastValue - oldMean) * duration / (duration + TotalTimeD);
|
---|
63 | variance = variance + (lastValue - oldMean) * (lastValue - Mean) * duration;
|
---|
64 | TotalTimeD += duration;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | lastUpdateTime = env.NowD;
|
---|
69 | lastValue = value;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|