File: bivariate_statistics_test.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (638 lines) | stat: -rw-r--r-- 20,031 bytes parent folder | download | duplicates (11)
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
/*
 *  (C) Copyright Nick Thompson 2018.
 *  (C) Copyright Matt Borland 2021.
 *  Use, modification and distribution are subject to the
 *  Boost Software License, Version 1.0. (See accompanying file
 *  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include <iostream>
#include <iomanip>
#include <vector>
#include <array>
#include <forward_list>
#include <algorithm>
#include <random>
#include <tuple>
#include <cmath>
#include "math_unit_test.hpp"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/math/statistics/bivariate_statistics.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_complex.hpp>
#include <boost/math/statistics/univariate_statistics.hpp>

using boost::multiprecision::cpp_bin_float_50;
using boost::multiprecision::cpp_complex_50;

/*
 * Test checklist:
 * 1) Does it work with multiprecision?
 * 2) Does it work with .cbegin()/.cend() if the data is not altered?
 * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.)
 * 4) Does it work with std::forward_list if a forward iterator is all that is required?
 * 5) Does it work with complex data if complex data is sensible?
 */

using  boost::math::statistics::means_and_covariance;
using  boost::math::statistics::covariance;

#ifndef BOOST_NO_CXX17_HDR_EXECUTION
#include <execution>

template<typename Real, typename ExecutionPolicy>
void test_covariance(ExecutionPolicy&& exec)
{
    std::cout << std::setprecision(std::numeric_limits<Real>::digits10+1);
    Real tol = std::numeric_limits<Real>::epsilon();
    using std::abs;

    // Covariance of a single thing is zero:
    std::array<Real, 1> u1{8};
    std::array<Real, 1> v1{17};
    std::tuple<Real, Real, Real> temp = means_and_covariance(exec, u1, v1);
    Real mu_u1 = std::get<0>(temp);
    Real mu_v1 = std::get<1>(temp);
    Real cov1 = std::get<2>(temp);

    CHECK_LE(abs(cov1), tol);
    CHECK_LE(abs(mu_u1 - 8), tol);
    CHECK_LE(abs(mu_v1 - 17), tol);


    std::array<Real, 2> u2{8, 4};
    std::array<Real, 2> v2{3, 7};
    temp = means_and_covariance(exec, u2, v2);
    Real mu_u2 = std::get<0>(temp);
    Real mu_v2 = std::get<1>(temp);
    Real cov2 = std::get<2>(temp);

    CHECK_LE(abs(cov2+4), tol);
    CHECK_LE(abs(mu_u2 - 6), tol);
    CHECK_LE(abs(mu_v2 - 5), tol);

    std::vector<Real> u3{1,2,3};
    std::vector<Real> v3{1,1,1};

    temp = means_and_covariance(exec, u3, v3);
    Real mu_u3 = std::get<0>(temp);
    Real mu_v3 = std::get<1>(temp);
    Real cov3 = std::get<2>(temp);

    // Since v is constant, covariance(u,v) = 0 against everything any u:
    CHECK_LE(abs(cov3), tol);
    CHECK_LE(abs(mu_u3 - 2), tol);
    CHECK_LE(abs(mu_v3 - 1), tol);
    // Make sure we pull the correct symbol out of means_and_covariance:
    cov3 = covariance(exec, u3, v3);
    CHECK_LE(abs(cov3), tol);

    cov3 = covariance(exec, v3, u3);
    // Covariance is symmetric: cov(u,v) = cov(v,u)
    CHECK_LE(abs(cov3), tol);

    // cov(u,u) = sigma(u)^2:
    cov3 = covariance(exec, u3, u3);
    Real expected = Real(2)/Real(3);

    CHECK_LE(abs(cov3 - expected), tol);

    std::mt19937 gen(15);
    // Can't template standard library on multiprecision, so use double and cast back:
    std::uniform_real_distribution<double> dis(-1.0, 1.0);
    std::vector<Real> u(500);
    std::vector<Real> v(500);
    for(size_t i = 0; i < u.size(); ++i)
    {
        u[i] = (Real) dis(gen);
        v[i] = (Real) dis(gen);
    }

    Real mu_u = boost::math::statistics::mean(u);
    Real mu_v = boost::math::statistics::mean(v);
    Real sigma_u_sq = boost::math::statistics::variance(u);
    Real sigma_v_sq = boost::math::statistics::variance(v);

    temp = means_and_covariance(exec, u, v);
    Real mu_u_ = std::get<0>(temp);
    Real mu_v_ = std::get<1>(temp);
    Real cov_uv = std::get<2>(temp);

    CHECK_LE(abs(mu_u - mu_u_), tol);
    CHECK_LE(abs(mu_v - mu_v_), tol);

    // Cauchy-Schwartz inequality:
    CHECK_LE(cov_uv*cov_uv, sigma_u_sq*sigma_v_sq);
    // cov(X, X) = sigma(X)^2:
    Real cov_uu = covariance(exec, u, u);
    CHECK_LE(abs(cov_uu - sigma_u_sq), tol);
    Real cov_vv = covariance(exec, v, v);
    CHECK_LE(abs(cov_vv - sigma_v_sq), tol);
}

