-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhaseLen76.dpr
1394 lines (1248 loc) · 48.8 KB
/
PhaseLen76.dpr
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
program PhaseLen76;
{$Q+}
{$APPTYPE CONSOLE}
{%File 'ModelSupport\default.txvpck'}
uses
SysUtils,
KWKStdXE;
{$R+}
{note this only works for the range of dates in Const from ideal_origin}
{to ideal_limit within the overall limits of dates of the range of integers}
const version='7.6'; {7.5, 7.6}
{enable use not doing the Monte Carlo Analysis}
{Make sampling of SD's w/o replacement, calculate K properly}
{7.2 adjust KS output, add CSV input EOF bug}
{7.1 weighted mean of intercepts for calibrated interval, also Ihat}
{7.0 increase KS, trials limits added 1ntcal13.14c 2020)
{5.1 Windows, debugging, IQR}
{5.0 Calibrated Dates}
years='1996-2020';
{ to change any of theses values recompile the program }
ideal_origin=-50000;{-10000}{earliest date considered by the KS method}
ideal_limit=2500; {3000} {latest date considered by the KS method}
calend=1; {subscript for calib}
c14=2;
c14sd=3; {these values are taken in but not used}
calib_year_array_earliest=-50000; {lower bound of calib_year considered}
maxidealtrial=100000; {to create ideal distribution for model}
maxtrial=1000000; {20000} {number of date samples drawn}
maxinterval=200; {number of phase intervals considered}
maxdate=2500; {must accommodate # of empiciacl or test sample
dates and number of intercepts of the dates}
maxtest=5000; {maximum number of test runs in test mode}
max_calib_dataset_lines=6000; {works for intCal13}
rounddates=5; {round all output dates to the nearest...years}
type {sample_array=array[1..maxtrial] of real;} {1..maxtrial}
date_array=array[1..maxdate] of integer;
int_array_type=array[1..1] of integer;
int_array_pntr=^int_array_type;
pathtype=ansistring;
var date, date_std, date_intercept, date_index, date_count, null_array, calendar_date:
date_array;
ideal: int_array_pntr;
rslt_span,rslt_len_c,rslt_iqr_c {,rslt_maxdif_c}: array[0..maxinterval] of integer;
test_array: array [0..maxtest,1..6] of integer;
rslt_maxdif, rslt_maxdif_mean, rslt_dst, rslt_dst_s,
rslt_len, rslt_len_p, rslt_len_s, rslt_iqr, rslt_iqr_p, rslt_iqr_s,
rslt_maxdif_s{, rslt_maxdif_p}: array[0..maxinterval] of real;
calib: array[0..max_calib_dataset_lines,calend..c14SD] of integer;
calib_year: int_array_pntr;
firstdate, lastdate, daterange, mean_date, ndate, median_date,
{maxempiricalstd,} date_iqr, cdate_iqr, orig_randseed, inc_width,
cfirstdate,clastdate,cdaterange,cmean_date,cmedian_date, nintercept,
calib_entries, calib_earliest, calib_latest, calib_origin,
ninterval,nidealtrials, ntrials, maxspan, middle, usermiddle,
minspan, testspan, ntrialadj, ideal_date,
generate_start, generate_finish, generate_length,generate_middle,generate_std,
sd_loop_start, sd_loop_inc, sd_loop_stop, this_loop, all_loops,
ndate_loop_start, ndate_loop_inc, ndate_loop_stop,
e_ihat, test_no, n_tests, ihat_warning: integer;
start_time,time_now,last_time,elapsed_time,lbound,ubound,{sample_date,}
ihatk: real;
calibin, filename, filein: ansistring;
dskin,dskout:text;
median,mode,model: ansichar;
warn_neg, euclid, big_loop, calibrated_dates, MonteCarlo: boolean;
{************************************}
{** General purpose utilities **}
{************************************}
function max(v1,v2: integer): integer;
begin
if v1<v2 then max:=v2 else max:=v1;
end;
function min(v1,v2: integer): integer;
begin
if v1<v2 then min:=v1 else min:=v2;
end;
function roundto(v,r: integer): integer;
{Round i to the nearest r}
var i: integer;
begin
i:=(v mod r); {remainder}
if i>=((r+1) div 2) then v:=v+(r-i) else v:=v-i;
roundto:=v;
end;
{Get a truncated normal value between low and up expressed as SD}
function tnormal(low,up: real): real;
var val: real;
begin
val:=-1.0e99;
while (val<low) or (val>up) do val:=normal;
tnormal:=val;
end;
{Sorts a Vector of Dates in Order}
procedure sortdate(n: integer; var vector, col2: date_array);
var h,i,j,s,k,k1,p: integer; stop: boolean;
begin { quicksort see knuth Vol 3 }
{Calculate the quicksort partition size}
p:=1;
while (twotothe(p+1)-1)<(n div 3) do inc(p);
for s:=p downto 1 do begin
h:=twotothe(s-1);
for j:=h+1 to n do begin
stop:=false; i:=j-h; k:=vector[j]; k1:=col2[j];
while (i>0) and not stop do
if k<vector[i] then begin
vector[i+h]:=vector[i];
col2[i+h]:=col2[i];
i:=i-h;
end
else stop:=true;
vector[i+h]:=k;
col2[i+h]:=k1;
end;
end;
end;
procedure randomize_order(n: integer; var values: date_array);
var i: integer;
vector, indx, tvalues: date_array;
begin
for i:=1 to n do begin
indx[i]:=i;
vector[i]:=random(100000);
tvalues[i]:=values[i];
end;
sortdate(n,vector,indx);
for i:=1 to n do values[i]:=tvalues[indx[i]];
end;
{read a set of input dates for evaluation}
procedure readdates;
var cnt,i,j,repetition,nval: integer;
keyentry: boolean;
begin
firstdate:=maxint; lastdate:=0-maxint; warn_neg:=false;
filein:='.ADF';
readfile('File with Empirical Dates (Con for Keyboard)',dskin,filein);
keyentry:=(filein='CON');
if not keyentry then begin
ndate:=getinteger(dskin);
nval:=getinteger(dskin);
if (nval<2) or (nval>3) then begin
writeln('Input Data must have 2 {or 3} values per line, mean, std, {count}');
halt;
end;
cnt:=0;
for i:=1 to ndate do begin
inc(cnt);
date[cnt]:=round(getreal(dskin));
if date[cnt]<0 then warn_neg:=true;
date_std[cnt]:=round(getreal(dskin));
if nval=3 then begin {third value is a count}
repetition:=getinteger(dskin);
for j:=2 to repetition do begin
inc(cnt); date[cnt]:=date[cnt-1];
date_std[cnt]:=date_std[cnt-1];
end;
end;
end;
ndate:=cnt;
close(dskin);
end
else {if keyentry then} begin
ndate:=readint('Number of Actual Dates',1,maxdate,'');
for i:=1 to ndate do begin
date[i]:=round(readreal(' Date Mean',-15000,15000,''));
if date[i]<0 then warn_neg:=true;
date_std[i]:=round(readreal(' Date Std ',0,10000,''));
end;
end;
sortdate(ndate,date,date_std);
end;
function midspread(n: integer; var datelist: date_array): integer;
{find the midspread of a sorted list}
var depth: real; d: integer;
begin
depth:=(floor((n+1)/2)+1)/2;
d:=floor(depth)-1;
if frac(depth)<0.001 then
midspread:=datelist[n-d]-datelist[1+d]
else
midspread:=
round(1.0*(datelist[n-d]+datelist[n-d-1]-datelist[1+d]-datelist[2+d])/2.0);
end;
{*************************************}
{**** Calibration Procedures *****)
{*************************************}
function offset(v,origin: integer): integer;
begin {conceptual array with lower bound of origin}
offset:=v-origin+1; {offset in actual linear array with bounds from 1..?}
end;
function interpolate(pos,fromss,toss,finddate: integer): integer;
var dif,frac: real;
begin
dif:=calib[pos+1,fromss]-calib[pos,fromss];
if dif=0 then writeln('Possible problem in Interpolate, dif=0, year=',finddate);
if dif=0 then frac:=0
else frac:=1.0*(finddate-calib[pos,fromss])/dif;
interpolate:=round(calib[pos,toss]+frac*(calib[pos+1,toss]-calib[pos,toss]));
end;
procedure fill_calib_year;
var i,j,k: integer; {c: char;}
begin
i:=1;
if calib_earliest<calib_year_array_earliest then begin
calib_origin:=calib_year_array_earliest;
{skip over entries before earliest considered}
while (i<calib_entries) and (calib_origin>calib[i,calend]) do inc(i);
calib_origin:=calib[i,calend];
end
else calib_origin:=calib_earliest;
alloc_matrix(calib_year,calib_latest-calib_origin+1,sizeof(integer));
{fill calib_year with corresponding interpolated c14 dates
calib_year index is a BP calendar year; value is the
linearly interpolated radiocarbon year between adjacent values
in calibration dataset}
{$R-}
calib_year^[offset(calib[i,calend],calib_origin)]:=calib[i,c14];
for j:=i to calib_entries-1 do
for k:=calib[j,calend]+1 to calib[j+1,calend] do
calib_year^[offset(k,calib_origin)]:=interpolate(j,calend,c14,k);
{for i:=calib_origin to calib_latest do begin
write(i:6,offset(i,calib_origin):7,calib_year^[offset(i,calib_origin)]:7);
if (i mod 88)=0 then begin
c:=readchoice('[C]ontinue [Q]uit [S]kip?','CQ','C');
if c='Q' then halt;
end;
end;}
{$R+}
end;
procedure read_calib;
var c,df: ansichar; i: integer; lne: ansistring;
calendar_year, cal_BP, skip, radiocarbon, sigma: real;
begin
{if date[ndate]>6000 then df:='2' else df:='1';} df:='1';
c:=readchoice('Calibration File: [1]IntCal13, [2]IntCal93, [3]UWTen93, [M]arine93','123M',df);
case c of
'1': calibin:='INTCAL13.14C';
'2': calibin:='INTCAL93.14C';
'3': calibin:='UWTEN93.14C';
'M': calibin:='MARINE93.14C';
end;
readfile('Calibration File',dskin,calibin);
{writeln(' Note Program will only work on calibrated dates back to ',
0-calib_year_array_earliest,' BP');} {not clear why this limit}
for i:=1 to 11 do begin
readln(dskin,lne); {skip over header}
{writeln(lne);} {for debugging}
end;
calib_entries:=0;
calib[0,1]:=low(integer); calib[0,2]:=maxint; calib[0,3]:=maxint;
while not(seekeof(dskin)) do begin
inc(calib_entries);
case c of
'1': begin {IntCal13.14c}
cal_BP:=getreal(dskin);
radiocarbon:=getreal(dskin);
skip:=getreal(dskin); {error}
skip:=getreal(dskin); {delta14C}
sigma:=getreal(dskin); {sigma}
if cal_BP<1950 then calendar_year:=1950-cal_BP else calendar_year:=1950-cal_BP-1;
end;
'2','3','M': begin
{readln(dskin,calendar_year,delta14C,sigma,radiocarbon,error);}
calendar_year:=getreal(dskin);
skip:=getreal(dskin); {delta14C}
sigma:=getreal(dskin); {sigma}
radiocarbon:=getreal(dskin);
skip:=getreal(dskin);
end;
end;
if calendar_year>10000 {gets rid of 99999.9 for missing data} then dec(calib_entries)
else begin
calib[calib_entries,calend]:=round(calendar_year);
calib[calib_entries,c14]:=round(radiocarbon);
calib[calib_entries,c14SD]:=round(sigma);
end;
end;
close(dskin);
calib_earliest:=calib[1,calend];
calib_latest:=calib[calib_entries,calend];
fill_calib_year;
end;
function next_intercept(var pos: integer; finddate: integer): integer;
var rslt: integer;
begin
while (pos<calib_entries) and (finddate<>calib[pos,c14]) and
(not ((finddate<max(calib[pos,c14],calib[pos+1,c14])) and
(finddate>min(calib[pos,c14],calib[pos+1,c14])))) do inc(pos);
if pos=calib_entries then rslt:=maxint else
begin
if finddate=calib[pos,c14] then rslt:=calib[pos,calend]
else rslt:=interpolate(pos,c14,calend,finddate);
inc(pos);
end;
next_intercept:=rslt;
end;
procedure calendar_stats(count: integer; var date_vector,weight: date_array; weighted: boolean);
var i: integer; weightsum, sum: real;
begin
sortdate(count,date_vector,weight);
cfirstdate:=date_vector[1];
clastdate:=date_vector[count];
cdaterange:=clastdate-cfirstdate;
cdate_iqr:=midspread(count,date_vector);
if (count mod 2)=1 then cmedian_date:=date_vector[(count+1) div 2] else
cmedian_date:=round((1.0*date_vector[(count div 2)]+1.0*date_vector[(count div 2)+1])/2);
sum:=0.0; weightsum:=0;
for i:=1 to count do
if weighted then begin
sum:=sum+date_vector[i]/weight[i];
weightsum:=weightsum+1/weight[i];
end
else begin
sum:=sum+date_vector[i];
weightsum:=weightsum+1.0;
end;
cmean_date:=roundto(round(sum/weightsum),rounddates);
end;
procedure calibrate;
{take uncalibrated dates in date and put calibrated intercepts in datemean}
var i,j,d, pos,start{,cnt}: integer; {sum: real; }
begin
nintercept:=1;
for i:=1 to ndate do begin
pos:=1;
while pos<>calib_entries do begin
date_intercept[nintercept]:=next_intercept(pos,date[i]);
date_index[nintercept]:=i;
if date_intercept[nintercept]<>maxint then inc(nintercept);
if nintercept>maxdate then begin
HaltProgram('Fatal Error: Problem too Large: Too Many Calibrated Intercepts');
end;
end;
end;
dec(nintercept);
if nintercept<=0 then begin
HaltProgram('Fatal Error: All Dates out of range of calibration file');
end;
{this is put in to weight each real date equally and weight the intercepts accordingly}
i:=1;
d:=1;
start:=1;
while i<=nintercept do begin
while (i<=nintercept) and (date_index[i]=d) do inc(i);
for j:=start to (i-1) do date_count[j]:=i-start;
inc(d); start:=i;
end;
end;
{*************************************}
{**** Date Calculations *****)
{*************************************}
function calc_K(rectangular: boolean; limit: real): real;
{limit is +/- sd oof truncted normal}
var sum,sumsq, slice, x, val, s2, mean, K, factor, length, totwt: real;
nslice, i,cnt: integer;
begin
{nslice:=readint('Number of slices',100,10000000,'10000');}
nslice:=20000;
if rectangular then k:=sqrt(12.0)
{length:=readreal('Length tested',1,1000,'100');}
{slice:=length/nslice; sum:=0.0; sumsq:=0.0;
for i:=0 to (nslice-1) do begin
x:=slice*i+slice/2.0;
sum:=sum+x;
sumsq:=sumsq+x*x;
end;
mean:=sum/nslice;
s2:= (1.0/(nslice-1))*(sumsq-nslice*mean*mean);
end}
else
begin
{factor:=readreal('Factor',100,10000000,'10000');}
factor:=1000000; {values to 6 decimals w/ 20K slides from 0.5 to 4sd)
{std of normal distribution between sd cutoffs}
length:=limit*2;
slice:=length/nslice; sum:=0.0; sumsq:=0.0; totwt:=0; {area:=0;}
for i:=0 to (nslice-1) do begin
x:=slice*i-limit+slice/2.0;
val:=exp(-x*x/2.0)/sqrt(2*pi);
cnt:=round(factor*val);
totwt:=totwt+cnt;
sum:=sum+x*cnt;
{area:=area+abs(val*slice); }
sumsq:=sumsq+x*x*cnt;
end;
mean:=sum/totwt;
s2:= (1.0/(totwt-1))*(sumsq-totwt*mean*mean); {second term should be 0}
K:= sqrt(length*length/s2);
end;
calc_K:=K
end;
function calculate_ihat: integer;
var i,ihat: integer;
meandate, meanstd, datesum, stdsum, datesumsq, datevar, adjvar, avgvar: real;
begin
if ndate<=1 then ihat:=-1
else begin
datesum:=0.0; datesumsq:=0.0; stdsum:=0.0;
for i:=1 to ndate do begin
datesum:=datesum+date[i];
datesumsq:=datesumsq+1.0*date[i]*date[i];
stdsum:=stdsum+date_std[i];
end;
meandate:=datesum/ndate;
meanstd:=stdsum/ndate;
datevar:=(1.0/(ndate))*(datesumsq-1.0*ndate*meandate*meandate) ;
adjvar:=datevar*ndate/(ndate-1);
avgvar:=1.0*meanstd*meanstd;
if adjvar>=avgvar then begin
ihat:=round(ihatK*sqrt(adjvar-avgvar));
end
else begin
ihat:=-1;
inc(ihat_warning);
end
end;
calculate_ihat:=ihat;
end;
procedure date_stats;
var i: integer; total: real;
begin;
{maxempiricalstd:=0; }
total:=0.0;
for i:=1 to ndate do begin
date_intercept[i]:=date[i]; {Sorted vector of original dates}
total:=total+date[i];
{if date_std[i]>maxempiricalstd then maxempiricalstd:=date_std[i];}
end;
firstdate:=date[1];
lastdate:=date[ndate];
daterange:=lastdate-firstdate;
date_iqr:=midspread(ndate,date);
mean_date:=round(total/ndate);
if (ndate mod 2)=1 then median_date:=date[(ndate+1) div 2] else
median_date:=round((1.0*date[(ndate div 2)]+1.0*date[(ndate div 2)+1])/2);
e_Ihat:=calculate_Ihat; {Note if the model has not been set then K will be -1}
end;
{*************************************}
{**** Input/Output Procedures *****)
{*************************************}
procedure write_parms_console(head: boolean);
begin
if head then begin
writeln;
writeln({'Random Seed ',}'#Dates Earliest Latest Span IQR Mean Median Ihat K');
end;
write({orig_randseed:11,' ',}ndate:6,firstdate:10,lastdate:8,
daterange:7,date_iqr:7,mean_date:7,median_date:8,e_ihat:6,ihatk:5:2,' Uncalibrated');
if e_ihat<0 then writeln(' (Ihat undefined)') else writeln;
if calibrated_dates then
writeln({' ':12,}nintercept:6,cfirstdate:10,clastdate:8,
cdaterange:7,cdate_iqr:7,cmean_date:7,cmedian_date:8,' ','BC/AD');
end;
procedure print_date_stats(head:boolean);
begin
if head then begin
writeln(dskout);
writeln(dskout,'#Dates Earliest Latest Span IQR Mean Median Ihat K');
end;
write(dskout,ndate:6,firstdate:10,lastdate:8,
daterange:7,date_iqr:7,mean_date:7,median_date:8,e_ihat:6,ihatk:5:2,' Uncalibrated');
if e_ihat<0 then writeln(dskout,' (Ihat is undefined)')else writeln(dskout);
if calibrated_dates then
writeln(dskout,nintercept:6,cfirstdate:10,clastdate:8,
cdaterange:7,cdate_iqr:7,cmean_date:7,cmedian_date:8,' ':12,'BC/AD');
{writeln(dskout);}
end;
procedure output_date_stats(headonly:boolean);
begin
if headonly then begin
write(dskout,'Dates, True_Start, True End, Model, Model_SD, Earliest, Latest, Span, IQR, Mean, Median, Ihat, K');
if calibrated_dates then
writeln(dskout,', Intercepts, Cal_Earliest, Cal_Latest, Cal_Span, cal_IQR, Cal_Mean, Cal_Median')
else writeln(dskout)
end else begin
write(dskout,ndate,',',generate_start,',',generate_finish,', "',model,'",',ubound:5:2,',',
firstdate,',',lastdate,',',daterange,',',date_iqr,',',mean_date,',',
median_date,',',e_ihat:6,',',ihatk:5:2);
if calibrated_dates then
writeln(dskout,',',nintercept,',',cfirstdate,',',clastdate,',',
cdaterange,',',cdate_iqr,',',cmean_date,',',cmedian_date)
else writeln(dskout);
end;
end;
procedure write_test_results(var fle: text; extension: ansichar);
begin
if MonteCarlo then
Case extension of
'S': writeln(fle,n_tests*(this_loop-1)+test_no:4,
this_loop:4,test_no:5,test_array[0,1]:8,rslt_span[test_array[0,2]]:8,
rslt_span[test_array[0,3]]:8,rslt_span[test_array[0,4]]:8,
rslt_span[test_array[0,5]]:8,test_array[0,6]:8,
ndate:5,generate_std:5,' "N',ndate:3,'/S',generate_std:3,'"');
'C': writeln(fle,n_tests*(this_loop-1)+test_no,',',
this_loop,',',test_no,',',test_array[0,1],',',rslt_span[test_array[0,2]],',',
rslt_span[test_array[0,3]],',',rslt_span[test_array[0,4]],',',
rslt_span[test_array[0,5]],',',test_array[0,6],',',
ndate,',',generate_std:5,',',' "N',ndate:3,'/S',generate_std:3,'"');
end
else
Case extension of
'S': begin
write(fle,ndate:6,firstdate:10,lastdate:8,
daterange:7,date_iqr:7,mean_date:7,median_date:8,e_ihat:6,ihatk:5:2);
if calibrated_dates then
writeln(fle,nintercept:6,cfirstdate:10,clastdate:8,
cdaterange:7,cdate_iqr:7,cmean_date:7,cmedian_date:8)
else writeln(fle);
end;
'C': begin
write(fle,n_tests*(this_loop-1)+test_no,',',
this_loop,',',test_no,',',firstdate,',',lastdate,',',
daterange,',',date_iqr,',',mean_date,',',median_date,',',e_ihat,',',ihatk:5:2,',',
ndate:5,',',generate_std:5,',',' "N',ndate:3,'/S',generate_std:3,'"');
if calibrated_dates then
writeln(fle,',',nintercept,',',cfirstdate,',',clastdate,',',
cdaterange,',',cdate_iqr,',',cmean_date,',',cmedian_date)
else writeln(fle);
end;
end;
end;
procedure print_run_parms;
begin
Writeln(dskout,'Program PhaseLen V',version,' - (c) ',years,
' Keith W. Kintigh');
writeln(dskout);
writeln(dskout,'File: ',filename);
writeln(dskout,'Random Number Seed: ',orig_randseed);
if mode<>'T' then print_date_stats(true);
if montecarlo then begin
writeln(dskout,'KS Population Size: ',nidealtrials);
writeln(dskout,'Sample Comparison Trials: ',ntrials);
end;
if calibrated_dates then writeln(dskout,'Calibrated (Calendar Year) Interval Used')
else writeln(dskout,'Uncalibrated Interval Used');
if model='R' then writeln(dskout,'Model Distribution: Rectangular')
else writeln(dskout,'Model Distribution: Truncated Normal +/- ',
ubound:5:2,' sd');
if montecarlo then begin
if median='2' then writeln(dskout,'Using Median as Center of True Intervals')
else if median='1' then writeln(dskout,'Using Mean as Center of True Intervals')
else writeln(dskout,' Using User-specified Midpoint of ',usermiddle:5,
' as Center of True Intervals');
if euclid then writeln(dskout,'Distance = Euclidean Distance/Number of Dates')
else writeln(dskout,'Distance= Mean|deviation|');
end;
end;
procedure model_dist_parms;
begin
writeln('Model Distribution of True Dates Across the Interval');
model:=readchoice(' Model: [R]ectangular or Truncated [N]ormal','RN','R');
if model='N' then begin
writeln(' Increasing the std. cutoff increases the weight of the center');
{e.g. 1 makes -1=phase begin, +1=phase end}
lbound:=-readreal(' Std. Cutoff: +/-',0.1,5.0,'2.0');
ubound:=-lbound;
end else model:='R';
ihatK:=calc_K(model='R',ubound);
{Asymmetric unimplemented because it is no longer centered}
{else if model='A' then begin
writeln(' Asymmetric note: 0,1 gives decline, -1, 0 increase over phase');
lbound:=readreal(' Low Std Cutoff',-10.0,10.0,'-1.0');
ubound:=readreal(' Up Std Cutoff ',lbound+0.1,10.0,'1.0');
end};
end;
procedure readparm;
var dflt,temp: string[6]; datespan,t:integer; {k: real;}
begin
filename:=file_prefix(filein)+'.TXT';
writefile('Listing File or Device',dskout,filename);
if mode<>'T' then model_dist_parms;
{e_Ihat:=calculate_Ihat; }
if montecarlo then
begin
{nidealtrials:=100000; }
if mode='T' then dflt:='10000' else dflt:='10000';
nidealtrials:=readint('Number of Trials to Create KS Population',1,maxidealtrial,dflt);
if mode='T' then dflt:='1000' else dflt:='10000';
ntrials:=readint('Number of Trials for Sample Comparisons',0,maxtrial,dflt);
if ntrials=1 then ntrials:=2;
median:=readchoice('Interval Midpoint Set at [1] Mean, [2] Median, [U]ser Selected Midpoint','12U','1');
if median='U' then usermiddle:=readint(' User Specificed Midpoint of True Intervals',-maxint,maxint,'');
euclid:=readchoice('Use Adjusted [E]uclidean Distance or Mean|[D]eviation|',
'ED','E')='E';
if calibrated_dates then datespan:=cdaterange else datespan:=daterange;
minspan:=readint('Minimum True Interval Considered',0,4*datespan,'0');
datespan:=roundto(datespan+4,10); {round up to nearest 10}
if datespan>(maxint div 2) then datespan:=roundto((maxint div 2)-5,10);
{to avoid problems with very long spans and 2*datespan}
str(datespan,temp);
maxspan:=readint('Maximum True Interval Considered',0,4*datespan,temp);
testspan:=maxspan-minspan;
if testspan<= 200 then t:=10
else if testspan<=500 then t:=25
else if testspan<=1000 then t:=50
else t:=100;
str(t,temp);
inc_width:=readint('Interval Increment (>=2)',2,1000,temp);
if (testspan mod inc_width)=0 then ninterval:=testspan div inc_width
else ninterval:=(testspan div inc_width)+1;
writeln;
end;
e_ihat:=calculate_Ihat; {For Eval mode it was calculated with K=-1 the first time}
if mode<>'T' then write_parms_console(true);
if filename<>'CON' then print_run_parms;
end;
procedure print_intervals;
var incr: integer; c1,c2,c3,c4: char;
begin
writeln(dskout);
writeln(dskout,
' Phase dmax Dist Span (',daterange:4,') IQR (',date_iqr:4,')');
writeln(dskout,
' Phase ----------- ------ ------------ -----','------', ' -----------');
writeln(dskout,
'Length From To Value Mean Std Mean',' %ile', ' Mean %ile');
for incr:=0 to ninterval do begin
if test_array[0,2]=incr then c1:='<' else c1:=' ';
if test_array[0,3]=incr then c2:='<' else c2:=' ';
if test_array[0,4]=incr then c3:='<' else c3:=' ';
if test_array[0,5]=incr then c4:='<' else c4:=' ';
writeln(dskout,rslt_span[incr]:6,
middle-(rslt_span[incr] div 2):6,middle-(rslt_span[incr] div 2)+rslt_span[incr]:6,
rslt_maxdif[incr]:7:3,c1,{rslt_maxdif_p[incr]:6:1,}
rslt_dst[incr]:7:2,c2,rslt_dst_s[incr]:5:2,
rslt_len[incr]:6:0,c3,rslt_len_p[incr]:5:1,
rslt_iqr[incr]:6:0,c4,rslt_iqr_p[incr]:5:1);
end;
end;
procedure print_empirical;
{Print Simulated Empirical Distribution for Test Mode}
var i: integer;
begin
writeln(dskout);
writeln(dskout,
'============================================================================');
writeln(dskout);
writeln(dskout,'Loop: ',this_loop,' Test: ',test_no,' Number of Dates: ',ndate,
' Std: ',generate_std);
write(dskout,'True Interval: ',generate_start,' to ',
generate_start+generate_length,' Middle: ',generate_middle,' Length: ',
generate_length,' Model: ',model);
if model='N' then writeln(dskout,' (',ubound:4:1,')') else writeln(dskout);
writeln(dskout);
writeln(dskout,'"Empirical" Dates (ndate=',ndate,' std=',date_std[1],'): ');
for i:=1 to ndate do begin
write(dskout,date_intercept[i]:6);
if (i<>ndate) and ((i mod 13)=0) then writeln(dskout);
end;
writeln(dskout);
end;
procedure print_test_summary;
var t: integer;
begin
writeln(dskout);
writeln(dskout,
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
writeln(dskout);
writeln(dskout,'Test Summary (',this_loop,') Number of Dates: ',ndate,
' Standard Deviation: ',generate_std);
writeln(dskout,'Ihat could not be calculated ',ihat_warning,' times');
ihat_warning:=0;
writeln(dskout);
writeln(dskout,'Test Middle dmax Dist Span IQR Ihat');
for t:=1 to n_tests do begin
writeln(dskout,t:4,test_array[t,1]:8,rslt_span[test_array[t,2]]:8,
rslt_span[test_array[t,3]]:8,rslt_span[test_array[t,4]]:8,rslt_span[test_array[t,5]]:8,test_array[t,6]:8);
end;
end;
procedure write_heading(var fle: text; extension: ansichar);
begin {make output file a systat DATA command file to read the data}
if montecarlo then
case extension of
'S': begin
if pos('_',filename)>0 then
writeln('Warning: Illegal Name for Systat File, Remove _ from SYC File Name');
writeln(fle,'New');
writeln(fle,'Basic');
write(fle,'Save ',file_prefix(filename),' /S "True Interval ',
generate_start,' to ',generate_start+generate_length,'; Middle=');
if median='2' then write(fle,'Median; Dist=')
else if median='1' then write(fle,'Mean; Dist=')
else write(fle,'User Midpoint=',usermiddle,'; Dist=');
if euclid then writeln(fle,'Euclidean/NDate"')
else writeln(fle,'Mean|deviation|"');
writeln(fle,'Input Sequence Loop Test Mid_Date dmax Dist Span IQR Ihat Ndate SD Group$');
writeln(fle,'Run');
end;
'C': begin
writeln(fle,'Sequence, Loop, Test, Mid_Date, dmax, Dist, Span, IQR, Ihat, Ndate, SD, Group');
end;
end
else
case extension of
{'S': begin
end; }
'C', 'S': Begin
write(fle,'Sequence, Loop, Test, Earliest , Latest, Span,IQR, Mean, Median, Ihat, K, Ndate, SD, Group');
if calibrated_dates then writeln(fle,', NIntercept, CalEarliest, CalLatest, Calspan, CalIQR, CalMean, CalMedian')
else writeln(fle);
End;
end;
end;
procedure true_interval_parms;
var temp: integer; dflt: ansichar;
begin
dflt:='U';
calibrated_dates:=readchoice('Generate dates from [C]alibrated Radiocarbon or [U]ncalibrated Interval',
'CU',dflt)='C';
if calibrated_dates then begin
ndate:=1; date[ndate]:=1000;
read_calib;
end;
generate_start:=readint('True Interval Start Date',-100000,100000,'');
generate_finish:=readint('True Interval End Date',-100000,100000,'');
{accounts for entering ages BP or dates}
if generate_finish<generate_start then begin
temp:=generate_finish; generate_finish:=generate_start; generate_start:=temp;
end;
generate_length:=generate_finish-generate_start;
generate_middle:=round((generate_start+generate_finish)/2);
model_dist_parms;
end;
procedure date_N_SD_parms;
begin
ndate:=readint('Number of Dates/Sample',1,maxdate,'');
generate_std:=readint('Standard Error of Dates',0{1},1000,'');
end;
{*************************************}
{**** Monte Carlo Functions *****)
{*************************************}
{Obtain a random date starting with start for an interval of length with a std amd model of m}
{returns calibrated date, if calibrated also return calendar date }
function get_date(calib: boolean; start,length,std: integer; var calendar: integer; m: ansichar): integer;
var sample: integer;
begin
calendar:=maxint;
sample:=maxint;
case m of
'R': begin
{$R-}
if calib then begin
calendar:=start+random(length);
sample:=calib_year^[calendar-calib_origin+1] {offset removed for time}
end
{$R+}
else sample:=start+random(length); {pop. date uniform w/i range}
sample:=sample+round(normal*std);
end;
'N': begin
{$R-}
if calib then begin
calendar:=start+round((ubound+tnormal(lbound,ubound))/(ubound-lbound)*length);
sample:=calib_year^[calendar-calib_origin+1] {offset removed for time}
end
{$R+}
else sample:=start+round((ubound+tnormal(lbound,ubound))/(ubound-lbound)*length);
sample:=sample+round(normal*std)
end;
end;
get_date:=sample;
end;
procedure get_maxdif(pop_span, incr:integer);
{create population distribution fpr interval size & find empricial maxdix}
var i,err,start_date,sampledate, ignore: integer;
sum, dif, maxdif: real;
begin
sum:=0.0; err:=0; ignore:=0;
start_date:=middle-(pop_span div 2); {start date of phase}
for i:=ideal_origin to ideal_limit do
{$R-} ideal^[offset(i,ideal_origin)]:=0; {$R+}
for i:=1 to nidealtrials do begin
sampledate:=get_date(calibrated_dates, start_date,pop_span,date_std[random(ndate-1)+1],
ignore,model);
sum:=sum+sampledate;
if (sampledate>=ideal_origin) and (sampledate<=ideal_limit) then
{$R-} inc(ideal^[sampledate-ideal_origin+1]) {$R+}
else
inc(err);
end;
if err>0 then writeln('Error: ', err,' Dates out of range');
ideal_date:=round(sum/nidealtrials);
ntrialadj:=nidealtrials-err;
if ntrialadj=0 then begin
writeln('Unable to do KS Comparison - Ideal Array Out of Range');
end;
for i:=ideal_origin+1 to ideal_limit do
{$R-}
ideal^[i-ideal_origin+1]:=
ideal^[(i-1)-ideal_origin+1]+ideal^[i-ideal_origin+1]; {offset removed}
{$R+}
maxdif:=0;
if ntrialadj>0 then
for i:=1 to ndate do begin
{$R-}
dif:=abs(1.0*i/ndate-1.0*ideal^[offset(date_intercept[i],ideal_origin)]/ntrialadj);
{$R+}
if dif>maxdif then maxdif:=dif;
end;
rslt_maxdif[incr]:=maxdif;
end;
{obtain a sample set of dates}
procedure sample_set(pop_span,middle: integer);
const replacement=false;
var i,start_date: integer;
begin
randomize_order(ndate,date_std);
start_date:=middle-(pop_span div 2); {start date of phase}
for i:=1 to ndate do begin
if replacement {stds assigned with replacement} then
date[i]:=get_date(calibrated_dates,start_date,pop_span,date_std[random(ndate-1)+1],
calendar_date[i],model)
else {stds assigned without replacement}
date[i]:=get_date(calibrated_dates,start_date,pop_span,date_std[i],calendar_date[i], model)
end;
sortdate(ndate,date,date_std);
end;
{evaluate the sorted sample stored in date against empirical or test data}
procedure sample_eval(incr: integer);
var i,first,last,span,iqr: integer; dst, maxdif,dif: real;
begin
{for range}
first:=date[1];
last:=date[ndate];
span:=last-first;
rslt_len[incr]:=rslt_len[incr]+span;
rslt_len_s[incr]:=rslt_len_s[incr]+sqr(1.0*span);
if span<=daterange then inc(rslt_len_c[incr]);
{for midspread}
iqr:=midspread(ndate,date);
rslt_iqr[incr]:=rslt_iqr[incr]+iqr;
rslt_iqr_s[incr]:=rslt_iqr_s[incr]+sqr(iqr);
if iqr<=date_iqr then inc(rslt_iqr_c[incr]);
{for maxdif}
maxdif:=0;
for i:=1 to ndate do begin
{$R-}
dif:=abs(1.0*i/ndate-1.0*ideal^[date[i]-ideal_origin+1]/ntrialadj); {offset}
{$R+}
if dif>maxdif then maxdif:=dif;
end;
{if rslt_maxdif[incr]>maxdif then inc(rslt_maxdif_c[incr]); } {percentile doesn't help}
rslt_maxdif_mean[incr]:=rslt_maxdif_mean[incr]+maxdif;
rslt_maxdif_s[incr]:=rslt_maxdif_s[incr]+sqr(maxdif);
{for dist}
dst:=0.0; {deviation from ordered empirical dates}
if euclid then begin
for i:=1 to ndate do dst:=dst+sqr(1.0*date[i]-date_intercept[i]);
dst:=sqrt(dst)/ndate;
end
else begin
for i:=1 to ndate do dst:=dst+1*abs(date[i]-date_intercept[i]);
dst:=dst/ndate;
end;
rslt_dst[incr]:=rslt_dst[incr]+dst;
rslt_dst_s[incr]:=rslt_dst_s[incr]+sqr(dst);
end;
{Do evaluation trials for Evaluation or Test Modes from min by inc_w for nint intervals}
procedure runtrials(min,inc_w,nint: integer);
var i,incr,{trial,}trialspan,hours,minutes,seconds: integer; variance, percent,time_left: real;
begin
if mode<>'T' then begin
writeln;
writeln(
' Phase dmax Dist Span (',daterange:4,') IQR (',date_iqr:4,')');
writeln(
' Phase ----------- ------ ------------ -----','------', ' -----------');
writeln(
'Length From To Value Mean Std Mean',' %ile', ' Mean %ile');
end;
for incr:=0 to nint do begin
trialspan:=min+incr*inc_w;
rslt_span[incr]:=trialspan;
get_maxdif(trialspan,incr);
{sample_date:=0.0; }
rslt_maxdif_mean[incr]:=0.0; rslt_maxdif_s[incr]:=0.0;
{rslt_maxdif_p[incr]:=0.0; rslt_maxdif_c[incr]:=0; }
rslt_dst[incr]:=0.0; rslt_dst_s[incr]:=0.0;
rslt_len[incr]:=0.0; rslt_len_s[incr]:=0.0;
rslt_len_p[incr]:=0.0; rslt_len_c[incr]:=0;
rslt_iqr[incr]:=0.0; rslt_iqr_s[incr]:=0.0;
rslt_iqr_p[incr]:=0.0; rslt_iqr_c[incr]:=0;
for i:=1 to ntrials do begin
sample_set(trialspan,middle);
sample_eval(incr);
end;
if ntrials>0 then begin
{sample_date:=sample_date/(1.0*ntrials*ndate); }
rslt_maxdif_mean[incr]:=rslt_maxdif_mean[incr]/ntrials;
{rslt_maxdif_s[incr]:=sqrt((rslt_maxdif_s[incr]-
1.0*ntrials*sqr(rslt_maxdif_mean[incr]))/(ntrials-1));}
{rslt_maxdif_p[incr]:=100.0*rslt_maxdif_c[incr]/ntrials;}
rslt_dst[incr]:=rslt_dst[incr]/ntrials;
variance:=(1/(ntrials-1)) * (rslt_dst_s[incr]-1.0*ntrials*sqr(rslt_dst[incr]));
if variance<0 then rslt_dst_s[incr]:=variance
else rslt_dst_s[incr]:=sqrt(variance);
rslt_len[incr]:=rslt_len[incr]/ntrials;
rslt_len_s[incr]:=sqrt((rslt_len_s[incr]-
1.0*ntrials*sqr(1.0*rslt_len[incr]))/(ntrials-1));
rslt_len_p[incr]:=100.0*rslt_len_c[incr]/ntrials;
rslt_iqr[incr]:=rslt_iqr[incr]/ntrials;
rslt_iqr_s[incr]:=sqrt((rslt_iqr_s[incr]-
1.0*ntrials*sqr(1.0*rslt_iqr[incr]))/(ntrials-1));