File: ResultTable.cc

package info (click to toggle)
pchar 1.5-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 644 kB
  • ctags: 402
  • sloc: cpp: 6,096; sh: 2,510; makefile: 192
file content (1083 lines) | stat: -rw-r--r-- 26,108 bytes parent folder | download | duplicates (5)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
static char rcsid[] = "$Id: ResultTable.cc 1082 2005-02-12 19:40:04Z bmah $";
//
// $Id: ResultTable.cc 1082 2005-02-12 19:40:04Z bmah $
//
// ResultTable.cc
// Bruce A. Mah <bmah@acm.org>
//
// This work was first produced by an employee of Sandia National
// Laboratories under a contract with the U.S. Department of Energy.
// Sandia National Laboratories dedicates whatever right, title or
// interest it may have in this software to the public. Although no
// license from Sandia is needed to copy and use this software,
// copying and using the software might infringe the rights of
// others. This software is provided as-is. SANDIA DISCLAIMS ANY
// WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "pc.h"
#include "ResultTable.h"
#include "Kendall.h"

//
// Constructor
//
// Input:  Table parameters (i, m, b, r)
//
// Output:  None
//
ResultTable::ResultTable(unsigned int inc, unsigned int m, unsigned int b, 
			 unsigned int r) : 
    increment(inc), mtu(m), repetitions(r), burst(b), columns((burst+1)*mtu+1)
// Note the initialization of the columns member; we want to be
// able to hold the largest possible response packet.  (burst+1)*MTU should
// work unless the user sets MTU to something small (then add one because 
// packet sizes begin with 1, not 0).
{
    
    int i;
    
    // Stupid typedef hack for SparcWorks C++ compilier, which apparently
    // can't handle "new (footype *)[bar]".  We're trying to do:
    // data = new (double *) [columns];
    typedef double *DoublePtr;
    data = new (double *[columns]);
    if (data == NULL) {
	fprintf(stderr, "Couldn't allocate data array for a ResultTable\n");
	exit(1);
    }

    used = new int[columns];
    if (used == NULL) {
	fprintf(stderr, "Couldn't allocate used array for a ResultTable\n");
	exit(1);
    }

    for (i = 0; i < columns; i++) {
	data[i] = NULL;
	used[i] = 0;
    }

    // Invalidate result caches
    cacheSlrValid = false;
    cacheTauValid = false;
    cacheLmsValid = false;
    cacheQueueingValid = false;
}

//
// ResultTable::~ResultTable
//
// Input:  None
//
// Output  None
//
ResultTable::~ResultTable() {
    int i;

    for (i = 0; i < columns; i++) {
	if (data[i]) {
	    delete [] data[i];
	    data[i] = NULL;
	}
    }
    delete data;
    delete used;
}

//
// ResultTable::put
//
// Input:  size, time pair
//
// Output:  Success code in return value (negative if an error)
//
// Insert a new result into the table.
//
int ResultTable::put(int size, double time) {
    int offset;

    // Is the offset within the proper range for the table?
    offset = size2column(size);
    if ((offset < 0) || (offset >= columns)) {
	fprintf(stderr, "Size %d out of bounds [0,%d)\n", offset, columns);
	return -1;
    }
    
    // Any room left for more results in this column?
    if (used[offset] == repetitions) {
	fprintf(stderr, 
		"Too many repetitions for this size (%d >= %d)\n", 
		used[offset], repetitions);
	return -1;
    }

    // Need to allocate more memory to hold this column?
    if (data[offset] == NULL) {
	data[offset] = new double[repetitions];
	if (data[offset] == NULL) {
	    fprintf(stderr, "Couldn't allocate memory for new column\n");
	    return -1;
	}
    }

    // Store data
    data[offset][used[offset]] = time;
    (used[offset])++;
    return 0;

    // Invalidate result caches
    cacheSlrValid = false;
    cacheTauValid = false;
    cacheQueueingValid = false;
}