template<typename Z, typename ExecutionPolicy>
void test_integer_covariance(ExecutionPolicy&& exec)
{
    std::cout << std::setprecision(std::numeric_limits<double>::digits10+1);
    double tol = std::numeric_limits<double>::epsilon();
    using std::abs;

    // Covariance of a single thing is zero:
    std::array<Z, 1> u1{8};
    std::array<Z, 1> v1{17};
    std::tuple<double, double, double> temp = means_and_covariance(exec, u1, v1);
    double mu_u1 = std::get<0>(temp);
    double mu_v1 = std::get<1>(temp);
    double cov1 = std::get<2>(temp);

    CHECK_LE(abs(cov1), tol);
    CHECK_LE(abs(mu_u1 - 8), tol);
    CHECK_LE(abs(mu_v1 - 17), tol);


    std::array<Z, 2> u2{8, 4};
    std::array<Z, 2> v2{3, 7};
    temp = means_and_covariance(exec, u2, v2);
    double mu_u2 = std::get<0>(temp);
    double mu_v2 = std::get<1>(temp);
    double cov2 = std::get<2>(temp);

    CHECK_LE(abs(cov2+4), tol);
    CHECK_LE(abs(mu_u2 - 6), tol);
    CHECK_LE(abs(mu_v2 - 5), tol);

    std::vector<Z> u3{1,2,3};
    std::vector<Z> v3{1,1,1};

    temp = means_and_covariance(exec, u3, v3);
    double mu_u3 = std::get<0>(temp);
    double mu_v3 = std::get<1>(temp);
    double cov3 = std::get<2>(temp);

    // Since v is constant, covariance(u,v) = 0 against everything any u:
    CHECK_LE(abs(cov3), tol);
    CHECK_LE(abs(mu_u3 - 2), tol);
    CHECK_LE(abs(mu_v3 - 1), tol);
    // Make sure we pull the correct symbol out of means_and_covariance:
    cov3 = covariance(exec, u3, v3);
    CHECK_LE(abs(cov3), tol);

    cov3 = covariance(exec, v3, u3);
    // Covariance is symmetric: cov(u,v) = cov(v,u)
    CHECK_LE(abs(cov3), tol);

    // cov(u,u) = sigma(u)^2:
    cov3 = covariance(exec, u3, u3);
    double expected = double(2)/double(3);

    CHECK_LE(abs(cov3 - expected), tol);

    std::mt19937 gen(15);
    // Can't template standard library on multiprecision, so use double and cast back:
    std::uniform_real_distribution<double> dis(-1.0, 1.0);
    std::vector<Z> u(500);
    std::vector<Z> v(500);
    for(size_t i = 0; i < u.size(); ++i)
    {
        u[i] = (Z) dis(gen);
        v[i] = (Z) dis(gen);
    }

    double mu_u = boost::math::statistics::mean(u);
    double mu_v = boost::math::statistics::mean(v);
    double sigma_u_sq = boost::math::statistics::variance(u);
    double sigma_v_sq = boost::math::statistics::variance(v);

    temp = means_and_covariance(exec, u, v);
    double mu_u_ = std::get<0>(temp);
    double mu_v_ = std::get<1>(temp);
    double cov_uv = std::get<2>(temp);

    CHECK_LE(abs(mu_u - mu_u_), tol);
    CHECK_LE(abs(mu_v - mu_v_), tol);

    // Cauchy-Schwartz inequality:
    CHECK_LE(cov_uv*cov_uv, sigma_u_sq*sigma_v_sq);
    // cov(X, X) = sigma(X)^2:
    double cov_uu = covariance(exec, u, u);
    CHECK_LE(abs(cov_uu - sigma_u_sq), tol);
    double cov_vv = covariance(exec, v, v);
    CHECK_LE(abs(cov_vv - sigma_v_sq), tol);
}

