This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatistic.h
More file actions
277 lines (209 loc) · 6.91 KB
/
Statistic.h
File metadata and controls
277 lines (209 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/****************************************************************************
* *
* File: Statistic.h *
* *
* Author: Branch Vincent *
* *
* Date: Aug 10, 2016 *
* *
* Purpose: This file defines the Statistic class. *
* *
****************************************************************************/
#ifndef STATISTIC_H
#define STATISTIC_H
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
// Type definitions
template <typename T>
using Matrix2D = vector<vector<T> >;
template <typename T>
using Matrix3D = vector<Matrix2D<T> >;
/****************************************************************************
* *
* Definition of Statistic class *
* *
****************************************************************************/
// Notes
// - Assert for positive dimensions
class Statistic
{
// Public member functions
public:
// Constructor
Statistic(string nm, int xDim, int yDim, int zDim) :
name(nm),
data(xDim + 1, Matrix2D<float>(yDim + 1, vector<float>(zDim, 0))),
avgs(xDim + 1, vector<float>(yDim + 1, 0)),
devs(xDim + 1, vector<float>(yDim + 1, 0)) {}
// Inspectors
string getName() {return name;}
float getData(int i, int j, int k) {return data[i][j][k];}
float getData(int j, int k)
{
int lastRow = (int)data.size() - 1;
return data[lastRow][j][k];
}
float getTaskAvg(int j, int k)
{
int lastRow = (int)data.size() - 1;
return data[lastRow][j][k];
}
float getTaskStdDev(int j, int k)
{
int lastRow = (int)data.size() - 1;
return data[lastRow][j][k];
}
// Mutators
void incData(int i, int j, int k, float val);
void avgData();
// Other member functions
// void output(ostream& out) const {outputSim(out); return;}
void outputRep(ostream& out, int rep) const;
void outputSim(ostream& out) const;
// Private member functions
// private:
// Data members
private:
string name; // stat name
Matrix3D<float> data; // stat data
Matrix2D<float> avgs; // sim averages
Matrix2D<float> devs; // sim std devs
};
// Operators
//ostream& operator<<(ostream& out, const Statistic& stat) {stat.output(out); return out;}
/****************************************************************************
* *
* Function: incData *
* *
* Purpose: To increment the data based on the specified indices and *
* value *
* *
****************************************************************************/
void Statistic::incData(int i, int j, int k, float val)
{
// Calculate indices for the last row/column
int rowSum = (int)data.size() - 1;
int colSum = (int)data[i].size() - 1;
// Increment replication sums
data[i][j][k] += val;
data[rowSum][j][k] += val; // col sum
data[i][colSum][k] += val; // row sum
data[rowSum][colSum][k] += val; // row + col sum
// Increment simulation sums
// avg[i][j] += val;
// avg[rowSum][j] += val; // avg row sum
// avg[i][colSum] += val; // avg col sum
// avg[rowSum][colSum] += val; // avg row + col sum
return;
}
/****************************************************************************
* *
* Function: outputRep *
* *
* Purpose: To output the data for the specified replication *
* *
****************************************************************************/
void Statistic::outputRep(ostream& out, int rep) const
{
// Output name
out << name;
// Output data
for (int i = 0; i < data.size(); i++)
{
// Output task type
if (i != data.size() - 1)
out << ", " << i;
else
out << ", Sum";
// Output data
for (int j = 0; j < data[i].size(); j++)
out << ", " << data[i][j][rep];
out << endl;
}
return;
}
/****************************************************************************
* *
* Function: outputSim *
* *
* Purpose: To output the data averaged across all replications *
* *
****************************************************************************/
void Statistic::outputSim(ostream& out) const
{
// Output stat name
out << name;
// Output data
for (int i = 0; i < avgs.size(); i++)
{
// Output task type
if (i != data.size() - 1)
out << ", " << i;
else
out << ",Sum";
// Output averages
for (int j = 0; j < avgs[i].size() - 1; j++)
out << ", " << avgs[i][j];
out << endl << ", ";
// Output std devs
for (int j = 0; j < devs[i].size() - 1; j++)
out << ", " << devs[i][j];
out << endl;
}
//
// if (name == "Wait Time")
// {
// int i = 8;
// int k = 2;
//// cout << "Col size = " << data[i].size() << endl;
//// cout << "Rep size = " << data[i][0].size() << endl;
// for (int j = 0; j < data[i].size(); j++)
//// for (int k = 0; k < data[i][j].size(); k++)
// cout << data[i][j][k] << ",";
// cout << endl << endl;
// }
return;
}
/****************************************************************************
* *
* Function: avgData *
* *
* Purpose: To calculate and store the mean and standard deviation for *
* the specified array across all replications *
* *
****************************************************************************/
void Statistic::avgData()
{
// Initialize variables
int N = 0;
float mean = 0;
float devSum = 0;
float delta;
// Calculate mean and std dev across all replications
for (int i = 0; i < data.size(); i++)
{
for (int j = 0; j < data[i].size(); j++)
{
// Reset variables
N = 0;
mean = 0;
devSum = 0;
// Calculate values using Welford's algorithm
for (int k = 0; k < data[i][j].size(); k++)
{
N++;
delta = data[i][j][k] - mean;
mean += delta/N;
devSum += delta * (data[i][j][k] - mean);
}
// Store values
avgs[i][j] = mean;
devs[i][j] = sqrt(devSum / (N - 1));
}
}
return;
}
#endif