libsequence  1.9.5
correlations.cc
1 #include <Sequence/Correlations.hpp>
2 #include <string>
3 #include <iostream>
4 #include <iterator>
5 #include <algorithm>
6 #include <vector>
7 #include <ctime>
8 #include <cstdlib>
9 #include <cstdio>
10 #include <random>
11 
12 //simple wrapper for us to use
13 void shuffle_it( std::vector<int>::iterator beg,
14  std::vector<int>::iterator end,
15  std::mt19937 & generator )
16 {
17  std::shuffle(beg,end,generator);
18 }
19 
20 int main()
21 {
22  std::vector<int> x(12);
23  std::vector<double> y(12);
24 
25  x[0]=159;
26  x[1]=179;
27  x[2]=100;
28  x[3]=45;
29  x[4]=384;
30  x[5]=230;
31  x[6]=100;
32  x[7]=320;
33  x[8]=80;
34  x[9]=220;
35  x[10]=320;
36  x[11]=210;
37  y[0]=14.4;
38  y[1]=15.2;
39  y[2]=11.3;
40  y[3]=2.5;
41  y[4]=22.7;
42  y[5]=14.9;
43  y[6]=1.41;
44  y[7]=15.81;
45  y[8]=4.19;
46  y[9]=15.39;
47  y[10]=17.25;
48  y[11]=9.52;
49 
50  std::mt19937 generator(std::time(0));
51 
52  std::cout << "original data:\n";
53  for( const auto _x : x )
54  {
55  std::cout << _x << ' ';
56  }
57  std::cout << std::endl;
58  for ( const auto _y : y )
59  {
60  std::cerr << _y << ' ';
61  }
62  std::cout << std::endl;
63  std::cout << "Product-moment correlation: "
64  << Sequence::ProductMoment()( x.begin(),x.end(),y.begin() )
65  <<std::endl
66  << "Spearman's rank correlation: "
67  << Sequence::SpearmansRank()( x.begin(),x.end(),y.begin() )<<std::endl;
68 
69 
70  std::function<void(std::vector<int>::iterator,
71  std::vector<int>::iterator)> __x = [&generator](std::vector<int>::iterator a,
72  std::vector<int>::iterator b) { return shuffle_it(a,b,generator); };
73 
74  std::cout << "(all pvalues are one-tailed tests of observing a more extreme,\n"
75  << "i.e. larger correlation than the observe dvalue)\n"
76  << "pval of P-M correlation, seed with time: "
77  << Sequence::PermuteCorrelation(x.begin(),x.end(),y.begin(),
78  Sequence::ProductMoment(),
79  std::greater_equal<double>(),
80  __x) << '\n'
81  << "pval of rank correlation, default seed: "
82  << Sequence::PermuteCorrelation(x.begin(),x.end(),
83  y.begin(),
84  Sequence::SpearmansRank(),
85  std::greater_equal<double>(),
86  __x) << std::endl
87  << "pval of rank correlation, seed with time: "
88  << Sequence::PermuteCorrelation(x.begin(),x.end(),y.begin(),
89  Sequence::SpearmansRank(),
90  std::greater_equal<double>(),
91  __x) <<std::endl;
92 
93  std::cout << "original data are intact:\n";
94  for( const auto _x : x )
95  {
96  std::cout << _x << ' ';
97  }
98  std::cout << std::endl;
99  for( const auto _y : y )
100  {
101  std::cout << _y << ' ';
102  }
103  std::cout << std::endl;
104 }