//
// ResultTable::getMin
//
// Input:  none
//
// Output:  Pointer to a new ResultTable (NULL if an error)
//
// Return a new ResultTable, which contains the minimum values
// of each packet size test.
//
ResultTable *ResultTable::getMin() {

    // Get new ResultTable, but we only need room for one
    // "repetition".
    ResultTable *t2 = new ResultTable(increment, mtu, burst, 1);
    if (t2 == NULL) {
	return NULL;
    }

    // Iterate over columns (packet sizes)
    int i;
    for (i = 0; i < columns; i++) {

	// If any values, then find the minimum and store it.
	if (used[i]) {
	    int j;
	    double min = data[i][0];
	    for (j = 1; j < used[i]; j++) {
		if (data[i][j] < min) {
		    min = data[i][j];
		}
	    }

	    if (t2->put(column2size(i), min) < 0) {
		return NULL;
	    }
	}
    }
    return t2;

}

//
// ResultTable::queueing
//
// Input:  None
//
// Output:  Average queueing delay for this dataset (in return
// value).  If there are no data points in this table, the result
// is 0.0.
//
// Compute average (?) queueing delay for this dataset.
// Found by computing, for each column, the difference from the column
// minimum.
//
// XXX we might want some better statistics too, such as getting
// a confidence interval.
//
double ResultTable::queueing()
{

    // If we've cached a queueing figure, then just return it.
    if (cacheQueueingValid) {
	return cacheQueueing;
	IF_DEBUG(1, fprintf(stderr, "ResultTable::queueing(): cache hit\n"));
    }

    // Results not valid, need to compute them.
    else {
	int i, j;
	double sigmaY = 0.0;
	int n = 0;

    // Loop over columns
	for (i = 0; i < columns; i++) {

	    // Only the ones with data points
	    if (used[i] > 0) {

		double min;
		double y;

		// Find the minimum data point for this column
		min = data[i][0];
		for (j = 1; j < used[i]; j++) {
		    if (data[i][j] < min) {
			min = data[i][j];
		    }
		}

		// Now compute the difference between each data
		// point and the minimum and add it to the sum.
		//
		// NB:  There are faster ways to get this result,
		// but we do it this way so that we can get access
		// to the individual data points, for example to
		// compute some other statistics on them.
		for (j = 0; j < used[i]; j++) {
		    y = data[i][j] - min;
		    sigmaY += y;
		    n++;
		}
	    }
	}
	if (n > 0) {
	    cacheQueueing = sigmaY / n;
	}
	else {
	    cacheQueueing = 0.0;
	}
	cacheQueueingValid = true;
	return cacheQueueing;
    }
}

//
// ResultTable::slr
//
// Input:  None
//
// Output:  SLR parameters (a and b, where a is the linear constant
// and b is the X coeffecient), coefficient of determination R2,
// standard deviation of parameters sb and sb.
//
// Compute simple linear regression for all data points, based on
// a least-squares algorithm as described by
// text in Chapter 14 of "The Art of Computer Systems Performance
// Analysis", R. Jain, 1991.
//
void ResultTable::slr(double &a, double &b, double &R2, double &sa, double &sb)
{

    // If cached results valid, use them
    if (cacheSlrValid) {
	a = cacheSlrA;
	b = cacheSlrB;
	R2 = cacheSlrR2;
	sa = cacheSlrSA;
	sb = cacheSlrSB;

	IF_DEBUG(1, fprintf(stderr, "ResultTable::slr(): cache hit\n"));

	return;
    }

    // Compute results
    else {
	double sigmaX = 0.0, sigmaY = 0.0, 
	    sigmaXY = 0.0, 
	    sigmaX2 = 0.0, sigmaY2 = 0.0;
	double Xbar, Ybar;
	double b0, b1;
	double SSY, SS0, SST, SSE, SSR;
	double se;
	int n = 0;
	int i, j;
   
	// Iterate over columns
	for (i = 0; i < columns; i++) {

	    // Iterate over points within a column
	    for (j = 0; j < used[i]; j++) {

		double X = (double) column2size(i);
		double Y = data[i][j];

		sigmaX += X;
		sigmaY += Y;
		sigmaXY += (X*Y);
		sigmaX2 += (X*X);
		sigmaY2 += (Y*Y);
		n++;
	    }
	
	}

	// We need at least three datapoints.  If we don't have that
	// many, return something that, while bogus, at least makes a
	// little sense, to avoid getting divide-by-zero situations.
	if (n == 0) {
	    a = 0.0;
	    b = 0.0;
	    R2 = 0.0;
	    sa = 0.0;
	    sb = 0.0;
	    return;
	}

	Xbar = sigmaX / n;
	Ybar = sigmaY / n;

	// b1 = b, b0 = a
	b1 = (sigmaXY - (n * Xbar * Ybar)) / (sigmaX2 - (n * Xbar * Xbar));
	b0 = Ybar - b1 * Xbar;
    
	// Compute variation
	SSY = sigmaY2;
	SS0 = n * (Ybar * Ybar);
	SST = SSY - SS0;
	SSE = sigmaY2 - (b0 * sigmaY) - (b1 * sigmaXY);
	SSR = SST - SSE;

	// Compute regression parameters
	a = b0;
	b = b1;

	// Compute coefficient of determination
	R2 = SSR/SST;

	// Compute standard deviation of errors
	se = sqrt(SSE/(n-2));

	// Compute Standard deviation of parameters
	sa = se * sqrt( (1/n) + ((Xbar * Xbar) / 
				 (sigmaX2 - (n * Xbar * Xbar))));
	sb = se / sqrt( sigmaX2 - (n * Xbar * Xbar));
	

	// Cache results for later
	cacheSlrA = a;
	cacheSlrB = b;
	cacheSlrR2 = R2;
	cacheSlrSA = sa;
	cacheSlrSB = sb;
	cacheSlrValid = true;
    }
}