template<typename Real, typename ExecutionPolicy>
void test_correlation_coefficient(ExecutionPolicy&& exec)
{
    using boost::math::statistics::correlation_coefficient;
    using std::abs;
    using std::sqrt;

    Real tol = std::numeric_limits<Real>::epsilon();
    std::vector<Real> u{1};
    std::vector<Real> v{1};
    Real rho_uv = correlation_coefficient(exec, u, v);
    CHECK_NAN(rho_uv);

    u = {1,1};
    v = {1,1};
    rho_uv = correlation_coefficient(exec, u, v);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {1, 2, 3};
    rho_uv = correlation_coefficient(exec, u, v);
    CHECK_LE(abs(rho_uv - 1), tol);

    u = {1, 2, 3};
    v = {-1, -2, -3};
    rho_uv = correlation_coefficient(exec, u, v);
    CHECK_LE(abs(rho_uv + 1), tol);

    rho_uv = correlation_coefficient(exec, v, u);
    CHECK_LE(abs(rho_uv + 1), tol);

    u = {1, 2, 3};
    v = {0, 0, 0};
    rho_uv = correlation_coefficient(exec, v, u);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {0, 0, 3};
    rho_uv = correlation_coefficient(exec, v, u);
    // mu_u = 2, sigma_u^2 = 2/3, mu_v = 1, sigma_v^2 = 2, cov(u,v) = 1.
    CHECK_LE(abs(rho_uv - sqrt(Real(3))/Real(2)), tol);
}

template<typename Z, typename ExecutionPolicy>
void test_integer_correlation_coefficient(ExecutionPolicy&& exec)
{
    using boost::math::statistics::correlation_coefficient;
    using std::abs;
    using std::sqrt;

    double tol = std::numeric_limits<double>::epsilon();
    std::vector<Z> u{1};
    std::vector<Z> v{1};
    double rho_uv = correlation_coefficient(exec, u, v);
    CHECK_NAN(rho_uv);

    u = {1,1};
    v = {1,1};
    rho_uv = correlation_coefficient(exec, u, v);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {1, 2, 3};
    rho_uv = correlation_coefficient(exec, u, v);
    CHECK_LE(abs(rho_uv - 1.0), tol);

    rho_uv = correlation_coefficient(exec, v, u);
    CHECK_LE(abs(rho_uv - 1.0), tol);

    u = {1, 2, 3};
    v = {0, 0, 0};
    rho_uv = correlation_coefficient(exec, v, u);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {0, 0, 3};
    rho_uv = correlation_coefficient(exec, v, u);
    // mu_u = 2, sigma_u^2 = 2/3, mu_v = 1, sigma_v^2 = 2, cov(u,v) = 1.
    CHECK_LE(abs(rho_uv - sqrt(double(3))/double(2)), tol);
}

