-
Notifications
You must be signed in to change notification settings - Fork 157
/
GenUtils.cpp
2506 lines (2261 loc) · 70.4 KB
/
GenUtils.cpp
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
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cfloat>
#include <iomanip>
#include <limits>
#include <math.h>
#include <sstream>
#include <boost/math/distributions/students_t.hpp>
#include <boost/thread.hpp>
#include <wx/dc.h>
#include <wx/msgdlg.h>
#include <wx/stdpaths.h>
#include <wx/regex.h>
#include "GdaConst.h"
#include "GenUtils.h"
#include "Explore/CatClassification.h"
int StringUtils::utf8_strlen(const std::string& str)
{
int c,i,q;
for (q=0, i=0; i < str.length(); i++, q++)
{
c = (unsigned char) str[i];
if (c>=0 && c<=127) i+=0;
else if ((c & 0xE0) == 0xC0) i+=1;
else if ((c & 0xF0) == 0xE0) i+=2;
else if ((c & 0xF8) == 0xF0) i+=3;
//else if (($c & 0xFC) == 0xF8) i+=4; // 111110bb //byte 5, unnecessary in 4 byte UTF-8
//else if (($c & 0xFE) == 0xFC) i+=5; // 1111110b //byte 6, unnecessary in 4 byte UTF-8
else return 0;//invalid utf8
}
return q;
}
void DbfFileUtils::SuggestDoubleParams(int length, int decimals,
int* suggest_len, int* suggest_dec)
{
// doubles have 52 bits for the mantissa, so we can allow at most
// floor(log(2^52)) = 15 digits of precision.
// We require that there length-2 >= decimals to allow for "x." . when
// writing to disk, and when decimals = 15, require length >= 17 to
// allow for "0." prefex. If length-2 == decimals, then negative numbers
// are not allowed since there is not room for the "-0." prefix.
if (GdaConst::max_dbf_double_len < length) {
length = GdaConst::max_dbf_double_len;
}
if (length < 3) length = 3;
if (decimals < 1) decimals = 1;
if (decimals > 15) decimals = 15;
if (length-2 < decimals) length = decimals + 2;
*suggest_len = length;
*suggest_dec = decimals;
}
double DbfFileUtils::GetMaxDouble(int length, int decimals,
int* suggest_len, int* suggest_dec)
{
// make sure that length and decimals have legal values
SuggestDoubleParams(length, decimals, &length, &decimals);
int len_inter = length - (1+decimals);
//if (len_inter + decimals > 15) len_inter = 15-decimals;
double r = 0;
for (int i=0; i<len_inter+decimals; i++) r = r*10 + 9;
for (int i=0; i<decimals; i++) r /= 10;
if (suggest_len) *suggest_len = length;
if (suggest_dec) *suggest_dec = decimals;
return r;
}
wxString DbfFileUtils::GetMaxDoubleString(int length, int decimals)
{
double x = GetMaxDouble(length, decimals, &length, &decimals);
return wxString::Format("%.*f", decimals, x);
}
double DbfFileUtils::GetMinDouble(int length, int decimals,
int* suggest_len, int* suggest_dec)
{
SuggestDoubleParams(length, decimals, &length, &decimals);
if (length-2 == decimals) return 0;
if (suggest_len) *suggest_len = length;
if (suggest_dec) *suggest_dec = decimals;
return -DbfFileUtils::GetMaxDouble(length-1, decimals);
}
wxString DbfFileUtils::GetMinDoubleString(int length, int decimals)
{
double x = GetMinDouble(length, decimals, &length, &decimals);
if (length-2 == decimals) {
wxString s("0.");
for (int i=0; i<decimals; i++) s += "0";
return s;
}
return wxString::Format("%.*f", decimals, x);
}
wxInt64 DbfFileUtils::GetMaxInt(int length)
{
// We want to allow the user to enter a string of
// all 9s for the largest value reported. So, we must
// limit the length of the string to be floor(log(2^63)) = 18
if (length < 1) return 0;
if (length > 18) length = 18;
wxInt64 r=0;
for (int i=0; i<length; i++) r = r*10 + 9;
return r;
}
wxString DbfFileUtils::GetMaxIntString(int length)
{
if (length < 19)
return wxString::Format("%lld", GetMaxInt(length));
else
return "9223372036854775807"; // max value of int64
}
wxInt64 DbfFileUtils::GetMinInt(int length)
{
// This is generally the -GetMaxInt(length-1), because we must
// allow one character for the minus sign unless the length
// is greater than 18;
if (length > 19) length = 19;
return -GetMaxInt(length-1);
}
wxString DbfFileUtils::GetMinIntString(int length)
{
if (length < 19)
return wxString::Format("%lld", GetMinInt(length));
else
return "-9223372036854775808"; // min value of int64
}
wxString Gda::DetectDateFormat(wxString s, std::vector<wxString>& date_items)
{
// input s could be sth. like: %Y-%m-%d %H:%M:%S
// 2015-1-11 13:57:24 %Y-%m-%d %H:%M:%S
wxString YY = "([0-9]{4})";
wxString yy = "([0-9]{2})";
wxString MM = "([0-9]{1,2})";//"(0?[1-9]|1[0-2])";
wxString DD = "([0-9]{1,2})";//"(0?[1-9]|[12][0-9]|3[01])";
wxString ii = "([0-9]{1,2})";//"(00|[0-9]|1[0-9]|2[0-3])";
wxString hh = "([0-9]{1,2})";//"(00|[0-9]|1[0-9]|2[0-3])";
wxString mm = "([0-9]{1,2})";//"([0-9]|[0-5][0-9])";
wxString ss = "([0-9]{1,2})"; //"([0-9]|[0-5][0-9])";
wxString pp = "([AP]M)"; //"(AM | PM)";
wxString pattern;
wxString original_pattern;
for (int i=0; i<GdaConst::gda_datetime_formats.size(); i++) {
original_pattern = GdaConst::gda_datetime_formats[i];
wxString select_pattern = original_pattern;
select_pattern.Replace("%Y", YY);
select_pattern.Replace("%y", yy);
select_pattern.Replace("%m", MM);
select_pattern.Replace("%d", DD);
select_pattern.Replace("%I", ii);
select_pattern.Replace("%H", hh);
select_pattern.Replace("%M", mm);
select_pattern.Replace("%S", ss);
select_pattern.Replace("%p", pp);
select_pattern = "^" + select_pattern + "$";
wxRegEx regex(select_pattern);
if (regex.IsValid()) {
if (regex.Matches(s)) {
if (regex.GetMatchCount()>0) {
pattern = select_pattern;
break;
}
}
}
}
if (!pattern.IsEmpty()){
wxString select_pattern = original_pattern;
wxRegEx regex("(%[YymdIHMSp])");
while (regex.Matches(select_pattern) ) {
size_t start, len;
regex.GetMatch(&start, &len, 0);
date_items.push_back(regex.GetMatch(select_pattern, 1));
select_pattern = select_pattern.Mid (start + len);
}
}
return pattern;
}
// wxRegEx regex
// regex.Compile(pattern);
unsigned long long Gda::DateToNumber(wxString s_date, wxRegEx& regex, std::vector<wxString>& date_items)
{
unsigned long long val = 0;
if (regex.Matches(s_date)) {
int n = (int)regex.GetMatchCount();
wxString _year, _short_year, _month, _day, _hour, _minute, _second, _am_pm;
long _l_year =0, _l_short_year=0, _l_month=0, _l_day=0, _l_hour=0, _l_minute=0, _l_second=0;
for (int i=1; i<n; i++) {
if (date_items[i-1] == "%Y") {
_year = regex.GetMatch(s_date, i);
_year.ToLong(&_l_year);
} else if (date_items[i-1] == "%y") {
_short_year = regex.GetMatch(s_date, i);
if( _short_year.ToLong(&_l_short_year)) {
_l_year = _l_short_year < 50 ? 2000 + _l_short_year : 1900 + _l_short_year;
}
} else if (date_items[i-1] == "%m") {
_month = regex.GetMatch(s_date, i);
_month.ToLong(&_l_month);
} else if (date_items[i-1] == "%d") {
_day = regex.GetMatch(s_date, i);
_day.ToLong(&_l_day);
} else if (date_items[i-1] == "%H" || date_items[i-1] == "%I") {
_hour = regex.GetMatch(s_date, i);
_hour.ToLong(&_l_hour);
} else if (date_items[i-1] == "%M") {
_minute = regex.GetMatch(s_date, i);
_minute.ToLong(&_l_minute);
} else if (date_items[i-1] == "%S") {
_second = regex.GetMatch(s_date, i);
_second.ToLong(&_l_second);
} else if (date_items[i-1] == "%p") {
_am_pm = regex.GetMatch(s_date, i);
}
}
if (!_am_pm.IsEmpty()) {
if (_am_pm.CmpNoCase("PM") == 0) {
_l_hour += 12;
}
}
val = _l_year * 10000000000 + _l_month * 100000000 + _l_day * 1000000 + _l_hour * 10000 + _l_minute * 100 + _l_second;
}
return val;
}
void GdaColorUtils::GetUnique20Colors(std::vector<wxColour>& colors)
{
colors.clear();
colors.push_back(wxColour(166,206,227));
colors.push_back(wxColour(31,120,180));
colors.push_back(wxColour(178,223,138));
colors.push_back(wxColour(51,160,44));
colors.push_back(wxColour(251,154,153));
colors.push_back(wxColour(227,26,28));
colors.push_back(wxColour(253,191,111));
colors.push_back(wxColour(255,127,0));
colors.push_back(wxColour(106,61,154));
colors.push_back(wxColour(255,255,153));
colors.push_back(wxColour(177,89,40));
colors.push_back(wxColour(255,255,179));
colors.push_back(wxColour(190,186,218));
colors.push_back(wxColour(251,128,114));
colors.push_back(wxColour(128,177,211));
colors.push_back(wxColour(179,222,105));
colors.push_back(wxColour(252,205,229));
colors.push_back(wxColour(217,217,217));
colors.push_back(wxColour(188,128,189));
colors.push_back(wxColour(204,235,197));
};
void GdaColorUtils::GetLISAColors(std::vector<wxColour>& colors)
{
colors.clear();
colors.push_back(wxColour(240, 240, 240));
colors.push_back(wxColour(255, 0, 0));
colors.push_back(wxColour(0, 0, 255));
colors.push_back(wxColour(150, 150, 255));
colors.push_back(wxColour(255, 150, 150));
}
void GdaColorUtils::GetLISAColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_not_sig);
labels.push_back(GdaConst::gda_lbl_highhigh);
labels.push_back(GdaConst::gda_lbl_lowlow);
labels.push_back(GdaConst::gda_lbl_lowhigh);
labels.push_back(GdaConst::gda_lbl_highlow);
}
void GdaColorUtils::GetLocalGColors(std::vector<wxColour>& colors)
{
colors.clear();
colors.push_back(wxColour(240, 240, 240));
colors.push_back(wxColour(255, 0, 0));
colors.push_back(wxColour(0, 0, 255));
}
void GdaColorUtils::GetLocalGColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_not_sig);
labels.push_back(GdaConst::gda_lbl_highhigh);
labels.push_back(GdaConst::gda_lbl_lowlow);
}
void GdaColorUtils::GetLocalJoinCountColors(std::vector<wxColour>& colors)
{
colors.clear();
colors.push_back(wxColour(240, 240, 240));
colors.push_back(wxColour(255, 0, 0));
}
void GdaColorUtils::GetLocalJoinCountColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_not_sig);
labels.push_back(GdaConst::gda_lbl_highhigh);
}
void GdaColorUtils::GetLocalGearyColors(std::vector<wxColour>& colors)
{
colors.clear();
colors.push_back(wxColour(240, 240, 240));
colors.push_back(wxColour(178,24,43));
colors.push_back(wxColour(239,138,98));
colors.push_back(wxColour(253,219,199));
colors.push_back(wxColour(103,173,199));
}
void GdaColorUtils::GetLocalGearyColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_not_sig);
labels.push_back(GdaConst::gda_lbl_highhigh);
labels.push_back(GdaConst::gda_lbl_lowlow);
labels.push_back(GdaConst::gda_lbl_otherpos);
labels.push_back(GdaConst::gda_lbl_negative);
}
void GdaColorUtils::GetMultiLocalGearyColors(std::vector<wxColour>& colors)
{
colors.clear();
colors.push_back(wxColour(240, 240, 240));
colors.push_back(wxColour(51,110,161));
}
void GdaColorUtils::GetMultiLocalGearyColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_not_sig);
labels.push_back(GdaConst::gda_lbl_positive);
}
void GdaColorUtils::GetPercentileColors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::diverging_color_scheme, 6, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetPercentileColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_1p);
labels.push_back(GdaConst::gda_lbl_1p_10p);
labels.push_back(GdaConst::gda_lbl_10p_50p);
labels.push_back(GdaConst::gda_lbl_50p_90p);
labels.push_back(GdaConst::gda_lbl_90p_99p);
labels.push_back(GdaConst::gda_lbl_99p);
}
void GdaColorUtils::GetBoxmapColors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::diverging_color_scheme, 6, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetBoxmapColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_loweroutlier);
labels.push_back(GdaConst::gda_lbl_25p);
labels.push_back(GdaConst::gda_lbl_25p_50p);
labels.push_back(GdaConst::gda_lbl_50p_75p);
labels.push_back(GdaConst::gda_lbl_75p);
labels.push_back(GdaConst::gda_lbl_upperoutlier);
}
void GdaColorUtils::GetStddevColors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::diverging_color_scheme, 6, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetStddevColorLabels(std::vector<wxString>& labels)
{
labels.clear();
labels.push_back(GdaConst::gda_lbl_n2sigma);
labels.push_back(GdaConst::gda_lbl_n2sigma_n1sigma);
labels.push_back(GdaConst::gda_lbl_n1sigma);
labels.push_back(GdaConst::gda_lbl_1sigma);
labels.push_back(GdaConst::gda_lbl_1sigma_2sigma);
labels.push_back(GdaConst::gda_lbl_2sigma);
}
void GdaColorUtils::GetQuantile2Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 2, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile3Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 3, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile4Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 4, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile5Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 5, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile6Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 6, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile7Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 7, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile8Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 8, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile9Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 9, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
void GdaColorUtils::GetQuantile10Colors(std::vector<wxColour>& colors)
{
colors.clear();
CatClassification::PickColorSet(colors, CatClassification::sequential_color_scheme, 10, false);
colors.insert(colors.begin(), wxColour(240, 240, 240));
}
wxString GdaColorUtils::ToHexColorStr(const wxColour& c)
{
return c.GetAsString(wxC2S_HTML_SYNTAX);
}
wxColour GdaColorUtils::ChangeBrightness(const wxColour& input_col,
int brightness)
{
unsigned char r = input_col.Red();
unsigned char g = input_col.Green();
unsigned char b = input_col.Blue();
unsigned char alpha = input_col.Alpha();
wxColour::ChangeLightness(&r, &g, &b, brightness);
return wxColour(r,g,b,alpha);
}
uint64_t Gda::ThomasWangHashUInt64(uint64_t key) {
key = (~key) + (key << 21); // key = (key << 21) - key - 1;
key = key ^ (key >> 24);
key = (key + (key << 3)) + (key << 8); // key * 265
key = key ^ (key >> 14);
key = (key + (key << 2)) + (key << 4); // key * 21
key = key ^ (key >> 28);
key = key + (key << 31);
return key;
}
double Gda::ThomasWangHashDouble(uint64_t key) {
key = (~key) + (key << 21); // key = (key << 21) - key - 1;
key = key ^ (key >> 24);
key = (key + (key << 3)) + (key << 8); // key * 265
key = key ^ (key >> 14);
key = (key + (key << 2)) + (key << 4); // key * 21
key = key ^ (key >> 28);
key = key + (key << 31);
return 5.42101086242752217E-20 * key;
}
double Gda::ThomasWangDouble(uint64_t& key) {
key = (~key) + (key << 21); // key = (key << 21) - key - 1;
key = key ^ (key >> 24);
key = (key + (key << 3)) + (key << 8); // key * 265
key = key ^ (key >> 14);
key = (key + (key << 2)) + (key << 4); // key * 21
key = key ^ (key >> 28);
key = key + (key << 31);
return 5.42101086242752217E-20 * key;
}
uint64_t Gda::factorial(unsigned int n)
{
uint64_t r = 1;
for(size_t i = n-1; i > 1; i--)
r *= i;
return r;
}
double Gda::combinatorial(unsigned int n, unsigned int k) {
double r = 1;
double s = 1;
size_t i;
int kk = k > n/2 ? k : n-k;
for(i=n; i > kk; i--) r *= i;
for(i=(n-kk); i>0; i--) s *= i;
return r / s;
}
wxString Gda::CreateUUID(int nSize)
{
if (nSize < 0 || nSize >= 38)
nSize = 8;
wxString letters = "abcdefghijklmnopqrstuvwxyz0123456789";
srand ((unsigned int)time(NULL));
wxString uid;
while (uid.length() < nSize) {
int iSecret = rand() % letters.size();
uid += letters[iSecret];
}
return uid;
}
/** Use with std::sort for sorting in ascending order */
bool Gda::dbl_int_pair_cmp_less(const dbl_int_pair_type& ind1,
const dbl_int_pair_type& ind2)
{
return ind1.first < ind2.first;
}
/** Use with std::sort for sorting in descending order */
bool Gda::dbl_int_pair_cmp_greater(const dbl_int_pair_type& ind1,
const dbl_int_pair_type& ind2)
{
return ind1.first > ind2.first;
}
/** Use with std::sort for sorting in ascending order */
bool Gda::dbl_int_pair_cmp_second_less(const dbl_int_pair_type& ind1,
const dbl_int_pair_type& ind2)
{
return ind1.second < ind2.second;
}
/** Use with std::sort for sorting in descending order */
bool Gda::dbl_int_pair_cmp_second_greater(const dbl_int_pair_type& ind1,
const dbl_int_pair_type& ind2)
{
return ind1.second > ind2.second;
}
void
HingeStats::
CalculateHingeStats(const std::vector<Gda::dbl_int_pair_type>& data)
{
num_obs = (int)data.size();
double N = num_obs;
is_even_num_obs = (num_obs % 2) == 0;
min_val = data[0].first;
max_val = data[num_obs-1].first;
Q2_ind = (N+1)/2.0 - 1;
if (is_even_num_obs) {
Q1_ind = (N+2)/4.0 - 1;
Q3_ind = (3*N+2)/4.0 - 1;
} else {
Q1_ind = (N+3)/4.0 - 1;
Q3_ind = (3*N+1)/4.0 - 1;
}
Q1 = (data[(int) floor(Q1_ind)].first +
data[(int) ceil(Q1_ind)].first)/2.0;
Q2 = (data[(int) floor(Q2_ind)].first +
data[(int) ceil(Q2_ind)].first)/2.0;
Q3 = (data[(int) floor(Q3_ind)].first +
data[(int) ceil(Q3_ind)].first)/2.0;
IQR = Q3 - Q1;
extreme_lower_val_15 = Q1 - 1.5*IQR;
extreme_lower_val_30 = Q1 - 3.0*IQR;
extreme_upper_val_15 = Q3 + 1.5*IQR;
extreme_upper_val_30 = Q3 + 3.0*IQR;
min_IQR_ind = -1;
for (int i=0; i<num_obs; i++) {
if (data[i].first < Q1) min_IQR_ind = i;
else break;
}
if (min_IQR_ind < num_obs-1) min_IQR_ind++;
max_IQR_ind = num_obs;
for (int i=num_obs-1; i>=0; i--) {
if (data[i].first > Q3) max_IQR_ind = i;
else break;
}
if (max_IQR_ind > 0) max_IQR_ind--;
}
void
HingeStats::
CalculateHingeStats(const std::vector<Gda::dbl_int_pair_type>& data,
const std::vector<bool>& data_undef)
{
num_obs = (int)data.size();
double N = 0.0;
std::vector<double> data_valid;
bool has_init = false;
for (size_t i =0; i<num_obs; i++) {
int obs_idx = data[i].second;
if (!data_undef[obs_idx]) {
double val = data[i].first;
data_valid.push_back(val); // sorted
if (!has_init) {
min_val = val;
max_val = val;
has_init = true;
}
if (val < min_val)
min_val = val;
if (val > max_val)
max_val = val;
}
}
N = data_valid.size();
is_even_num_obs = (data_valid.size() % 2) == 0;
Q2_ind = (N+1)/2.0 - 1;
if (is_even_num_obs) {
Q1_ind = (N+2)/4.0 - 1;
Q3_ind = (3*N+2)/4.0 - 1;
} else {
Q1_ind = (N+3)/4.0 - 1;
Q3_ind = (3*N+1)/4.0 - 1;
}
if (N == 0 || N < Q3_ind) return;
Q1 = (data_valid[(int) floor(Q1_ind)] + data_valid[(int) ceil(Q1_ind)])/2.0;
Q2 = (data_valid[(int) floor(Q2_ind)] + data_valid[(int) ceil(Q2_ind)])/2.0;
Q3 = (data_valid[(int) floor(Q3_ind)] + data_valid[(int) ceil(Q3_ind)])/2.0;
IQR = Q3 - Q1;
extreme_lower_val_15 = Q1 - 1.5*IQR;
extreme_lower_val_30 = Q1 - 3.0*IQR;
extreme_upper_val_15 = Q3 + 1.5*IQR;
extreme_upper_val_30 = Q3 + 3.0*IQR;
min_IQR_ind = -1;
for (int i=0; i<num_obs; i++) {
if (data[i].first < Q1) {
min_IQR_ind = i;
}
else
break;
}
if (min_IQR_ind < num_obs-1) {
min_IQR_ind++;
}
max_IQR_ind = num_obs;
for (int i=num_obs-1; i>=0; i--) {
if (data[i].first > Q3) {
max_IQR_ind = i;
}
else
break;
}
if (max_IQR_ind > 0)
max_IQR_ind--;
}
// Assume input v is sorted. If not, can sort
// with std::sort(v.begin(), v.end())
// Testing: for v = {15, 20, 35, 40, 50},
// percentile(1, v) = 15, percentile(10, v) = 15, percentile(11) = 15.25
// percentile(50, v) = 35, percentile(89, v) = 49.5,
// percentile(90, v) = 50, percentile(99, v) = 50
double Gda::percentile(double x, const std::vector<double>& v)
{
int N = (int)v.size();
double Nd = (double) N;
double p_0 = (100.0/Nd) * (1.0-0.5);
double p_Nm1 = (100.0/Nd) * (Nd-0.5);
if (v.empty()) return 0;
if (x <= p_0) return v[0];
if (x >= p_Nm1) return v[N-1];
for (int i=1; i<N; i++) {
double p_i = (100.0/Nd) * ((((double) i)+1.0)-0.5);
if (x == p_i) return v[i];
if (x < p_i) {
double p_im1 = (100.0/Nd) * ((((double) i))-0.5);
return v[i-1] + Nd*((x-p_im1)/100.0)*(v[i]-v[i-1]);
}
}
return v[N-1]; // execution should never get here
}
// Same assumptions as above
double Gda::percentile(double x, const Gda::dbl_int_pair_vec_type& v,
const std::vector<bool>& undefs)
{
std::vector<double> valid_data;
for (size_t i = 0; i<v.size(); i++ ) {
double val = v[i].first;
int ind = v[i].second;
if (undefs[ind])
continue;
valid_data.push_back(val);
}
return percentile(x, valid_data);
}
// Same assumptions as above
double Gda::percentile(double x, const Gda::dbl_int_pair_vec_type& v)
{
int N = (int)v.size();
double Nd = (double) N;
double p_0 = (100.0/Nd) * (1.0-0.5);
double p_Nm1 = (100.0/Nd) * (Nd-0.5);
if (x <= p_0)
return v[0].first;
if (x >= p_Nm1)
return v[N-1].first;
for (int i=1; i<N; i++) {
double p_i = (100.0/Nd) * ((((double) i)+1.0)-0.5);
if (x == p_i)
return v[i].first;
if (x < p_i) {
double p_im1 = (100.0/Nd) * ((((double) i))-0.5);
return v[i-1].first + Nd*((x-p_im1)/100.0)*(v[i].first-v[i-1].first);
}
}
return v[N-1].first; // execution should never get here
}
SampleStatistics::SampleStatistics()
: sample_size(0), min(0), max(0), mean(0),
var_with_bessel(0), var_without_bessel(0),
sd_with_bessel(0), sd_without_bessel(0)
{
}
SampleStatistics::SampleStatistics(const std::vector<double>& data)
: sample_size(0), min(0), max(0), mean(0),
var_with_bessel(0), var_without_bessel(0),
sd_with_bessel(0), sd_without_bessel(0)
{
CalculateFromSample(data);
}
SampleStatistics::SampleStatistics(const std::vector<double>& data,
const std::vector<bool>& undefs)
: sample_size(0), min(0), max(0), mean(0),
var_with_bessel(0), var_without_bessel(0),
sd_with_bessel(0), sd_without_bessel(0)
{
std::vector<double> valid_data;
for (int i=0; i<data.size(); i++) {
if (undefs[i] == false)
valid_data.push_back(data[i]);
}
CalculateFromSample(valid_data);
}
SampleStatistics::SampleStatistics(const std::vector<double>& data,
const std::vector<bool>& undefs1,
const std::vector<bool>& undefs2)
: sample_size(0), min(0), max(0), mean(0),
var_with_bessel(0), var_without_bessel(0),
sd_with_bessel(0), sd_without_bessel(0)
{
std::vector<double> valid_data;
for (int i=0; i<data.size(); i++) {
if (undefs1[i] || undefs2[i])
continue;
valid_data.push_back(data[i]);
}
CalculateFromSample(valid_data);
}
void SampleStatistics::CalculateFromSample(const std::vector<double>& data,
const std::vector<bool>& undefs)
{
std::vector<double> valid_data;
for (int i=0; i<data.size(); i++) {
if (undefs[i] == false)
valid_data.push_back(data[i]);
}
CalculateFromSample(valid_data);
}
void SampleStatistics::CalculateFromSample(const std::vector<double>& data)
{
sample_size = (int)data.size();
if (sample_size == 0) return;
CalcMinMax(data, min, max);
mean = CalcMean(data);
double n = sample_size;
double sum_squares = 0;
for (int i=0; i<data.size(); i++) {
sum_squares += data[i] * data[i];
}
var_without_bessel = (sum_squares/n) - (mean*mean);
sd_without_bessel = sqrt(var_without_bessel);
if (sample_size == 1) {
var_with_bessel = var_without_bessel;
sd_with_bessel = sd_without_bessel;
} else {
var_with_bessel = (n/(n-1)) * var_without_bessel;
sd_with_bessel = sqrt(var_with_bessel);
}
}
/** We assume that the data has been sorted in ascending order */
void
SampleStatistics::
CalculateFromSample(const std::vector<Gda::dbl_int_pair_type>& data_,
const std::vector<bool>& undefs)
{
std::vector<double> data;
for (int i=0; i<data_.size(); i++) {
int id = data_[i].second;
if (!undefs[id]) {
data.push_back(data_[i].first);
}
}
sample_size = (int)data.size();
if (sample_size == 0) return;
min = data[0];
max = data[sample_size-1];
mean = CalcMean(data);
double n = sample_size;
double sum_squares = 0;
for (int i=0; i<data.size(); i++) {
sum_squares += data[i] * data[i];
}
var_without_bessel = (sum_squares/n) - (mean*mean);
sd_without_bessel = sqrt(var_without_bessel);
if (sample_size == 1) {
var_with_bessel = var_without_bessel;
sd_with_bessel = sd_without_bessel;
} else {
var_with_bessel = (n/(n-1)) * var_without_bessel;
sd_with_bessel = sqrt(var_with_bessel);
}
}
std::string SampleStatistics::ToString()
{
std::ostringstream ss;
ss << "sample_size = " << sample_size << std::endl;
ss << "min = " << min << std::endl;
ss << "max = " << max << std::endl;
ss << "mean = " << mean << std::endl;
ss << "var_with_bessel = " << var_with_bessel << std::endl;
ss << "var_without_bessel = " << var_without_bessel << std::endl;
ss << "sd_with_bessel = " << sd_with_bessel << std::endl;
ss << "sd_without_bessel = " << sd_without_bessel << std::endl;
return ss.str();
}
double SampleStatistics::CalcMin(const std::vector<double>& data)
{
double min = std::numeric_limits<double>::max();
for (int i=0; i<data.size(); i++) {
if ( data[i] < min ) min = data[i];
}
return min;
}
double SampleStatistics::CalcMax(const std::vector<double>& data)
{
double max = -std::numeric_limits<double>::max();
for (int i=0; i<data.size(); i++) {
if ( data[i] > max ) max = data[i];
}
return max;
}
void SampleStatistics::CalcMinMax(const std::vector<double>& data,
double& min, double& max)
{
if (data.size() == 0) return;
min = data[0];
max = data[0];
for (int i=1; i<data.size(); i++) {
if ( data[i] < min ) {
min = data[i];
} else if ( data[i] > max ) {
max = data[i];
}
}
}
double SampleStatistics::CalcMean(const std::vector<double>& data)
{
if (data.size() == 0) return 0;
double total = 0;
for (int i=0; i<data.size(); i++) {
total += data[i];
}
return total / (double) data.size();
}
double SampleStatistics::CalcMean(const std::vector<Gda::dbl_int_pair_type>& data)
{
if (data.size() == 0) return 0;
double total = 0;
for (int i=0; i<data.size(); i++) {
total += data[i].first;
}
return total / (double) data.size();
}
SimpleLinearRegression::SimpleLinearRegression(const std::vector<double>& X,
const std::vector<double>& Y,
double meanX, double meanY,
double varX, double varY)
: n(0), covariance(0), correlation(0), alpha(0), beta(0), r_squared(0),
std_err_of_estimate(0), std_err_of_beta(0), std_err_of_alpha(0),
t_score_alpha(0), t_score_beta(0), p_value_alpha(0), p_value_beta(0),
valid(false), valid_correlation(false), valid_std_err(false),
error_sum_squares(0)
{
CalculateRegression(X, Y, meanX, meanY, varX, varY);
}
SimpleLinearRegression::SimpleLinearRegression(const std::vector<double>& X,
const std::vector<double>& Y,
const std::vector<bool>& X_undef,
const std::vector<bool>& Y_undef,
double meanX, double meanY,
double varX, double varY)
: n(0), covariance(0), correlation(0), alpha(0), beta(0), r_squared(0),
std_err_of_estimate(0), std_err_of_beta(0), std_err_of_alpha(0),
t_score_alpha(0), t_score_beta(0), p_value_alpha(0), p_value_beta(0),
valid(false), valid_correlation(false), valid_std_err(false),
error_sum_squares(0)
{