//
// ResultTable::tau
//
// Input:  None
//
// Output:  Linear regression parameters (a and b, where a is the
// linear constant and b is the X coeffecient), width of XXX% confidence
// interval for b.
//
// Compute linear fit based on Kendall's tau statistic, as described
// in "Practical Nonparametric Statistics", Third Edition, W. J. Conover, 
// 1999, p. 335.
//
void ResultTable::tau(double &a, double &b, double &blower, double &bupper)
{

    // Check for valid, cached results
    if (cacheTauValid) {
    }
    else {
	unsigned int maxSlopes;	// maximum number of slopes to compute
	unsigned int numSlopes;	// actual number of slopes found
	unsigned int maxValues;	// max values in the table?
	unsigned int numValues;	// how many values in the table?
	int i;			// universal loop counter
	unsigned int xcol, xitem, ycol, yitem;
	
	// Compute number of slopes we might need to work with
	maxSlopes = 0;
	maxValues = 0;
	for (i = 0; i < columns; i++) {
	    maxValues += used[i];
	}

	// If less than two values we can't compute a regression,
	// so give up.
	if (maxValues < 2) {
	    a = 0.0;
	    b = 0.0;
	    blower = 0.0;
	    bupper = 0.0;
	    return;
	}

	maxSlopes = maxValues * (maxValues - 1) / 2;
	
	double *slopes;
	slopes = new double[maxSlopes];
	if (slopes == NULL) {
	    fprintf(stderr, 
		    "Couldn't allocate slopes array for a ResultTable\n");
	    exit(1);
	}

	double *xvalues, *yvalues;
	xvalues = new double[maxValues];
	if (xvalues == NULL) {
	    fprintf(stderr, 
		    "Couldn't allocate xvalues array for a ResultTable\n");
	    exit(1);
	}

	yvalues = new double[maxValues];
	if (yvalues == NULL) {
	    fprintf(stderr, 
		    "Couldn't allocate yvalues array for a ResultTable\n");
	    exit(1);
	}

	// Compute all the slopes.  Basically, we try to treat the
	// maxSlopes datapoints as being in a single, 1-D array,
	// rather than being in a set of 1-D arrays of variable
	// sizes.  We refer to the two values being "pointed to"
	// as x and y.
	numSlopes = 0;
	numValues = 0;
	xcol = 0;
	xitem = 0;

	// Iterate through the items to find X values
	while (xcol < columns) {
	    while (xitem < used[xcol]) {

		// Record this X and Y value
		xvalues[numValues] = (double)column2size(xcol);
		yvalues[numValues] = data[xcol][xitem];
		numValues++;

		// Start looking for Y values, given a single X 
		// value.  Start with the "next" item in sequence
		// after the one we chose for X.  Note that after
		// the next two lines, ycol/yitem might point out
		// of bounds.  That's OK, because we check them
		// immediately afterwards (incrementing if necessary).
		ycol = xcol;
		yitem = xitem + 1;

		while (ycol < columns) {
		    while (yitem < used[ycol]) {

			double xx, xy, yx, yy;
			xx = column2size(xcol);
			xy = data[xcol][xitem];
			yx = column2size(ycol);
			yy = data[ycol][yitem];

			// Try to avoid divide-by-zero errors
			if (yx != xx) {
			    double slope = (yy-xy) / (yx-xx);
			    slopes[numSlopes++] = slope;
			}
			else {
			    fprintf(stderr, "Warning:  Duplicate x values (%f,%f) = (%f,%f)\n", xx, xy, yx, yy);
			}

			yitem++;
		    }
		    ycol++;
		    yitem = 0;
		}

		xitem++;
	    }
	    xcol++;
	    xitem = 0;
	}

	// If we had to throw away points because of duplicate X
	// values, this could throw our confidence intervals off.
	if (numSlopes != maxSlopes) {
	    fprintf(stderr, "Warning: duplicate X values forced discarding of data points\n");
	}

	// Compute slope
	b = median(slopes, numSlopes);

	// Compute intercept
	double xmedian, ymedian;
	xmedian = median(xvalues, numValues);
	ymedian = median(yvalues, numValues);
	a = ymedian - b * xmedian;

	// Compute confidence interval on slope
	unsigned int T, r, s;
	T = Kendall::T(numValues, KendallP950);	// 90% confidence for now
	r = (numSlopes - T) / 2 - 1;
	s = ((numSlopes + T + 1)) / 2;
	
	bupper = slopes[r];
	blower = slopes[s];

	delete [] slopes;
	delete [] xvalues;
	delete [] yvalues;

    }

}