int main()
{
    test_covariance<float>(std::execution::seq);
    test_covariance<float>(std::execution::par);
    test_covariance<double>(std::execution::seq);
    test_covariance<double>(std::execution::par);
    test_covariance<long double>(std::execution::seq);
    test_covariance<long double>(std::execution::par);
    test_covariance<cpp_bin_float_50>(std::execution::seq);
    test_covariance<cpp_bin_float_50>(std::execution::par);
    
    test_integer_covariance<int>(std::execution::seq);
    test_integer_covariance<int>(std::execution::par);
    test_integer_covariance<int32_t>(std::execution::seq);
    test_integer_covariance<int32_t>(std::execution::par);
    test_integer_covariance<int64_t>(std::execution::seq);
    test_integer_covariance<int64_t>(std::execution::par);
    test_integer_covariance<uint32_t>(std::execution::seq);
    test_integer_covariance<uint32_t>(std::execution::par);

    test_correlation_coefficient<float>(std::execution::seq);
    test_correlation_coefficient<float>(std::execution::par);
    test_correlation_coefficient<double>(std::execution::seq);
    test_correlation_coefficient<double>(std::execution::par);
    test_correlation_coefficient<long double>(std::execution::seq);
    test_correlation_coefficient<long double>(std::execution::par);
    test_correlation_coefficient<cpp_bin_float_50>(std::execution::seq);
    test_correlation_coefficient<cpp_bin_float_50>(std::execution::par);
    
    test_integer_correlation_coefficient<int>(std::execution::seq);
    test_integer_correlation_coefficient<int>(std::execution::par);
    test_integer_correlation_coefficient<int32_t>(std::execution::seq);
    test_integer_correlation_coefficient<int32_t>(std::execution::par);
    test_integer_correlation_coefficient<int64_t>(std::execution::seq);
    test_integer_correlation_coefficient<int64_t>(std::execution::par);
    test_integer_correlation_coefficient<uint32_t>(std::execution::seq);
    test_integer_correlation_coefficient<uint32_t>(std::execution::par);
    
    return boost::math::test::report_errors();
}

#else

template<typename Real>
void test_covariance()
{
    std::cout << std::setprecision(std::numeric_limits<Real>::digits10+1);
    Real tol = std::numeric_limits<Real>::epsilon();
    using std::abs;

    // Covariance of a single thing is zero:
    std::array<Real, 1> u1{8};
    std::array<Real, 1> v1{17};
    std::tuple<Real, Real, Real> temp = means_and_covariance(u1, v1);
    Real mu_u1 = std::get<0>(temp);
    Real mu_v1 = std::get<1>(temp);
    Real cov1 = std::get<2>(temp);

    CHECK_LE(abs(cov1), tol);
    CHECK_LE(abs(mu_u1 - 8), tol);
    CHECK_LE(abs(mu_v1 - 17), tol);


    std::array<Real, 2> u2{8, 4};
    std::array<Real, 2> v2{3, 7};
    temp = means_and_covariance(u2, v2);
    Real mu_u2 = std::get<0>(temp);
    Real mu_v2 = std::get<1>(temp);
    Real cov2 = std::get<2>(temp);

    CHECK_LE(abs(cov2+4), tol);
    CHECK_LE(abs(mu_u2 - 6), tol);
    CHECK_LE(abs(mu_v2 - 5), tol);

    std::vector<Real> u3{1,2,3};
    std::vector<Real> v3{1,1,1};

    temp = means_and_covariance(u3, v3);
    Real mu_u3 = std::get<0>(temp);
    Real mu_v3 = std::get<1>(temp);
    Real cov3 = std::get<2>(temp);

    // Since v is constant, covariance(u,v) = 0 against everything any u:
    CHECK_LE(abs(cov3), tol);
    CHECK_LE(abs(mu_u3 - 2), tol);
    CHECK_LE(abs(mu_v3 - 1), tol);
    // Make sure we pull the correct symbol out of means_and_covariance:
    cov3 = covariance(u3, v3);
    CHECK_LE(abs(cov3), tol);

    cov3 = covariance(v3, u3);
    // Covariance is symmetric: cov(u,v) = cov(v,u)
    CHECK_LE(abs(cov3), tol);

    // cov(u,u) = sigma(u)^2:
    cov3 = covariance(u3, u3);
    Real expected = Real(2)/Real(3);

    CHECK_LE(abs(cov3 - expected), tol);

    std::mt19937 gen(15);
    // Can't template standard library on multiprecision, so use double and cast back:
    std::uniform_real_distribution<double> dis(-1.0, 1.0);
    std::vector<Real> u(500);
    std::vector<Real> v(500);
    for(size_t i = 0; i < u.size(); ++i)
    {
        u[i] = (Real) dis(gen);
        v[i] = (Real) dis(gen);
    }

    Real mu_u = boost::math::statistics::mean(u);
    Real mu_v = boost::math::statistics::mean(v);
    Real sigma_u_sq = boost::math::statistics::variance(u);
    Real sigma_v_sq = boost::math::statistics::variance(v);

    temp = means_and_covariance(u, v);
    Real mu_u_ = std::get<0>(temp);
    Real mu_v_ = std::get<1>(temp);
    Real cov_uv = std::get<2>(temp);

    CHECK_LE(abs(mu_u - mu_u_), tol);
    CHECK_LE(abs(mu_v - mu_v_), tol);

    // Cauchy-Schwartz inequality:
    CHECK_LE(cov_uv*cov_uv, sigma_u_sq*sigma_v_sq);
    // cov(X, X) = sigma(X)^2:
    Real cov_uu = covariance(u, u);
    CHECK_LE(abs(cov_uu - sigma_u_sq), tol);
    Real cov_vv = covariance(v, v);
    CHECK_LE(abs(cov_vv - sigma_v_sq), tol);
}

