@@ -134,9 +134,9 @@ if(doubleInput!(T) && doubleInput!(U)) {
134134}
135135
136136unittest {
137- assert (approxEqual (pearsonCor([1 ,2 ,3 ,4 ,5 ][], [1 ,2 ,3 ,4 ,5 ][]).cor, 1 ));
138- assert (approxEqual (pearsonCor([1 ,2 ,3 ,4 ,5 ][], [10.0 , 8.0 , 6.0 , 4.0 , 2.0 ][]).cor, - 1 ));
139- assert (approxEqual (pearsonCor([2 , 4 , 1 , 6 , 19 ][], [4 , 5 , 1 , 3 , 2 ][]).cor, - .2382314 ));
137+ assert (isClose (pearsonCor([1 ,2 ,3 ,4 ,5 ][], [1 ,2 ,3 ,4 ,5 ][]).cor, 1 , 1e-2 , 1e-2 ));
138+ assert (isClose (pearsonCor([1 ,2 ,3 ,4 ,5 ][], [10.0 , 8.0 , 6.0 , 4.0 , 2.0 ][]).cor, - 1 , 1e-2 , 1e-2 ));
139+ assert (isClose (pearsonCor([2 , 4 , 1 , 6 , 19 ][], [4 , 5 , 1 , 3 , 2 ][]).cor, - .2382314 , 1e-2 , 1e-2 ));
140140
141141 // Make sure everything works with lowest common denominator range type.
142142 static struct Count {
@@ -156,7 +156,7 @@ unittest {
156156 Count a, b;
157157 a.upTo = 100 ;
158158 b.upTo = 100 ;
159- assert (approxEqual (pearsonCor(a, b).cor, 1 ));
159+ assert (isClose (pearsonCor(a, b).cor, 1 , 1e-2 , 1e-2 ));
160160
161161 PearsonCor cor1 = pearsonCor([1 ,2 ,4 ][], [2 ,3 ,5 ][]);
162162 PearsonCor cor2 = pearsonCor([4 ,2 ,9 ][], [2 ,8 ,7 ][]);
@@ -165,11 +165,11 @@ unittest {
165165 cor1.put(cor2);
166166
167167 foreach (ti, elem; cor1.tupleof) {
168- assert (approxEqual (elem, combined.tupleof[ti]));
168+ assert (isClose (elem, combined.tupleof[ti], 1e-2 , 1e-2 ));
169169 }
170170
171- assert (approxEqual (pearsonCor([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ][],
172- [8 ,6 ,7 ,5 ,3 ,0 ,9 ,3 ,6 ,2 ][]).cor, - 0.4190758 ));
171+ assert (isClose (pearsonCor([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ][],
172+ [8 ,6 ,7 ,5 ,3 ,0 ,9 ,3 ,6 ,2 ][]).cor, - 0.4190758 , 1e-2 , 1e-2 ));
173173
174174 foreach (iter; 0 .. 1000 ) {
175175 // Make sure results for the ILP-optimized and non-optimized versions
@@ -183,7 +183,7 @@ unittest {
183183 }
184184
185185 foreach (ti, elem; res1.tupleof) {
186- assert (approxEqual (elem, res2.tupleof[ti]));
186+ assert (isClose (elem, res2.tupleof[ti], 1e-2 , 1e-2 ));
187187 }
188188
189189 PearsonCor resCornerCase; // Test where one N is zero.
@@ -313,7 +313,7 @@ if(doubleInput!(T) && doubleInput!(U)) {
313313}
314314
315315unittest {
316- assert (approxEqual (covariance([1 ,4 ,2 ,6 ,3 ].dup , [3 ,1 ,2 ,6 ,2 ].dup ), 2.05 ));
316+ assert (isClose (covariance([1 ,4 ,2 ,6 ,3 ].dup , [3 ,1 ,2 ,6 ,2 ].dup ), 2.05 , 1e-2 , 1e-2 ));
317317}
318318
319319/* *Spearman's rank correlation. Non-parametric. This is essentially the
@@ -358,18 +358,18 @@ is(typeof(input2.front < input2.front) == bool)) {
358358
359359unittest {
360360 // Test against a few known values.
361- assert (approxEqual (spearmanCor([1 ,2 ,3 ,4 ,5 ,6 ].dup , [3 ,1 ,2 ,5 ,4 ,6 ].dup ), 0.77143 ));
362- assert (approxEqual (spearmanCor([3 ,1 ,2 ,5 ,4 ,6 ].dup , [1 ,2 ,3 ,4 ,5 ,6 ].dup ), 0.77143 ));
363- assert (approxEqual (spearmanCor([3 ,6 ,7 ,35 ,75 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), 0.3 ));
364- assert (approxEqual (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [3 ,6 ,7 ,35 ,75 ].dup ), 0.3 ));
365- assert (approxEqual (spearmanCor([1.5 ,6.3 ,7.8 ,4.2 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .56429 ));
366- assert (approxEqual (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [1.5 ,6.3 ,7.8 ,4.2 ,1.5 ].dup ), .56429 ));
367- assert (approxEqual (spearmanCor([1.5 ,6.3 ,7.8 ,7.8 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .79057 ));
368- assert (approxEqual (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [1.5 ,6.3 ,7.8 ,7.8 ,1.5 ].dup ), .79057 ));
369- assert (approxEqual (spearmanCor([1.5 ,6.3 ,7.8 ,6.3 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .63246 ));
370- assert (approxEqual (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [1.5 ,6.3 ,7.8 ,6.3 ,1.5 ].dup ), .63246 ));
371- assert (approxEqual (spearmanCor([3 ,4 ,1 ,5 ,2 ,1 ,6 ,4 ].dup , [1 ,3 ,2 ,6 ,4 ,2 ,6 ,7 ].dup ), .6829268 ));
372- assert (approxEqual (spearmanCor([1 ,3 ,2 ,6 ,4 ,2 ,6 ,7 ].dup , [3 ,4 ,1 ,5 ,2 ,1 ,6 ,4 ].dup ), .6829268 ));
361+ assert (isClose (spearmanCor([1 ,2 ,3 ,4 ,5 ,6 ].dup , [3 ,1 ,2 ,5 ,4 ,6 ].dup ), 0.77143 , 1e-2 , 1e-2 ));
362+ assert (isClose (spearmanCor([3 ,1 ,2 ,5 ,4 ,6 ].dup , [1 ,2 ,3 ,4 ,5 ,6 ].dup ), 0.77143 , 1e-2 , 1e-2 ));
363+ assert (isClose (spearmanCor([3 ,6 ,7 ,35 ,75 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), 0.3 , 1e-2 , 1e-2 ));
364+ assert (isClose (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [3 ,6 ,7 ,35 ,75 ].dup ), 0.3 , 1e-2 , 1e-2 ));
365+ assert (isClose (spearmanCor([1.5 ,6.3 ,7.8 ,4.2 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .56429 , 1e-2 , 1e-2 ));
366+ assert (isClose (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [1.5 ,6.3 ,7.8 ,4.2 ,1.5 ].dup ), .56429 , 1e-2 , 1e-2 ));
367+ assert (isClose (spearmanCor([1.5 ,6.3 ,7.8 ,7.8 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .79057 , 1e-2 , 1e-2 ));
368+ assert (isClose (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [1.5 ,6.3 ,7.8 ,7.8 ,1.5 ].dup ), .79057 , 1e-2 , 1e-2 ));
369+ assert (isClose (spearmanCor([1.5 ,6.3 ,7.8 ,6.3 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .63246 , 1e-2 , 1e-2 ));
370+ assert (isClose (spearmanCor([1 ,63 ,53 ,67 ,3 ].dup , [1.5 ,6.3 ,7.8 ,6.3 ,1.5 ].dup ), .63246 , 1e-2 , 1e-2 ));
371+ assert (isClose (spearmanCor([3 ,4 ,1 ,5 ,2 ,1 ,6 ,4 ].dup , [1 ,3 ,2 ,6 ,4 ,2 ,6 ,7 ].dup ), .6829268 , 1e-2 , 1e-2 ));
372+ assert (isClose (spearmanCor([1 ,3 ,2 ,6 ,4 ,2 ,6 ,7 ].dup , [3 ,4 ,1 ,5 ,2 ,1 ,6 ,4 ].dup ), .6829268 , 1e-2 , 1e-2 ));
373373 uint [] one = new uint [1000 ], two = new uint [1000 ];
374374 foreach (i; 0 .. 100 ) { // Further sanity checks for things like commutativity.
375375 size_t lowerBound = uniform(0 , one.length);
@@ -396,10 +396,10 @@ unittest {
396396 two[lowerBound.. upperBound].reverse();
397397 double sFive =
398398 spearmanCor(one[lowerBound.. upperBound], two[lowerBound.. upperBound]);
399- assert (approxEqual (sOne, sTwo) || (isNaN(sOne) && isNaN(sTwo)));
400- assert (approxEqual (sTwo, sThree) || (isNaN(sThree) && isNaN(sTwo)));
401- assert (approxEqual (sThree, sFour) || (isNaN(sThree) && isNaN(sFour)));
402- assert (approxEqual (sFour, sFive) || (isNaN(sFour) && isNaN(sFive)));
399+ assert (isClose (sOne, sTwo, 1e-2 , 1e-2 ) || (isNaN(sOne) && isNaN(sTwo)));
400+ assert (isClose (sTwo, sThree, 1e-2 , 1e-2 ) || (isNaN(sThree) && isNaN(sTwo)));
401+ assert (isClose (sThree, sFour, 1e-2 , 1e-2 ) || (isNaN(sThree) && isNaN(sFour)));
402+ assert (isClose (sFour, sFive, 1e-2 , 1e-2 ) || (isNaN(sFour) && isNaN(sFive)));
403403 }
404404
405405 // Test input ranges.
@@ -420,7 +420,7 @@ unittest {
420420 Count a, b;
421421 a.upTo = 100 ;
422422 b.upTo = 100 ;
423- assert (approxEqual (spearmanCor(a, b), 1 ));
423+ assert (isClose (spearmanCor(a, b), 1 ));
424424}
425425
426426version (unittest ) {
759759
760760unittest {
761761 // Test against known values.
762- assert (approxEqual (kendallCor([1 ,2 ,3 ,4 ,5 ].dup , [3 ,1 ,7 ,4 ,3 ].dup ), 0.1054093 ));
763- assert (approxEqual (kendallCor([3 ,6 ,7 ,35 ,75 ].dup ,[1 ,63 ,53 ,67 ,3 ].dup ), 0.2 ));
764- assert (approxEqual (kendallCor([1.5 ,6.3 ,7.8 ,4.2 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .3162287 ));
762+ assert (isClose (kendallCor([1 ,2 ,3 ,4 ,5 ].dup , [3 ,1 ,7 ,4 ,3 ].dup ), 0.1054093 , 1e-2 , 1e-2 ));
763+ assert (isClose (kendallCor([3 ,6 ,7 ,35 ,75 ].dup ,[1 ,63 ,53 ,67 ,3 ].dup ), 0.2 , 1e-2 , 1e- 2 ));
764+ assert (isClose (kendallCor([1.5 ,6.3 ,7.8 ,4.2 ,1.5 ].dup , [1 ,63 ,53 ,67 ,3 ].dup ), .3162287 , 1e-2 , 1e-2 ));
765765
766766 static void doKendallTest (T)() {
767767 T[] one = new T[1000 ], two = new T[1000 ];
@@ -781,7 +781,7 @@ unittest {
781781 kendallCor(one[lowerBound.. upperBound], two[lowerBound.. upperBound]);
782782 double kTwo =
783783 kendallCorSmallN(one[lowerBound.. upperBound], two[lowerBound.. upperBound]);
784- assert (approxEqual (kOne, kTwo) || (isNaN(kOne) && isNaN(kTwo)));
784+ assert (isClose (kOne, kTwo) || (isNaN(kOne) && isNaN(kTwo)));
785785 }
786786 }
787787
@@ -807,12 +807,12 @@ unittest {
807807 Count a, b;
808808 a.upTo = 100 ;
809809 b.upTo = 100 ;
810- assert (approxEqual (kendallCor(a, b), 1 ));
810+ assert (isClose (kendallCor(a, b), 1 ));
811811
812812 // This test will fail if there are overflow bugs, especially in tie
813813 // handling.
814814 auto rng = chain(repeat(0 , 100_000), repeat(1 , 100_000));
815- assert (approxEqual (kendallCor(rng, rng), 1 ));
815+ assert (isClose (kendallCor(rng, rng), 1 ));
816816
817817 // Test the case where we have one range sorted already.
818818 assert (kendallCor(iota(5 ), [3 , 1 , 2 , 5 , 4 ]) ==
@@ -823,7 +823,7 @@ unittest {
823823 kendallCor([3 , 1 , 2 , 5 , 4 ], assumeSorted(iota(5 )))
824824 );
825825
826- assert (approxEqual (
826+ assert (isClose (
827827 kendallCor(assumeSorted(iota(5 )), assumeSorted (iota(5 ))), 1
828828 ));
829829
@@ -929,11 +929,11 @@ unittest {
929929 uint [] consumerFear = [1 , 2 , 3 , 4 , 5 , 6 , 7 ];
930930 double partialCor =
931931 partial! pearsonCor(stock1Price, stock2Price, [economicHealth, consumerFear][]);
932- assert (approxEqual (partialCor, - 0.857818 ));
932+ assert (isClose (partialCor, - 0.857818 , 1e-2 , 1e-2 ));
933933
934934 double spearmanPartial =
935935 partial! spearmanCor(stock1Price, stock2Price, economicHealth, consumerFear);
936- assert (approxEqual (spearmanPartial, - 0.7252 ));
936+ assert (isClose (spearmanPartial, - 0.7252 , 1e-2 , 1e-2 ));
937937}
938938
939939private __gshared TaskPool emptyPool;
@@ -1001,7 +1001,7 @@ auto input = [[8.0, 6, 7, 5],
10011001 [3.0, 0, 9, 3],
10021002 [1.0, 4, 1, 5]];
10031003auto pearson = pearsonMatrix(input);
1004- assert(approxEqual (pearson[0, 0], 1));
1004+ assert(isClose (pearson[0, 0], 1, 1e-2, 1e-2 ));
10051005---
10061006*/
10071007SymmetricMatrix! double pearsonMatrix (RoR)(RoR mat, TaskPool pool = null )
@@ -1058,7 +1058,7 @@ Examples:
10581058---
10591059auto pearsonRoR = [[0.0], [0.0, 0.0], [0.0, 0.0, 0.0]];
10601060pearsonMatrix(input, pearsonRoR);
1061- assert(approxEqual (pearsonRoR[1][1], 1));
1061+ assert(isClose (pearsonRoR[1][1], 1, 1e-2, 1e-2 ));
10621062---
10631063*/
10641064void pearsonMatrix (RoR, Ret)(RoR mat, ref Ret ans, TaskPool pool = null )
@@ -1298,39 +1298,38 @@ unittest {
12981298
12991299 // Values from R.
13001300
1301- alias approxEqual ae; // Save typing.
1302- assert (ae(pearsonRoR[0 ][0 ], 1 ));
1303- assert (ae(pearsonRoR[1 ][1 ], 1 ));
1304- assert (ae(pearsonRoR[2 ][2 ], 1 ));
1305- assert (ae(pearsonRoR[1 ][0 ], 0.3077935 ));
1306- assert (ae(pearsonRoR[2 ][0 ], - 0.9393364 ));
1307- assert (ae(pearsonRoR[2 ][1 ], - 0.6103679 ));
1308-
1309- assert (ae(spearmanRoR[0 ][0 ], 1 ));
1310- assert (ae(spearmanRoR[1 ][1 ], 1 ));
1311- assert (ae(spearmanRoR[2 ][2 ], 1 ));
1312- assert (ae(spearmanRoR[1 ][0 ], 0.3162278 ));
1313- assert (ae(spearmanRoR[2 ][0 ], - 0.9486833 ));
1314- assert (ae(spearmanRoR[2 ][1 ], - 0.5 ));
1315-
1316- assert (ae(kendallRoR[0 ][0 ], 1 ));
1317- assert (ae(kendallRoR[1 ][1 ], 1 ));
1318- assert (ae(kendallRoR[2 ][2 ], 1 ));
1319- assert (ae(kendallRoR[1 ][0 ], 0.1825742 ));
1320- assert (ae(kendallRoR[2 ][0 ], - 0.9128709 ));
1321- assert (ae(kendallRoR[2 ][1 ], - 0.4 ));
1322-
1323- assert (ae(covRoR[0 ][0 ], 1.66666 ));
1324- assert (ae(covRoR[1 ][1 ], 14.25 ));
1325- assert (ae(covRoR[2 ][2 ], 4.25 ));
1326- assert (ae(covRoR[1 ][0 ], 1.5 ));
1327- assert (ae(covRoR[2 ][0 ], - 2.5 ));
1328- assert (ae(covRoR[2 ][1 ], - 4.75 ));
1301+ assert (isClose(pearsonRoR[0 ][0 ], 1 , 1e-2 , 1e-2 ));
1302+ assert (isClose(pearsonRoR[1 ][1 ], 1 , 1e-2 , 1e-2 ));
1303+ assert (isClose(pearsonRoR[2 ][2 ], 1 , 1e-2 , 1e-2 ));
1304+ assert (isClose(pearsonRoR[1 ][0 ], 0.3077935 , 1e-2 , 1e-2 ));
1305+ assert (isClose(pearsonRoR[2 ][0 ], - 0.9393364 , 1e-2 , 1e-2 ));
1306+ assert (isClose(pearsonRoR[2 ][1 ], - 0.6103679 , 1e-2 , 1e-2 ));
1307+
1308+ assert (isClose(spearmanRoR[0 ][0 ], 1 , 1e-2 , 1e-2 ));
1309+ assert (isClose(spearmanRoR[1 ][1 ], 1 , 1e-2 , 1e-2 ));
1310+ assert (isClose(spearmanRoR[2 ][2 ], 1 , 1e-2 , 1e-2 ));
1311+ assert (isClose(spearmanRoR[1 ][0 ], 0.3162278 , 1e-2 , 1e-2 ));
1312+ assert (isClose(spearmanRoR[2 ][0 ], - 0.9486833 , 1e-2 , 1e-2 ));
1313+ assert (isClose(spearmanRoR[2 ][1 ], - 0.5 , 1e-2 , 1e-2 ));
1314+
1315+ assert (isClose(kendallRoR[0 ][0 ], 1 , 1e-2 , 1e-2 ));
1316+ assert (isClose(kendallRoR[1 ][1 ], 1 , 1e-2 , 1e-2 ));
1317+ assert (isClose(kendallRoR[2 ][2 ], 1 , 1e-2 , 1e-2 ));
1318+ assert (isClose(kendallRoR[1 ][0 ], 0.1825742 , 1e-2 , 1e-2 ));
1319+ assert (isClose(kendallRoR[2 ][0 ], - 0.9128709 , 1e-2 , 1e-2 ));
1320+ assert (isClose(kendallRoR[2 ][1 ], - 0.4 , 1e-2 , 1e-2 ));
1321+
1322+ assert (isClose(covRoR[0 ][0 ], 1.66666 , 1e-2 , 1e-2 ));
1323+ assert (isClose(covRoR[1 ][1 ], 14.25 , 1e-2 , 1e-2 ));
1324+ assert (isClose(covRoR[2 ][2 ], 4.25 , 1e-2 , 1e-2 ));
1325+ assert (isClose(covRoR[1 ][0 ], 1.5 , 1e-2 , 1e-2 ));
1326+ assert (isClose(covRoR[2 ][0 ], - 2.5 , 1e-2 , 1e-2 ));
1327+ assert (isClose(covRoR[2 ][1 ], - 4.75 , 1e-2 , 1e-2 ));
13291328
13301329 version (scid) {
13311330 static bool test (double [][] a, SymmetricMatrix! double b) {
13321331 foreach (i; 0 .. 3 ) foreach (j; 0 .. i + 1 ) {
1333- if (! ae (a[i][j], b[i, j])) return false ;
1332+ if (! isClose (a[i][j], b[i, j], 1e-2 , 1e-2 )) return false ;
13341333 }
13351334
13361335 return true ;
0 commit comments