//
// ResultTable::lms
//
// Input:  None
//
// Output:  Linear regression parameters (a and b, where a is the
// linear constant and b is the X coeffecient), coeffecient of
// determination R2.
//
// Compute linear fit based on a Least Median of Squares fit, as
// described in Peter J. Rousseeuw and Annick M. Leroy's 
// "Robust Regression and Outlier Detection", John Wiley & Sons, Inc.,
// New York, NY, 1987.
//
void ResultTable::lms(double &a, double &b, double &r2)
{

    // Check for valid, cached results
    if (cacheLmsValid) {
    }
    else {
	unsigned int maxSlopes;	// maximum number of slopes to compute
	unsigned int numSlopes;	// actual number of slopes found
	unsigned int maxValues;	// max values in the table?
	int i;			// universal loop counter
	unsigned int xcol, xitem, ycol, yitem, zcol, zitem;
	bool estimatorFound;	// flag to see if we've actually computed
				// a residuals quantity yet
	double minLMS, minLMSa, minLMSb; // LMS estimator and associated regression parameters
	
	// Compute number of slopes we might need to work with
	maxSlopes = 0;
	maxValues = 0;
	for (i = 0; i < columns; i++) {
	    maxValues += used[i];
	}

	// If less than two values we can't compute a regression,
	// so give up.
	if (maxValues < 2) {
	    a = 0.0;
	    b = 0.0;
	    r2 = 0.0;
	    return;
	}

	maxSlopes = maxValues * (maxValues - 1) / 2;
	
	double *residuals;
	double *ys;
	residuals = new double[maxValues];
	if (residuals == NULL) {
	    fprintf(stderr, 
		    "Couldn't allocate residuals array for a ResultTable\n");
	    exit(1);
	}

	ys = new double[maxValues];
	if (ys == NULL) {
	    fprintf(stderr, 
		    "Couldn't allocate ys array for a ResultTable\n");
	    exit(1);
	}

        estimatorFound = false;

	// Find all pairs of points, and use them to find a trial
	// set of regression parameters.  We then compute the LMS
	// estimator given these regression parameters, and save
	// the parameters that give us the minimum value of the
	// estimator.
	// 
	// Implementation note:  As with ResultTable::tau (from which 
	// this code is derived), we try to treat the
	// maxSlopes datapoints as being in a single, 1-D array,
	// rather than being in a set of 1-D arrays of variable
	// sizes.  We refer to the two values being "pointed to"
	// as x and y.
	numSlopes = 0;
	xcol = 0;
	xitem = 0;

	// Iterate through the items to find X values
	while (xcol < columns) {
	    while (xitem < used[xcol]) {

		// Start looking for Y values, given a single X 
		// value.  Start with the "next" item in sequence
		// after the one we chose for X.  Note that after
		// the next two lines, ycol/yitem might point out
		// of bounds.  That's OK, because we check them
		// immediately afterwards (incrementing if necessary).
		ycol = xcol;
		yitem = xitem + 1;

		while (ycol < columns) {
		    while (yitem < used[ycol]) {

			double xx, xy, yx, yy;
			xx = column2size(xcol);
			xy = data[xcol][xitem];
			yx = column2size(ycol);
			yy = data[ycol][yitem];

			// Try to avoid divide-by-zero errors
			if (yx != xx) {
			    double slope = (yy-xy) / (yx-xx);
			    double intercept = xy - (slope * xx);
			    unsigned int numResiduals = 0;
			    double estimator;

			    // Compute residuals (well, actually
			    // we're computing the squares of the residuals)
			    zcol = 0;
			    zitem = 0;
			    while (zcol < columns) {
				while (zitem < used[zcol]) {
				    residuals[numResiduals] = 
    pow(data[zcol][zitem] - (column2size(zcol) * slope + intercept), 2);
				    numResiduals++;
				    zitem++;
				}
				zcol++;
				zitem = 0;
			    }

			    // Compute estimator.  If it's less than our
			    // minimum, then save the current regression
			    // parameters.
			    estimator = median(residuals, numResiduals);
			    
			    if ((!estimatorFound) || 
				(estimator < minLMS)) {

				minLMS = estimator;
				minLMSa = intercept;
				minLMSb = slope;
				estimatorFound = true;
				
			    }
			    numSlopes++;

			}
			else {
			    fprintf(stderr, "Warning:  Duplicate x values (%f,%f) = (%f,%f)\n", xx, xy, yx, yy);
			}

			yitem++;
		    }
		    ycol++;
		    yitem = 0;
		}

		xitem++;
	    }
	    xcol++;
	    xitem = 0;
	}

	// If we had to throw away points because of duplicate X
	// values, note this.  It shouldn't really affect results much.
	if (numSlopes != maxSlopes) {
	    fprintf(stderr, "Warning: duplicate X values forced discarding of data points\n");
	}

	if (estimatorFound) {
	    a = minLMSa;
	    b = minLMSb;

	    // Coefficient of Determination computation
	    unsigned int numResiduals;
	    unsigned int numYs;
	    double medianRabs;	// median of all absolute residuals
	    double medianY;	// median of all Y values
	    double madY;	// median absolute deviation

	    // We need to make two passes over the data.  The first pass
	    // gathers the absolute values of the residuals, as well as
	    // all of the data values.  The former will go to compute
	    // med|r sub i|, while the latter gives us med(y sub i).
	    xcol = 0;
	    xitem = 0;
	    numResiduals = 0;
	    numYs = 0;

	    while (xcol < columns) {
		while (xitem < used[xcol]) {

		    residuals[numResiduals] = 
			fabs(data[xcol][xitem] -
			     (column2size(xcol) * minLMSb + minLMSa));
		    numResiduals++;

		    ys[numYs] = data[xcol][xitem];
		    numYs++;

		    xitem++;
		}

		xcol++;
		xitem = 0;
	    }

	    medianRabs = median(residuals, numResiduals);
	    medianY = median(ys, numYs);

	    // In the second pass over the data, we use the median Y
	    // value we computed earlier to determine 
	    // med|y sub i - med(y sub j)|.
	    xcol = 0;
	    xitem = 0;
	    numYs = 0;

	    while (xcol < columns) {
		while (xitem < used[xcol]) {

		    ys[numYs] = 
			fabs(data[xcol][xitem] - medianY);
		    numYs++;
		    
		    xitem++;
		}
	       
		xcol++;
		xitem = 0;
	    }

	    madY = median(ys, numYs);
	    r2 = 1.0 - pow((medianRabs / madY), 2);

	}
	else {
	    fprintf(stderr, "Warning: residual computation failed\n");
	    a = 0.0;
	    b = 0.0;
	    r2 = 0.0;
	}

	delete [] residuals;
	delete [] ys;

    }

}