template<typename Z>
void test_integer_covariance()
{
    std::cout << std::setprecision(std::numeric_limits<double>::digits10+1);
    double tol = std::numeric_limits<double>::epsilon();
    using std::abs;

    // Covariance of a single thing is zero:
    std::array<Z, 1> u1{8};
    std::array<Z, 1> v1{17};
    std::tuple<double, double, double> temp = means_and_covariance(u1, v1);
    double mu_u1 = std::get<0>(temp);
    double mu_v1 = std::get<1>(temp);
    double cov1 = std::get<2>(temp);

    CHECK_LE(abs(cov1), tol);
    CHECK_LE(abs(mu_u1 - 8), tol);
    CHECK_LE(abs(mu_v1 - 17), tol);


    std::array<Z, 2> u2{8, 4};
    std::array<Z, 2> v2{3, 7};
    temp = means_and_covariance(u2, v2);
    double mu_u2 = std::get<0>(temp);
    double mu_v2 = std::get<1>(temp);
    double cov2 = std::get<2>(temp);

    CHECK_LE(abs(cov2+4), tol);
    CHECK_LE(abs(mu_u2 - 6), tol);
    CHECK_LE(abs(mu_v2 - 5), tol);

    std::vector<Z> u3{1,2,3};
    std::vector<Z> v3{1,1,1};

    temp = means_and_covariance(u3, v3);
    double mu_u3 = std::get<0>(temp);
    double mu_v3 = std::get<1>(temp);
    double cov3 = std::get<2>(temp);

    // Since v is constant, covariance(u,v) = 0 against everything any u:
    CHECK_LE(abs(cov3), tol);
    CHECK_LE(abs(mu_u3 - 2), tol);
    CHECK_LE(abs(mu_v3 - 1), tol);
    // Make sure we pull the correct symbol out of means_and_covariance:
    cov3 = covariance(u3, v3);
    CHECK_LE(abs(cov3), tol);

    cov3 = covariance(v3, u3);
    // Covariance is symmetric: cov(u,v) = cov(v,u)
    CHECK_LE(abs(cov3), tol);

    // cov(u,u) = sigma(u)^2:
    cov3 = covariance(u3, u3);
    double expected = double(2)/double(3);

    CHECK_LE(abs(cov3 - expected), tol);

    std::mt19937 gen(15);
    // Can't template standard library on multiprecision, so use double and cast back:
    std::uniform_real_distribution<double> dis(-1.0, 1.0);
    std::vector<Z> u(500);
    std::vector<Z> v(500);
    for(size_t i = 0; i < u.size(); ++i)
    {
        u[i] = (Z) dis(gen);
        v[i] = (Z) dis(gen);
    }

    double mu_u = boost::math::statistics::mean(u);
    double mu_v = boost::math::statistics::mean(v);
    double sigma_u_sq = boost::math::statistics::variance(u);
    double sigma_v_sq = boost::math::statistics::variance(v);

    temp = means_and_covariance(u, v);
    double mu_u_ = std::get<0>(temp);
    double mu_v_ = std::get<1>(temp);
    double cov_uv = std::get<2>(temp);

    CHECK_LE(abs(mu_u - mu_u_), tol);
    CHECK_LE(abs(mu_v - mu_v_), tol);

    // Cauchy-Schwartz inequality:
    CHECK_LE(cov_uv*cov_uv, sigma_u_sq*sigma_v_sq);
    // cov(X, X) = sigma(X)^2:
    double cov_uu = covariance(u, u);
    CHECK_LE(abs(cov_uu - sigma_u_sq), tol);
    double cov_vv = covariance(v, v);
    CHECK_LE(abs(cov_vv - sigma_v_sq), tol);
}

