-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLSDMostLikelyPartitionsFinder.cpp
More file actions
1568 lines (1326 loc) · 52.4 KB
/
Copy pathLSDMostLikelyPartitionsFinder.cpp
File metadata and controls
1568 lines (1326 loc) · 52.4 KB
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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDMostLikeleyPartitionsFinder
// Land Surface Dynamics MostLikeleyPartitionsFinder
//
// An object for extracting segments from x,y data
// developed for the University of Edinburgh
// Land Surface Dynamics group topographic toolbox.
//
// This object is mainly used in the analysis of channel profiles
// transformed using the integral method of channel analysis.
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
//
// Copyright (C) 2013 Simon M. Mudd 2013
//
// Developer can be contacted by simon.m.mudd _at_ ed.ac.uk
//
// Simon Mudd
// University of Edinburgh
// School of GeoSciences
// Drummond Street
// Edinburgh, EH8 9XP
// Scotland
// United Kingdom
//
// This program 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 2 of the License, or (at your option) any later version.
//
// This program 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, write to:
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301
// USA
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDMostLikelyPartitionsFinder.cpp
// source code for the LSDMostLikelyPartitionsFinder object
// this object looks for the most likeley partitions or segments
// of 2D data, and is principally used to identify segments of
// differing channel steepness in chi-zeta space
// LSD stands for Land Surface Dynamics
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
// David Milodowski, University of Edinburgh
// Martin D. Hurst, British Geological Survey
//
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Version 1.0 03/01/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// TODO:
// 1. Peel out the partitioning so that you can save and load partition files.
// 3. p, t and ANCOVA test to see if regressions are significant and different
#include <vector>
#include <iostream>
#include <algorithm>
#include <time.h>
#include "TNT/tnt.h"
#include "LSDStatsTools.hpp"
#include "LSDMostLikelyPartitionsFinder.hpp"
using namespace std;
using namespace TNT;
#ifndef LSDMostLikelyPartitionsFinder_CPP
#define LSDMostLikelyPartitionsFinder_CPP
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Create function, makes an partitions finder object with some x and y data.
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::create(int this_min_seg_length, vector<float> this_x_data, vector<float> this_y_data)
{
minimum_segment_length = this_min_seg_length;
x_data = this_x_data;
y_data = this_y_data;
base_sigma = 100.0; // this is arbitrary
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function resets all the derived data members.
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::reset_derived_data_members()
{
Array2D<float> empty_array;
like_array =empty_array;
m_array =empty_array;
b_array = empty_array;
rsquared_array = empty_array;
DW_array = empty_array;
vector<float> empty_dv;
MLE_of_segments = empty_dv;
vector< vector<int> > vv_int;
segments_for_each_n_segments = vv_int;
vector< vector < vector<int> > > vvvi;
partitions = vvvi;
vector<int> empty_vec;
best_fit_AIC = empty_vec;
best_fit_AICc = empty_vec;
vector< vector<float> > empty_vecvec;
AIC_for_each_n_segments= empty_vecvec;
AICc_for_each_n_segments = empty_vecvec;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Various algorithms are implemented for thinning the data.
// Several options are available.
//
// this one collects data as close as possible to some target dx, but does
// not modify invidivual data points.
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::thin_data_target_dx_preserve_data(float dx)
{
vector<float> thinned_x;
vector<float> thinned_y;
//cout << "x_data size: " << x_data.size() << endl;
thinned_x.push_back(x_data[0]);
thinned_y.push_back(y_data[0]);
float start_x = x_data[0];
float next_x = start_x+dx;
int n_nodes = x_data.size();
int last_picked = 0;
for (int i = 1; i<n_nodes; i++)
{
//cout << "next x is: " << next_x << " and x is: " << x_data[i] << endl;
if(x_data[i] >=next_x)
{
thinned_x.push_back(x_data[i]);
thinned_y.push_back(y_data[i]);
next_x += dx;
last_picked = i;
}
}
// make sure the last data element is included
if (last_picked != n_nodes-1)
{
thinned_x.push_back(x_data[n_nodes-1]);
thinned_y.push_back(y_data[n_nodes-1]);
}
x_data = thinned_x;
y_data = thinned_y;
reset_derived_data_members();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// same as above but returns an index vector into the data points that were selected
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::thin_data_target_dx_preserve_data(float dx, vector<int>& node_ref)
{
vector<float> thinned_x;
vector<float> thinned_y;
vector<int> node_reference;
//cout << "x_data size: " << x_data.size() << endl;
thinned_x.push_back(x_data[0]);
thinned_y.push_back(y_data[0]);
node_reference.push_back(0);
float start_x = x_data[0];
float next_x = start_x+dx;
int n_nodes = x_data.size();
int last_picked = 0;
for (int i = 1; i<n_nodes; i++)
{
//cout << "next x is: " << next_x << " and x is: " << x_data[i] << endl;
if(x_data[i] >=next_x)
{
thinned_x.push_back(x_data[i]);
thinned_y.push_back(y_data[i]);
node_reference.push_back(i);
next_x += dx;
last_picked = i;
}
}
// make sure the last data element is included
if (last_picked != n_nodes-1)
{
thinned_x.push_back(x_data[n_nodes-1]);
thinned_y.push_back(y_data[n_nodes-1]);
node_reference.push_back(n_nodes-1);
}
x_data = thinned_x;
y_data = thinned_y;
reset_derived_data_members();
node_ref = node_reference;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// same as above but spawns a new object
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LSDMostLikelyPartitionsFinder LSDMostLikelyPartitionsFinder::spawn_thinned_data_target_dx_preserve_data(float dx)
{
vector<float> thinned_x;
vector<float> thinned_y;
thinned_x.push_back(x_data[0]);
thinned_y.push_back(y_data[0]);
float start_x = x_data[0];
float next_x = start_x+dx;
int n_nodes = x_data.size();
int last_picked = 0;
for (int i = 1; i<n_nodes; i++)
{
if(x_data[i] >=next_x)
{
thinned_x.push_back(x_data[i]);
thinned_y.push_back(y_data[i]);
next_x += dx;
last_picked = i;
}
}
// make sure the last data element is included
if (last_picked != n_nodes-1)
{
thinned_x.push_back(x_data[n_nodes-1]);
thinned_y.push_back(y_data[n_nodes-1]);
}
LSDMostLikelyPartitionsFinder new_object(minimum_segment_length,thinned_x,thinned_y);
return new_object;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this thins the data by skipping elements.
// a positive number N means it skips N elements after each element
// a negative number -N means that after N elements it skips one element
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::thin_data_skip(int N, vector<int>& node_ref)
{
vector<float> thinned_x;
vector<float> thinned_y;
vector<int> node_reference;
int n_nodes = x_data.size();
int node = 0;
int n_counted = 0;
if (N == 0)
{
thinned_x = x_data;
thinned_y = y_data;
for (int i = 0; i<n_nodes; i++)
{
node_reference.push_back(i);
}
}
else
{
while (node < n_nodes)
{
if (N < 0)
{
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
n_counted++;
if (n_counted == -N) // this triggers the skip
{
node+=2;
n_counted = 0;
}
else
{
node++;
}
}
else // this is for skipping after each node
{
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
node+=N+1;
}
}
}
// ensure the final daat element is selected
if (node != n_nodes-1)
{
node = n_nodes-1;
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
}
x_data = thinned_x;
y_data = thinned_y;
reset_derived_data_members();
node_ref = node_reference;
//cout << "Did the skipping (LSDMLPF line 280), n_nodes: " << n_nodes
// << " after skip: " << x_data.size() << endl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this one recasts data at fixed values of dx, using linear interpoloation
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::thin_data_target_dx_linear_interpolation(float dx)
{
vector<float> thinned_x;
vector<float> thinned_y;
thinned_x.push_back(x_data[0]);
thinned_y.push_back(y_data[0]);
float start_x = x_data[0];
float next_x = start_x+dx;
float data_slope, next_y;
int n_nodes = x_data.size();
for (int i = 1; i<n_nodes; i++)
{
if(x_data[i] >=next_x)
{
thinned_x.push_back(next_x);
data_slope = (y_data[i]-y_data[i-1])/(x_data[i]-x_data[i-1]);
next_y = data_slope*(next_x-x_data[i-1])+y_data[i-1];
thinned_y.push_back(next_y );
next_x += dx;
}
}
x_data = thinned_x;
y_data = thinned_y;
reset_derived_data_members();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// same as above but spawns a new object.
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LSDMostLikelyPartitionsFinder LSDMostLikelyPartitionsFinder::spawn_thinned_data_target_dx_linear_interpolation(float dx)
{
vector<float> thinned_x;
vector<float> thinned_y;
thinned_x.push_back(x_data[0]);
thinned_y.push_back(y_data[0]);
float start_x = x_data[0];
float next_x = start_x+dx;
float data_slope, next_y;
int n_nodes = x_data.size();
for (int i = 1; i<n_nodes; i++)
{
if(x_data[i] >=next_x)
{
thinned_x.push_back(next_x);
data_slope = (y_data[i]-y_data[i-1])/(x_data[i]-x_data[i-1]);
next_y = data_slope*(next_x-x_data[i-1])+y_data[i-1];
thinned_y.push_back(next_y );
next_x += dx;
}
}
LSDMostLikelyPartitionsFinder new_object(minimum_segment_length,thinned_x, thinned_y);
return new_object;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this skips nodes but using a Monte Carlo scheme that samples random points along the channel profile
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::thin_data_monte_carlo_skip(int Mean_skip,int skip_range, vector<int>& node_ref)
{
int minimum_skip = Mean_skip - 0.5*skip_range;
long seed = time(NULL);
int N = int((float(skip_range))*(ran3(&seed))+0.5)+minimum_skip;
vector<float> thinned_x;
vector<float> thinned_y;
vector<int> node_reference;
int n_nodes = x_data.size();
int node = 0; // the node along the channel that will be used for the next data element
int n_counted = 0;
int new_N_switch; // this is used if N is negative and multiple nodes are selelcted in a row
// if N is greater than zero, new_N_switch is true (== 1)
if(N >= 0)
{
new_N_switch = 1;
}
else
{
new_N_switch = 0;
}
int last_node = 0; // the last node used
while (node < n_nodes)
{
last_node = node;
if (N == 0)
{
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
node++;
}
else if (N < 0)
{
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
n_counted++;
if (n_counted == -N) // this triggers the skip
{
node+=2;
n_counted = 0;
new_N_switch = 1; // once the skip is triggered, you will need a new N
}
else
{
node++;
}
}
else // this is for skipping after each node
{
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
node+=N+1;
}
if (new_N_switch == 1)
{
float random_N = ran3(&seed);
float skippy = (float(skip_range));
N = int(skippy*(random_N)+0.5)+minimum_skip;
//cout << "N is: " << N << " and random: " << random_N << " and skppy: " << skippy
// << " and skip_range: " << skip_range << " and minimum_skip: " << minimum_skip << endl;
if(N >= 0)
{
new_N_switch = 1;
}
else
{
new_N_switch = 0;
}
}
}
//cout << "node: " << node << " and n_nodes: " << n_nodes << endl;
// ensure the final data element is selected
if (last_node != n_nodes-1)
{
//cout << "yo, node before = " << node;
node = n_nodes-1;
//cout << " and after: " << node << endl;
thinned_x.push_back(x_data[node]);
thinned_y.push_back(y_data[node]);
node_reference.push_back(node);
}
x_data = thinned_x;
y_data = thinned_y;
reset_derived_data_members();
node_ref = node_reference;
//cout << "Did the skipping (LSDMLPF line 463), n_nodes: " << n_nodes
// << " after skip: " << x_data.size() << endl;
//for (int i = 0; i< int(node_ref.size()); i++)
//{
// cout << "node ref["<<i<< "]: " << node_ref[i] << endl;
//}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Thins object based on a monte carlo approach using a mean, max and minimum dchi
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::thin_data_monte_carlo_dchi(float mean_dchi, float variation_dchi, vector<int>& node_ref)
{
//cout << "LSDMostLikelyPartitionsFinder, LINE 391, mean dchi: " << mean_dchi << endl;
vector<float> thinned_x;
vector<float> thinned_y;
vector<int> node_reference;
// set the max standard deviation to mean dchi so that you don't get
// negative dchi values
if (variation_dchi > mean_dchi)
{
variation_dchi = mean_dchi;
}
float min_dchi = mean_dchi-variation_dchi;
float range_chi = 2*variation_dchi;
long seed = time(NULL);
// get dx using a random seed
float dx = ran3(&seed)*range_chi+min_dchi;
thinned_x.push_back(x_data[0]);
thinned_y.push_back(y_data[0]);
node_reference.push_back(0);
float start_x = x_data[0];
float next_x = start_x+dx;
int n_nodes = x_data.size();
int last_picked = 0;
for (int i = 1; i<n_nodes; i++)
{
//cout << "next x is: " << next_x << " and x is: " << x_data[i] << endl;
if(x_data[i] >=next_x)
{
thinned_x.push_back(x_data[i]);
thinned_y.push_back(y_data[i]);
node_reference.push_back(i);
dx = ran3(&seed)*range_chi+min_dchi;
next_x += dx;
last_picked = i;
}
}
// make sure the last data element is included
if (last_picked != n_nodes-1)
{
thinned_x.push_back(x_data[n_nodes-1]);
thinned_y.push_back(y_data[n_nodes-1]);
node_reference.push_back(n_nodes-1);
}
x_data = thinned_x;
y_data = thinned_y;
reset_derived_data_members();
node_ref = node_reference;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Prints the x and y data to screen (for bug checking)
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::print_x_y_data_to_screen()
{
int n_nodes = x_data.size();
for (int i = 0; i<n_nodes; i++)
{
cout << "i: " << i << " \t x: " << x_data[i] << " \t y: " << y_data[i] << endl;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This is a wrapper function to find the likelihood of all the segments
// you can enter a vector of sigmas such that each node has a different sigma value
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::best_fit_driver_AIC_for_linear_segments(vector<float> sigma_values)
{
//cout << "best_fit_driver_AIC_for_linear_segments, getting like data" <<endl;
calculate_segment_matrices(base_sigma);
//cout << "best_fit_driver_AIC_for_linear_segments, got like data" <<endl;
// get the maximum liklihood of segments
find_max_like_of_segments();
get_n_segments_for_various_sigma(sigma_values);
//print_AIC_and_AICc_to_screen(sigma_values);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This is a wrapper function to find the likelihood of all the segments
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::best_fit_driver_AIC_for_linear_segments(float sigma)
{
//cout << "best_fit_driver_AIC_for_linear_segments, getting like data" <<endl;
calculate_segment_matrices(base_sigma);
//cout << "best_fit_driver_AIC_for_linear_segments, got like data" <<endl;
// get the maximum liklihood of segments
find_max_like_of_segments();
vector<float> sigma_values;
sigma_values.push_back(sigma);
get_n_segments_for_various_sigma(sigma_values);
//print_AIC_and_AICc_to_screen(sigma_values);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function returns data for a given sigma value
// the 'node' int is the index into the sigma vector
// this function replaces a large number of data elements.
// the vectors b, m, r^2 and DW are from the regressions of each segment
// the fit y is a vector with data in the x positions that is derived from
// the best fit lines
// the seg lengths are the individual segment lengths,
// this_MLE, this_n_segments and this_n_nodes are all returned
// so the user can combine two or more segments and get an AIC or AICc
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::get_data_from_best_fit_lines(int node, vector<float> sigma_values,
vector<float>& b_values, vector<float>& m_values,
vector<float>& r2_values, vector<float>& DW_values,
vector<float>& fitted_y,vector<int>& seg_lengths,
float& this_MLE, int& this_n_segments, int& this_n_nodes,
float& this_AIC, float& this_AICc)
{
//cout <<"LSDMLPF line 613, getting data" << endl;
int n_sigma_values = best_fit_AICc.size();
if (node >= n_sigma_values)
{
cout << "LSDMostLikelyPartitionsFinder::get_data_from_best_fit_lines " << endl
<< "you have not calculated AIC with this many nodes" << endl;
}
// get the m, b, etc from the data
int n_sigma_for_printing = node;
int bestfit_segments_node = best_fit_AICc[n_sigma_for_printing];
//cout << "LINE 627 getting properties, bf segments node: " << bestfit_segments_node << endl;
get_properties_of_best_fit_segments(bestfit_segments_node,
m_values,b_values, r2_values,DW_values);
//cout << "LINE 631 got properties" << endl;
vector<int> segment_length;
segment_length = segments_for_each_n_segments[ best_fit_AICc[n_sigma_for_printing] ];
vector<float> new_sig_MLE = transform_like_from_sigma1_to_sigma2(base_sigma,
MLE_of_segments, sigma_values[n_sigma_for_printing]);
vector<float> AICc_values = AICc_for_each_n_segments[node];
float AICc_value = AICc_values[ best_fit_AICc[n_sigma_for_printing] ];
// now print this data
//cout << endl << endl << endl << "The data from the best fit: " << endl;
//cout << "sigma is: " << sigma_values[n_sigma_for_printing]
// << " MLE: " << new_sig_MLE [ best_fit_AICc[n_sigma_for_printing] ]
// << " and the number of segments is: " << best_fit_AICc[n_sigma_for_printing]+1
// << " AICc: " << AICc_value << endl;
//for (int i = 0; i< best_fit_AICc[n_sigma_for_printing]+1; i++)
//{
// cout << "seg_length: " << segment_length[i] << " " << m_values[i] << " " << b_values[i] << " "
// << r2_values[i] << " " << DW_values[i] << endl;
//}
// create a vector of y values from the best fit segments
int n_nodes = x_data.size();
vector<float> fit_y(n_nodes);
int this_node = 0;
for (int seg = 0; seg< best_fit_AICc[n_sigma_for_printing]+1; seg++)
{
for(int seg_node =0; seg_node< segment_length[seg]; seg_node++)
{
fit_y[this_node] = m_values[seg]*x_data[this_node]+ b_values[seg];
this_node++;
}
}
// now replace data for the purposes of getting it to the calling function
fitted_y = fit_y;
seg_lengths = segment_length;
this_MLE = new_sig_MLE [ best_fit_AICc[n_sigma_for_printing] ];
this_n_segments = best_fit_AICc[n_sigma_for_printing]+1;
this_n_nodes = n_nodes;
this_AICc = AICc_value;
vector<float> AIC_values = AIC_for_each_n_segments[node];
this_AIC = AICc_values[ best_fit_AICc[n_sigma_for_printing] ];
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this replaces two vectors, which have the starting and ending position of the best fit segments
// for a given sigma. This gets data from (most likeley) the get_data_from_best_fit_lines function
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::get_start_and_end_x_for_segments(vector<float>& start_x,
vector<float>& end_x, vector<int> seg_lengths)
{
vector<float> empty_vec;
start_x = empty_vec;
end_x = empty_vec;
int n_segs = seg_lengths.size();
start_x.push_back(x_data[0]);
end_x.push_back(seg_lengths[0]-1);
for (int i = 1; i<n_segs; i++)
{
start_x.push_back( x_data[ seg_lengths[i-1] ]);
end_x.push_back( x_data[ seg_lengths[i]-1 ] );
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function is used to calculate the slope, intercept, and likelihood of
// all possible linear segments along a series of data points.
// the function requires the data in x and y vectors, the maximum segment length
// and sigma, the standard deviation of the measured data. This will be approxamately
// the error in the surface elevation, although it might have to be increased simply because
// likelihood will tend to zero if this is too small. sigma should also be considered to
// contain the 'noise' inherent in channel incision so perhaps 1-5 metres is appropriate
// the maximum segment length is an integer: it is the number of data points used.
// these data points from raw chi data are irregularly spaced so two segments of the same
// 'length' can have different lengths in chi space. One remedey for this is a preprocessor that
// places the zeta vs chi data along evenly spaced points.
//
// The routine generates three matrices. The row of the matrix is the starting node of the segment.
// The column of the matrix is the ending node of the segment. Thus the routine will generate a
// matrix that is dimension n x n where n is the number of data points.
//
// One potential future development is to implement this using a sparse matrix from the boost mtl
// library to reduce the memory usage.
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::calculate_segment_matrices(float sigma)
{
int n_data_points = x_data.size();
if (minimum_segment_length>n_data_points)
{
//cout << "LSDMostLikelyPartitionsFinder::calculate_segment_matrices: your segment length is greater than half the number of data points" << endl;
//cout << "This means that there can only be overlapping segments. Changing segment length to maximum segment length "<< endl;
minimum_segment_length = n_data_points;
}
// set up the arrays
// in the future I might consider using sparse arrays but for now we'll just populate
// the empty spots with placeholders
float no_data_value = -9999;
Array2D<float> temp_array(n_data_points,n_data_points,no_data_value);
like_array = temp_array.copy();
m_array = temp_array.copy();
b_array = temp_array.copy();
rsquared_array = temp_array.copy();
DW_array = temp_array.copy();
int start_node = 0;
int end_node = n_data_points-1;
// populate the matrix.
// the get segment row function is recursive so it moves down through all the possible
// starting nodes
//cout << "LINE 518, sigma is: " << sigma << endl;
populate_segment_matrix(start_node, end_node, no_data_value, sigma);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This function populates the matrices of liklihood, m and b values
// it is a recursive algorithm so in fact it doesn't just get one row
// but drills down through all the possible starting nodes to complete the
// matrix
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::populate_segment_matrix(int start_node, int end_node, float no_data_value,float sigma)
{
if (like_array[start_node][end_node] == no_data_value)
{
// create the two segments
vector<float> segment_x;
vector<float> segment_y;
vector<float> residuals;
vector<float> regression_results;
float this_MLE;
// now create iterators to deal with these segments
vector<float>::iterator vec_iter_start;
vector<float>::iterator vec_iter_end;
// the first step is to get the segment starting on the
// first node and ending on the last node
// find out how many nodes are in the segment
int n_nodes_in_segment = end_node - start_node+1;
// resize the vectors accordingly
segment_x.resize(n_nodes_in_segment);
segment_y.resize(n_nodes_in_segment);
// get the iterators for the start and end of the vectors
vec_iter_start = x_data.begin()+start_node;
vec_iter_end = vec_iter_start+n_nodes_in_segment;
segment_x.assign(vec_iter_start,vec_iter_end);
vec_iter_start = y_data.begin()+start_node;
vec_iter_end = vec_iter_start+n_nodes_in_segment;
segment_y.assign(vec_iter_start,vec_iter_end);
// do the least squares regression on this segment
// cout << "LINE 568, sigma is: " << sigma << endl;
regression_results = simple_linear_regression(segment_x, segment_y, residuals);
this_MLE = calculate_MLE_from_residuals( residuals, sigma);
//cout << "LINE 584 doing start: " << start_node << " end: " << end_node << endl;
like_array[start_node][end_node] = this_MLE;
m_array[start_node][end_node] = regression_results[0];
b_array[start_node][end_node] = regression_results[1];
rsquared_array[start_node][end_node] = regression_results[2];
DW_array[start_node][end_node] = regression_results[3];
// now loop through all the end nodes that are allowed that are not the final node.
// that is the first end node is first plus the maximum length -1 , and then
// the final end node before the end of the data is the last node minus the
// maximum length of the segment
for (int loop_end = start_node+minimum_segment_length-1; loop_end< end_node-minimum_segment_length+1; loop_end++)
{
if (like_array[start_node][loop_end] == no_data_value)
{
// get this segment and resize the vectors
n_nodes_in_segment = loop_end - start_node+1;
segment_x.resize(n_nodes_in_segment);
segment_y.resize(n_nodes_in_segment);
// get the iterators for the start and end of the vectors
vec_iter_start = x_data.begin()+start_node;
vec_iter_end = vec_iter_start+n_nodes_in_segment;
segment_x.assign(vec_iter_start,vec_iter_end);
vec_iter_start = y_data.begin()+start_node;
vec_iter_end = vec_iter_start+n_nodes_in_segment;
segment_y.assign(vec_iter_start,vec_iter_end);
// do the least squares regression on this segment
regression_results = simple_linear_regression(segment_x, segment_y, residuals);
this_MLE = calculate_MLE_from_residuals( residuals, sigma);
// fill in the matrices
like_array[start_node][loop_end] = this_MLE;
m_array[start_node][loop_end] = regression_results[0];
b_array[start_node][loop_end] = regression_results[1];
rsquared_array[start_node][loop_end] = regression_results[2];
DW_array[start_node][loop_end] = regression_results[3];
//cout << "LINE 612 doing start: " << start_node << " end: " << loop_end << endl;
// now get the row from the next segment
populate_segment_matrix(loop_end+1, end_node, no_data_value,sigma);
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function calculates the most likeley combination of segments given the liklihood
// of individual segments calculated by the calculate_segment_matrices function
//
// SMM 01/02/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDMostLikelyPartitionsFinder::find_max_like_of_segments()
{
// first get the number of nodes
int n_data_points = like_array.dim1();