//
// ResultTable::lmsint
//
// Input:  None
//
// Output:  Linear regression parameters (a and b, where a is the
// linear constant and b is the X coeffecient), coeffecient of
// determination R2.
//
// Compute linear fit based on a Least Median of Squares fit.
// The algorithm used is the same as ResultTable::lms, except that
// we do all computations using only int32 variables.  This is a
// check of an IOS implementation of this algorithm.
//
void ResultTable::lmsint(double &a, double &b, double &r2)
{
    unsigned int *partialmins;	// We assume we've got minfiltered points
    unsigned int *residuals;	// Residuals
    unsigned int *ys;		// Copy of y values
    int i, j, k, l;		// loop counters
    int currentslope;
    int currentintercept;
    unsigned int r2int;		// coefficient of determination

    const unsigned int timeoutresult = 0;
    const unsigned int slopescale = 1000; // scaling factor for slope computations 
    const unsigned int codscale = 1000;	// sqrt of scaling factor for coefficient of determination

    ys = new unsigned int[columns];
    if (ys == NULL) {
	fprintf(stderr, 
		"Couldn't allocate ys array for a ResultTable\n");
	exit(1);
    }

    partialmins = new unsigned int[columns];
    if (partialmins == NULL) {
	fprintf(stderr, 
		"Couldn't allocate partialmins array for a ResultTable\n");
	exit(1);
    }

    for (i = 0; i < columns; i++) {
	// Convert dataset to integers representing microseconds.
	partialmins[i] = (unsigned int) (data[i][0] * 1000000.0);
    }
    residuals = new unsigned int[columns*columns];
    if (residuals == NULL) {
	fprintf(stderr, 
		"Couldn't allocate residuals array for a ResultTable\n");
	exit(1);
    }
    
    // Following code comes from the IOS version of pchar, hence
    // the C-style comments.

    /*
     * Linear regression happens on the minfiltered datapoints.
     */
    {
	/*
	 * Use the least median of squares regression.  Slopes are in
	 * microseconds per byte but this may change.
	 * 
	 * We need to do something here for the case that we didn't
	 * get any data points at all for one or more packet sizes.
	 */
	unsigned long testslope, testintercept;
	unsigned long estimator;
	unsigned long minestimator;
	bool estimatorvalid;

	minestimator = 0;
	estimatorvalid = false;
	testslope = 0;
	testintercept = 0;
	
	for (i = 0; i < columns; i++) {
	    for (j = i+1; j < columns; j++) {
		
		if ((partialmins[i] != timeoutresult) &&
		    (partialmins[j] != timeoutresult)) {
		    
		    /* Compute test slope and estimator */
		    testslope = (((partialmins[j] - partialmins[i])) * 
				 slopescale) /
			(column2size(j - i));
		    testintercept = partialmins[j] -
			((partialmins[j] - partialmins[i]) * 
			 (column2size(j)) / 
			 (column2size(j - i)));
		    
		    /* Compute squares of residuals */
		    for (k = 0, l = 0; k < columns; k++) {
			if (partialmins[k] != timeoutresult) {
			    residuals[l] = partialmins[k] - 
				((testslope * 
				  column2size(k) /
				  slopescale) + 
				 testintercept);
			    residuals[l] *= residuals[l];
			    l++;
			}
		    }
		    
		    if (l > 0) {
			
			/* Estimator is median of squared residuals */
			estimator = median(residuals, l);
			
			if ((estimator < minestimator) || (!estimatorvalid)) {
			    minestimator = estimator;
			    currentslope = testslope;
			    currentintercept = testintercept;
			    estimatorvalid = true;
			}
		    }
		}
	    }
	}
    }

    /* 
     * Coeffecient of determination calculation...how good was
     * the fit?
     */
    r2int = 0;
    if ((currentslope != 0) || (currentintercept != 0)) {
	
	unsigned int medianr;	/* median of all absolute residuals */
	unsigned int mediany;	/* median of all Y values */
	unsigned int mady;	/* median absolute deviation */
	
	/*
	 * Make two passes over the data.  The first pass gather
	 * the absolute values of the residuals, as well as all
	 * of the dependent variable values.  The former goes to
	 * compute med|r|, while the latter gives med(y).
	 */
	l = 0;
	for (i = 0; i < columns; i++) {
	    if (partialmins[i] != timeoutresult) {
		residuals[l] = abs(partialmins[i] - 
				   ((currentslope * 
				     column2size(i) /
				     slopescale) +
				    currentintercept));
		ys[l] = partialmins[i];
		
		l++;
	    }
	}
	medianr = median(residuals, l);
	mediany = median(ys, l);
	
	/*
	 * In the second pass over the data, we use the median Y
	 * value computed by the first pass to determine
	 * med|y sub i - med(y)|
	 */
	l = 0;
	for (i = 0; i < columns; i++) {
	    if (partialmins[i] != timeoutresult) {
		ys[l] = abs(partialmins[i] - mediany);
		l++;
	    }
	}
	mady = median(ys, l);
	
	/* r2 = 1.0 - pow((medianr / mady), 2); */
	r2int = (codscale * codscale) -
	    ((codscale * codscale * medianr * medianr) /
	     (mady * mady));
	
    }
    
    a = ((double) currentintercept) / 1000000.0;
    b = ((double) currentslope / 1000000.0 / (double) slopescale);
    r2 = ((double) r2int) / ((double) codscale * (double) codscale);
    delete [] partialmins;
    delete [] residuals;
    delete [] ys;

}