template<typename Real>
void test_correlation_coefficient()
{
    using boost::math::statistics::correlation_coefficient;
    using std::abs;
    using std::sqrt;

    Real tol = std::numeric_limits<Real>::epsilon();
    std::vector<Real> u{1};
    std::vector<Real> v{1};
    Real rho_uv = correlation_coefficient(u, v);
    CHECK_NAN(rho_uv);

    u = {1,1};
    v = {1,1};
    rho_uv = correlation_coefficient(u, v);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {1, 2, 3};
    rho_uv = correlation_coefficient(u, v);
    CHECK_LE(abs(rho_uv - 1), tol);

    u = {1, 2, 3};
    v = {-1, -2, -3};
    rho_uv = correlation_coefficient(u, v);
    CHECK_LE(abs(rho_uv + 1), tol);

    rho_uv = correlation_coefficient(v, u);
    CHECK_LE(abs(rho_uv + 1), tol);

    u = {1, 2, 3};
    v = {0, 0, 0};
    rho_uv = correlation_coefficient(v, u);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {0, 0, 3};
    rho_uv = correlation_coefficient(v, u);
    // mu_u = 2, sigma_u^2 = 2/3, mu_v = 1, sigma_v^2 = 2, cov(u,v) = 1.
    CHECK_LE(abs(rho_uv - sqrt(Real(3))/Real(2)), tol);
}

template<typename Z>
void test_integer_correlation_coefficient()
{
    using boost::math::statistics::correlation_coefficient;
    using std::abs;
    using std::sqrt;

    double tol = std::numeric_limits<double>::epsilon();
    std::vector<Z> u{1};
    std::vector<Z> v{1};
    double rho_uv = correlation_coefficient(u, v);
    CHECK_NAN(rho_uv);

    u = {1,1};
    v = {1,1};
    rho_uv = correlation_coefficient(u, v);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {1, 2, 3};
    rho_uv = correlation_coefficient(u, v);
    CHECK_LE(abs(rho_uv - 1.0), tol);

    rho_uv = correlation_coefficient(v, u);
    CHECK_LE(abs(rho_uv - 1.0), tol);

    u = {1, 2, 3};
    v = {0, 0, 0};
    rho_uv = correlation_coefficient(v, u);
    CHECK_NAN(rho_uv);

    u = {1, 2, 3};
    v = {0, 0, 3};
    rho_uv = correlation_coefficient(v, u);
    // mu_u = 2, sigma_u^2 = 2/3, mu_v = 1, sigma_v^2 = 2, cov(u,v) = 1.
    CHECK_LE(abs(rho_uv - sqrt(double(3))/double(2)), tol);
}

int main()
{
    test_covariance<float>();
    test_covariance<double>();
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
    test_covariance<long double>();
#endif
    test_covariance<cpp_bin_float_50>();

    test_integer_covariance<int>();
    test_integer_covariance<int32_t>();
    test_integer_covariance<int64_t>();
    test_integer_covariance<uint32_t>();

    test_correlation_coefficient<float>();
    test_correlation_coefficient<double>();
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
    test_correlation_coefficient<long double>();
#endif
    test_correlation_coefficient<cpp_bin_float_50>();

    test_integer_correlation_coefficient<int>();
    test_integer_correlation_coefficient<int32_t>();
    test_integer_correlation_coefficient<int64_t>();
    test_integer_correlation_coefficient<uint32_t>();

    return boost::math::test::report_errors();
}

#endif