//
// ResultTable::median
//
// Input:
//
// Output: Median value
//
// Compute the median of an array of doubles.  
// As a side effect, the input array is sorted
// 
double ResultTable::median(double *values, unsigned int numValues)
{
    double medianValue;

    // Sort the using qsort(3).
    extern int doublecomp(const void *a, const void *b);
    qsort((void *) values, numValues, sizeof(double), doublecomp);

    // Find median value.
    if (numValues & 1) {
	// Odd number of samples
	medianValue = values[(numValues-1)/2];
    }
    else {
	// Even number of samples
	medianValue = (values[(numValues/2)] + values[(numValues/2)-1]) /
	    2.0;
    }
    return medianValue;
}

// Function for qsort(3) to determine the relative ordering of two
// doubles.  Used in the call to qsort above.
int doublecomp(const void *a, const void *b) 
{
    double adouble = *(const double *) a;
    double bdouble = *(const double *) b;
    if (adouble == bdouble) {
	return 0;
    }
    else {
	if (adouble < bdouble) {
	    return -1;
	}
	else {
	    return 1;
	}
    }
}


//
// ResultTable::median
//
// Input:
//
// Output: Median value
//
// Compute the median of an array of unsigned ints.
// As a side effect, the input array is sorted
// 
unsigned int ResultTable::median(unsigned int *values, unsigned int numValues)
{
    unsigned int medianValue;

    // Sort the using qsort(3).
    extern int uintcomp(const void *a, const void *b);
    qsort((void *) values, numValues, sizeof(unsigned int), uintcomp);

    // Find median value.
    if (numValues & 1) {
	// Odd number of samples
	medianValue = values[(numValues-1)/2];
    }
    else {
	// Even number of samples
	medianValue = (values[(numValues/2)] + values[(numValues/2)-1]) /
	    2;
    }
    return medianValue;
}

// Function for qsort(3) to determine the relative ordering of two
// doubles.  Used in the call to qsort above.
int uintcomp(const void *a, const void *b) 
{
    unsigned int auint = *(const unsigned int *) a;
    unsigned int buint = *(const unsigned int *) b;
    if (auint == buint) {
	return 0;
    }
    else {
	if (auint < buint) {
	    return -1;
	}
	else {
	    return 1;
	}
    }
}


//
// ResultTable::Print
//
// Input:  file pointer to print to, tag string, hop number
//
// Output:  Success code
//
// Print the contents of the table to the file pointer fp.
//
int ResultTable::Print(FILE *fp, char *tag, int hop)
{

    int i, j;

    for (i = 0; i < columns; i++) {
	for (j = 0; j < used[i]; j++) {

	    fprintf(fp, "%s %d %d %f\n", tag, hop, column2size(i), 
		    data[i][j]);

	}
    }
    return 0;
}