-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez_fit.pro
5247 lines (4617 loc) · 151 KB
/
ez_fit.pro
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
;*************************************************************************
; Copyright (c) 2002 The University of Chicago, as Operator of Argonne
; National Laboratory.
; Copyright (c) 2002 The Regents of the University of California, as
; Operator of Los Alamos National Laboratory.
; This file is distributed subject to a Software License Agreement found
; in the file LICENSE that is included with this distribution.
;*************************************************************************
;@plot1d.pro
FUNCTION goodness_fit,yres,N,Weight=Weight
; sqrt(( W * YRES) ^2 / (M-N))
M = n_elements(yres)
if M gt N then begin
goodness = 0.
if keyword_set(Weight) then $
for i=0,M-1 do goodness = goodness + ( Weight(i)*yres(i))^2 else $
for i=0,M-1 do goodness = goodness + yres(i) ^ 2
goodness = sqrt(goodness /( M - N))
return,goodness
end
END
PRO newSegment,x,y,begin_no,end_no,newX,newY,newW,gaussian=gaussian,poisson=poisson, whole=whole
if n_params() lt 5 then begin
print,'Usage: newSegment,X,Y,begin_no,end_no,newX,newY,newW'
print,''
print,'This routine extracts a continuous segment from the data array'
print,''
return
end
width = n_elements(x)
if begin_no ge end_no then begin
print,'Error: start_no >= end_no'
return
end
if n_elements(y) eq width and width ge end_no then begin
newX = x(begin_no:end_no)
newY = y(begin_no:end_no)
newW = replicate(1., end_no-begin_no+1)
if keyword_set(gaussian) then begin
sdev = 0.05*newY
newW=1.0 / sdev ; Gaussian
end
if keyword_set(poisson) then newW=1.0 / newY ; Poisson
end
if keyword_set(whole) then begin
tempY=make_array(width, value=MIN(newY))
tempY(begin_no:end_no) = newY
newY = tempY
tempW=make_array(width)
tempW(begin_no:end_no) = newW
newW = tempW
newX = X
end
END
PRO ComfitGraf,x,y,a,yfit,sigma,print=print,test=test,geometric=geometric, $
exponential=exponential, gompertz=gompertz, hyperbolic=hyperbolic, $
logistic=logistic, logsquare=logsquare, GROUP=group
;+
; NAME:
; COMFITGRAF
;
; PURPOSE:
; This routine packages the IDL gradient-expansion least
; square fit COMFIT function to fit the paired data {x(i), y(i)}.
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; COMFITGRAF, X, Y [,A] [,YFIT] [,SIGMA] ,/FIT_TYPE [,/PRINT] [,/TEST]
;
;
; INPUTS:
;
; X: Position X vector of type integer, float or double.
;
; Y: Data value Y vector of type integer, float or double.
;
; A: Optional input, initial estimates of fitting coefficients.
; Number of elements in A depends on the FIT_TYPE specified.
; If A given, the number of elements in A must be consistant
; with the FIT_TYPE entered.
;
; KEYWORD PARAMETERS:
;
; FIT_TYPE: Six type of COMFIT, it can be any of following
;
; EXPONENTIAL Y = a0 * a1^x + a2
;
; GEOMETRIC Y = a0 * x^a1 + a2
;
; GOMPERTZ Y = a0 * a1^(a2*x) + a3
;
; HYPERBOLIC Y = 1./(a0 + a1*x)
;
; LOGISTIC Y = 1./(a0 * a1^x + a2)
;
; LOGSQUARE Y = a0 + a1*alog10(x) + a2 * alog10(x)^2
;
; PRINT: Specifies whether the output window will be poped up.
;
; TEST: Specifies whether the default test data will be used.
;
; OPTIONAL OUTPUTS:
;
; YFIT: Y vector calculated from the fitted equation.
;
; SIGMA: Standard deviation for the parameters in A.
;
; SIDE EFFECTS:
;
; The computed parameters and the convergence may depend on the data and
; the initial parameters of A vector entered.
;
; RESTRICTIONS:
; The number of parameters must match exactly to the FIT_TYPE specified.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; EXAMPLE:
; Run the geometric fitting, and pops up the fitting result window
;
; X = [ ...]
; Y = [ ...]
; A = [ 0.5, 0.5, 0.5]
; COMFITGRAF,X,Y,A,/GEOMETRIC,/PRINT
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 08-13-97.
; xx-xx-xxbkc comment
;-
if keyword_set(test) then begin
x = [2.27, 15.01, 34.74, 36.01, 43.65, 50.02, 53.84, 58.30, 62.12, $
64.66, 71.66, 79.94, 85.67, 114.95]
y = [5.16, 22.63, 34.36, 34.92, 37.98, 40.22, 41.46, 42.81, 43.91, $
44.62, 46.44, 48.43, 49.70, 55.31]
yfit = make_array(n_elements(x))
end
; newA = comfit(x, y, a, sigma = sigma, yfit = yfit, _Extra=extra)
if n_elements(a) eq 0 then begin
if keyword_set(hyperbolic) then a=[0.5,0.5] else a=[0.5,0.5,0.5]
if keyword_set(gompertz) then a =[a,0.5]
N = n_elements(A)
end
title = 'GRADIENT-EXPANSION LEAST-SQUARE FIT'
if keyword_set(exponential) then begin
title = title + ' ( EXPONENTIAL ) '
comment = 'Y = a0 * a1^x + a2 '
newA = comfit(x, y, a, sigma = sigma, yfit = yfit, /exponential)
end
if keyword_set(gompertz) then begin
title = title + ' ( GOMPERTZ ) '
comment = 'Y = a0 * a1^( a2 * x) + a3 '
newA = comfit(x, y, a, sigma = sigma, yfit = yfit, /gompertz)
end
if keyword_set(geometric) then begin
title = title + ' ( GEOMETRIC )'
comment = 'Y = a0 * x^a1 + a2 '
newA = comfit(x, y, a, sigma = sigma, yfit = yfit, /geometric)
end
if keyword_set(hyperbolic) then begin
title = title + ' ( HYPERBOLIC ) '
comment = 'Y = 1. / ( a0 + a1 * x )'
newA = comfit(x, y, a, sigma = sigma, yfit = yfit, /hyperbolic)
end
if keyword_set(logistic) then begin
title = title + ' ( LOGISTIC ) '
comment = 'Y = 1. / ( a0 * a1^x + a2 )'
newA = comfit(x, y, a, sigma = sigma, yfit = yfit, /logistic)
end
if keyword_set(logsquare) then begin
title = title + ' ( LOGSQUARE ) '
comment = 'Y = a0 + a1 * alog10(x) + a2 * alog10(x)^2 '
newA = comfit(x, y, a, sigma = sigma, yfit = yfit, /logsquare)
end
if n_elements(yfit) eq 0 then begin
str=['Usage: comfitGraf, X, Y [,A] [,YFIT] [,SIGMA] ,/FIT_TYPE [,/PRINT] [,/TEST]', $
'', $
' FIT_TYPE - EXPONENTIAL Y = a0 * a1^x + a2',$
'', $
' GEOMETRIC Y = a0 * x^a1 + a2',$
'', $
' GOMPERTZ Y = a0 * a1^(a2*x) + a3',$
'', $
' HYPERBOLIC Y = 1./(a0 + a1*x)',$
'', $
' LOGISTIC Y = 1./(a0 * a1^x + a2)',$
'', $
' LOGSQUARE Y = a0 + a1*alog10(x) + a2 * alog10(x)^2',$
'','If A is entered, then the number of elements in A must be ', $
'consistant with the FIT_TYPE entered.' $
]
res=widget_message(str,/info,title='FITTING Info')
return
end
N=n_elements(newA)
if n_elements(newA) eq 2 then begin
comment = [comment, $
'a0 = '+ string(newA(0)) + ', sigma = '+strtrim(sigma(0),2),$
'a1 = ' + string(newA(1)) + ', sigma = '+strtrim(sigma(1),2) ]
end
if n_elements(newA) eq 3 then begin
comment = [comment, $
'a0 = '+ string(newA(0)) + ', sigma = '+strtrim(sigma(0),2),$
'a1 = ' + string(newA(1)) + ', sigma = '+strtrim(sigma(1),2),$
'a2 = ' + string(newA(2)) + ', sigma = '+strtrim(sigma(2),2) ]
end
if n_elements(newA) eq 4 then begin
comment = [comment, $
'a0 = '+ string(newA(0)) + ', sigma = '+strtrim(sigma(0),2),$
'a1 = ' + string(newA(1)) + ', sigma = '+strtrim(sigma(1),2),$
'a2 = ' + string(newA(2)) + ', sigma = '+strtrim(sigma(2),2),$
'a3 = ' + string(newA(3)) + ', sigma = '+strtrim(sigma(3),2) ]
end
if n_elements(y) le 1 then begin
str=['','Usage: comfitGraf,X,Y,A,/geometric','', $
'e.g. use default test data','', $
' comfitGraf,/geometric,/test']
res = widget_message(str,/info,title='FITTING Info')
return
end
yres = yfit - y
goodness = goodness_fit(yres,N)
comment = [comment,'','GOODNESS OF FIT = '+string(goodness)]
curv=make_array(n_elements(y),2)
curv(0,1) = float(yfit)
curv(0,0) = float(y)
; plot,x,yfit
; oplot,x,y,PSYM=7
plot1d,x,curv,/curvfit,title=title,comment=comment,width=500,/stamp, $
/symbol,Group=group,wtitle='COMFIT',report='fitting.rpt'
if keyword_set(print) then begin
OPENW,unit,'fitting.rpt',/GET_LUN,ERROR=err
if err ne 0 then begin
res = widget_message(!err_string,/info,title='FITTING Info')
return
end
printf,unit,''
printf,unit,title
printf,unit,''
for i=0,n_elements(comment)-1 do printf,unit,comment(i)
printf,unit,''
printf,unit,' X Y YFIT YFIT-Y '
for i=0,n_elements(x) - 1 do printf,unit,x(i),y(i),yfit(i),yres(i)
FREE_LUN,unit
; xdisplayfile,'fitting.rpt',title=title
end
END
PRO ladfitgraf,x,y,yfit,absdev=absdev,test=test,double=double,print=print,GROUP=group
;+
; NAME:
; LADFITGRAF
;
; PURPOSE:
; This routine uses the IDL LADFIT function to fit the paired data
; {x(i), y(i)} with the linear model Y = A + Bx.
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; LADFITGRAF, X, Y [,YFIT] [,ABSDEV=absdev] [,/DOUBLE] [,/PRINT] [,/TEST]
;
;
; INPUTS:
; X: Position X vector
; Y: Data value Y vector
;
; KEYWORD PARAMETERS:
; ABSDEV: Specifies whether the mean absolute deviation to be returned.
; PRINT: Specifies whether the output window will be poped up.
; TEST: Specifies whether the default test data will be used.
; DOUBLE: If set to a non-zero value, computations are done in double
; precision arithmetic.
;
; OPTIONAL OUTPUTS:
; YFIT: Y vector calculated from the fitted equation.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; EXAMPLE:
;
; X = [ ...]
; Y = [ ...]
; LADFITGRAF,X,Y,/PRINT
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 08-13-97.
; xx-xx-xxbkc comment
;-
if keyword_set(test) then begin
x = [-3.20, 4.49, -1.66, 0.64, -2.43, -0.89, -0.12, 1.41, $
2.95, 2.18, 3.72, 5.26]
y = [-7.14, -1.30, -4.26, -1.90, -6.19, -3.98, -2.87, -1.66, $
-0.78, -2.61, 0.31, 1.74]
endif else begin
if n_params() eq 0 then begin
str='Usage: ladfitGraf,X,Y [,YFIT] [,ABSDEV=absdev] [,/DOUBLE] [,/PRINT] [,/TEST]'
str=[str,'', ' Y = A0 + A1 * X ','', $
'Linear fit - Least Absolute Deviation Method']
res=widget_message(str,/info,title='FITTING Info')
return
end
end
if keyword_set(double) then begin
x=double(x)
y=double(y)
end
A = ladfit(x, y, absdev = absdev)
yfit = A(0) + A(1) * x
curv=make_array(n_elements(y),2)
curv(0,1) = float(yfit)
curv(0,0) = float(y)
title = 'Linear Fit - Least Absolute Deviation Method'
comment = 'Y = A0 + A1 * X'
for i=0,1 do comment=[comment,'A'+strtrim(i,2)+'='+ $
string(A(i)) ]
comment=[comment,'','ABS_DEVIATION=' + string(absdev)]
yres = yfit - y
goodness = goodness_fit(yres,n_elements(A))
comment=[comment,'','GOODNESS OF FIT = '+string(goodness)]
plot1d,x,curv,/curvfit,title=title,comment=comment,width=500,/stamp, $
/symbol,Group=group,wtitle='LADFIT',report='fitting.rpt'
if keyword_set(print) then begin
OPENW,unit,'fitting.rpt',/GET_LUN,ERROR=err
if err ne 0 then begin
res = widget_message(!err_string,/info,title='FITTING Info')
return
end
printf,unit,'LADFIT - ',title
printf,unit,''
for i=0,n_elements(comment)-1 do printf,unit,comment(i)
printf,unit,''
printf,unit,' X Y YFIT YFIT-Y'
for i=0,n_elements(x)-1 do printf,unit,x(i),y(i),yfit(i),yres(i)
FREE_LUN,unit
; xdisplayfile,'fitting.rpt',title=title
end
END
PRO linfitgraf,x,y,sdev=sdev,chisq=chisq,prob=prob,sigma=sigma,double=double,test=test,print=print,GROUP=group
;+
; NAME:
; LINFITGRAF
;
; PURPOSE:
; This routine uses the IDL LINFIT function to fit the paired data
; {x(i), y(i)} with the linear model Y = A + Bx. It minimize the
; chi-square error statistic.
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; LINFITGRAF, X, Y [,YFIT] [,CHISQ=chisq] [,PROG=prob] [,SDEV=sdev]
; [,SIGMA=sigma] [,/DOUBLE] [,/PRINT] [,/TEST]
;
;
; INPUTS:
; X: Position X vector
; Y: Data value Y vector
;
; KEYWORD PARAMETERS:
; CHISQ: Use this keyword to specify a named variable which returns the
; chi-square error statistic as the sum of squared errors between
; Y(i) and A + BX(i). If individual standard deviations are
; supplied, then the chi-square error statistic is computed as
; the sum of squared errors divided by the standard deviations.
; PROB: Use this keyword to specify a named variable which returns the
; probability that the computed fit would have a value of CHISQR
; or greater. If PROB is greater than 0.1, the model parameters
; are "believable". If PROB is less than 0.1, the accuracy of the
; model parameters is questionable.
; SDEV: An n-element vector of type integer, float or double that
; specifies the individual standard deviations for {X(i), Y(i)}.
; SIGMA: Use this keyword to specify a named variable which returns a
; two-element vector of probable uncertainties for the model par-
; ameters, [SIG_A,SIG_B].
; PRINT: Specifies whether the output window will be poped up.
; TEST: Specifies whether the default test data will be used.
; DOUBLE: If set to a non-zero value, computations are done in double
; precision arithmetic.
;
; OPTIONAL OUTPUTS:
; YFIT: Y vector calculated from the fitted equation.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; EXAMPLE:
;
; X = [ ...]
; Y = [ ...]
; LINFITGRAF,X,Y,sigma=sigma,chisq=chisq,prob=prob,/PRINT
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 08-13-97.
; xx-xx-xxbkc comment
;-
if keyword_set(test) then begin
x = [-3.20, 4.49, -1.66, 0.64, -2.43, -0.89, -0.12, 1.41, $
2.95, 2.18, 3.72, 5.26]
y = [-7.14, -1.30, -4.26, -1.90, -6.19, -3.98, -2.87, -1.66, $
-0.78, -2.61, 0.31, 1.74]
sdev = replicate(0.85, n_elements(x))
endif else begin
if n_params() eq 0 then begin
str='Usage: linfitGraf,X,Y [,YFIT] [,CHISQ=chisq] [,PROB=prob] [,SDEV=sdev] '
str=[str, ' [,SIGMA=sigma] [,/DOUBLE] [,/PRINT] [,/TEST]']
str=[str,'',' Y = A0 + A1 * X ', '', $
'Linear fit by Minimize the Chi-Square Error']
res=widget_message(str,/info,title='FITTING Info')
return
end
if keyword_set(sdev) then begin
sdev = float(sdev)*y
endif else begin
vec = moment(y, mdev=md, sdev=sd)
mean = vec(0)
variance = vec(1)
skew = vec(2)
kurtosis = vec(3)
sdev = replicate(sd, n_elements(x))
end
end
if strpos(!version.release,'4.') lt 0 then $
A = linfit(x, y, sdev=sdev, chisq=chisq, prob=prob, sigma=sigma) else $
A = linfit(x, y, sdev=sdev, chisq=chisq, prob=prob)
yfit = A(0) + A(1) * x
curv=make_array(n_elements(y),2)
curv(0,1) = float(yfit)
curv(0,0) = float(y)
title = 'Linear Fit by Minimize the Chi-Square Error'
comment = 'Y = A0 + A1 * X'
if strpos(!version.release,'4.') lt 0 then begin
for i=0,1 do comment=[comment,'A'+strtrim(i,2)+'='+ $
string(A(i)) + ' SIGMA='+string(sigma(i))]
endif else begin
for i=0,1 do comment=[comment,'A'+strtrim(i,2)+'='+ $
string(A(i))]
end
yres = yfit - y
goodness = goodness_fit(yres,n_elements(A))
comment=[comment,'','GOODNESS OF FIT = '+string(goodness)]
; plot,x,yfit
; oplot,x,y,PSYM=7
plot1d,x,curv,/curvfit,title=title,comment=comment,width=500,/stamp, $
/symbol,Group=group,wtitle='LINFIT - minimize chi-square',report='fitting.rpt'
if keyword_set(print) then begin
OPENW,unit,'fitting.rpt',/GET_LUN,ERROR=err
if err ne 0 then begin
res = widget_message(!err_string,/info,title='FITTING Info')
return
end
printf,unit,'LINFIT - ',title
printf,unit,''
printf,unit,' MEAN=',mean
printf,unit,' SDEV=',sqrt(variance)
printf,unit,' VARIANCE=',variance
printf,unit,''
for i=0,n_elements(comment)-1 do printf,unit,comment(i)
printf,unit,''
printf,unit,' CHISQ=',chisq
printf,unit,' PROB=',prob
printf,unit,''
printf,unit,' X Y YFIT YFIT-Y SDEV'
for i=0,n_elements(x)-1 do printf,unit,x(i),y(i),yfit(i),yres(i),sdev(i)
printf,unit,''
printf,unit,' PROB =',prob
printf,unit,' CHISQ =',chisq, '
FREE_LUN,unit
; xdisplayfile,'fitting.rpt',title=title
end
END
PRO polyfitwgraf,x,y,w,ndegree,A,yfit,yband,sigma,print=print,GROUP=group
;+
; NAME:
; POLYFITWGRAF
;
; PURPOSE:
; This routine uses the IDL least square polynomial fit function
; PLOYFITW with optional error estimates.
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; POLYFITWGRAF,X,Y,W,NDEGREE [,A ] [,YFIT] [,YBAND] [,SIGMA] [,/PRINT]
;
;
; INPUTS:
; X: Position X vector
; Y: Data value Y vector
; W: The vector of weights. This vector should be same length as
; X and Y.
; NDEGREE: The degree of polynomial to fit.
;
; KEYWORD PARAMETERS:
; PRINT: Specifies whether the output window will be poped up.
;
; OPTIONAL OUTPUTS:
; A: Correlation matrix of the coefficients.
; YFIT: The vector of calculated Y's. Has an error of + or - Yband.
; YBAND: Error estimate for each point = 1 sigma.
; SIGMA: The standard deviation in Y units.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; EXAMPLE:
;
; X = [ ...]
; Y = [ ...]
; POLYFITWGRAF,X,Y,W,4,/PRINT
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 10-03-97.
; xx-xx-xxbkc comment
;-
if n_params() lt 4 then begin
str='Usage: polyfitwGraf,X,Y,W,NDEGREE [,A] [,YFIT] [,YBAND] [,SIGMA] [,/PRINT]'
str=[str,'',$
'Y = A0 + A1 * X^1 + A2 * X^2 + A3 * X^3 + A4 * X^4 + ...', $
'','POLYFITW - Least-Square Polynomial Fit with Weights']
res=widget_message(str,/info,title='FITTING Info')
return
end
result = polyfitw(x,y,w,ndegree,yfit,yband,sigma,corrm)
curv=make_array(n_elements(y),2)
curv(0,1) = float(yfit)
curv(0,0) = float(y)
title = 'Least-Square Polynomial Fit with Weights'
comment = 'Y = A0'
for i=1,ndegree do comment=comment+' + A'+strtrim(i,2) +' * X^'+strtrim(i,2)
for i=0,ndegree do comment=[comment,'A'+strtrim(i,2)+'='+strtrim(result(i),2)]
comment=[comment,'SIGMA='+strtrim(sigma,2)]
yres = yfit - y
goodness = goodness_fit(yres,n_elements(A))
comment=[comment,'','GOODNESS OF FIT = '+string(goodness)]
plot1d,x,curv,/curvfit,title=title,comment=comment,width=500,/stamp, $
/symbol,Group=group,wtitle='POLYFITW',report='fitting.rpt'
if keyword_set(print) then begin
OPENW,unit,'fitting.rpt',/GET_LUN,ERROR=err
if err ne 0 then begin
res = widget_message(!err_string,/info,title='FITTING Info')
return
end
printf,unit,'POLYFITW - ',title
vec = moment(y, mdev=md, sdev=sd)
mean = vec(0)
variance = vec(1)
skew = vec(2)
kurtosis = vec(3)
sdev = replicate(sd, n_elements(x))
statistic_1d,x,y,c_mass,x_peak,y_peak,y_hpeak,fwhm,fwhm_xl,fwhm_wd
st = ''
st = [st, ' Peak Y = '+strtrim(y_peak,1)]
st = [st, ' 1st Peak @ '+strtrim(x_peak,1)]
; st = [st, ' H-Peak Y = '+strtrim(y_hpeak)]
st = [st, ' Centroid @ '+ strtrim(c_mass,1)]
st = [st, ' FWHM = '+strtrim(FWHM,1)]
for i=0,n_elements(st)-1 do printf,unit,st(i)
printf,unit,''
printf,unit,' MEAN=',mean
printf,unit,' SDEV=',sqrt(variance)
printf,unit,' VARIANCE=',variance
printf,unit,''
for i=0,n_elements(comment) - 1 do printf,unit,comment(i)
printf,unit,''
printf,unit,' X Y YFIT YFIT-Y WEIGHT YBAND '
for i=0,n_elements(x)-1 do printf,unit,X(i),Y(i),YFIT(i),YRES(i),W(i),yband(i),format='(6G15.8)'
; print,'A',A
FREE_LUN,unit
; xdisplayfile,'fitting.rpt',title=title,width=100
end
END
PRO polyfitgraf,x,y,ndegree,A,YFIT,YBAND,SIGMA,CORRM,print=print,GROUP=group
;+
; NAME:
; POLYFITGRAF
;
; PURPOSE:
; This routine uses the IDL least square polynomial fit function
; PLOYFIT with optional error estimates. Double precision computation
; is assumed.
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; POLYFITGRAF,X,Y,NDEGREE [,A ] [,YFIT] [,YBAND] [,SIGMA] [,CORRM] [,/PRINT]
;
; INPUTS:
; X: Position X vector
; Y: Data value Y vector
; NDEGREE: The degree of polynomial to fit.
;
; KEYWORD PARAMETERS:
; PRINT: Specifies whether the output window will be poped up.
;
; OPTIONAL OUTPUTS:
; A: Correlation matrix of the coefficients.
; YFIT: The vector of calculated Y's. Has an error of + or - Yband.
; YBAND: Error estimate for each point = 1 sigma.
; SIGMA: The standard deviation in Y units.
; CORRM: The correlation matrix of the coefficients.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; EXAMPLE:
;
; X = [ ...]
; Y = [ ...]
; POLYFITGRAF,X,Y,4,A,/PRINT
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 10-03-97.
; xx-xx-xxbkc comment
;-
; if ndegree >=3 singular matrix detected
; always use double precision in this routine
if n_params() lt 3 then begin
str='Usage: polyfitGraf,X,Y,NDEGREE [,A] [,YFIT] [,YBAND] [,SIGMA] [,CORRM] [,/PRINT]'
str=[str,'',$
'Y = A0 + A1 * X^1 + A2 * X^2 + A3 * X^3 + A4 * X^4 + ...', $
'','POLY_FIT - Least-Square Polynomial Fit']
res=widget_message(str,/info,title='FITTING Info')
return
end
; CATCH,Error_status
; if Error_status ne 0 then begin
; res=widget_message([!err_string ,'','NDEGREE='+string(ndegree)])
; retall
; return
; end
dx=double(x)
dy=double(y)
result = poly_fit(dx,dy,ndegree,yfit,yband,sigma,corrm)
curv=make_array(n_elements(y),2)
curv(0,1) = float(yfit)
curv(0,0) = float(y)
title = 'Least-Square POLY_FIT '
comment = 'Y = A0'
for i=1,ndegree do comment=comment+' + A'+strtrim(i,2) +' * X^'+strtrim(i,2)
for i=0,ndegree do comment=[comment,'A'+strtrim(i,2)+'='+strtrim(result(i),2)]
comment=[comment,'SIGMA='+strtrim(sigma,2)]
yres = yfit - y
goodness = goodness_fit(yres,n_elements(A))
comment=[comment,'','GOODNESS OF FIT = '+string(goodness)]
plot1d,x,curv,/curvfit,title=title,comment=comment,width=500,/stamp, $
/symbol,Group=group,wtitle='POLY_FIT',report='fitting.rpt'
if keyword_set(print) then begin
OPENW,unit,'fitting.rpt',/GET_LUN,ERROR=err
if err ne 0 then begin
res = widget_message(!err_string,/info,title='FITTING Info')
return
end
printf,unit,'POLY_FIT - ',title
printf,unit,''
for i=0,n_elements(comment) - 1 do printf,unit,comment(i)
printf,unit,''
printf,unit,' X Y YFIT YFIT-Y YBAND'
for i=0,n_elements(x)-1 do printf,unit,X(i),Y(i),YFIT(i),yres(i),yband(i)
; print,'A',A
FREE_LUN,unit
; xdisplayfile,'fitting.rpt',title=title,width=90
end
END
PRO gaussfitgraf,x,y,A,estimates=estimages,nterms=nterms,print=print,GROUP=group
;+
; NAME:
; GAUSSFITGRAF
;
; PURPOSE:
; This routine uses the IDL GAUSSIAN fit function y=f(x) where:
; F(x) = A0*EXP(-z^2/2) + A3 + A4*x + A5*x^2
; and
; z=(x-A1)/A2
;
; A0 = height of exp, A1 = center of exp, A2 = sigma (the width).
; A3 = constant term, A4 = linear term, A5 = quadratic term.
;
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; GAUSSFITGRAF,X,Y [,A ] [,ESTIMATES=extimates] [,NTERMS=nterms] [,/PRINT]
;
;
; INPUTS:
; X: Position X vector
; Y: Data value Y vector
;
; KEYWORD PARAMETERS:
; ESTIMATES: Optional starting estimates for the parameters of the
; equation. Should contain NTERMS (6 if NTERMS is not
; provided) elements.
; NTERMS: Set NTERMS of parameters used in Gaussian fit.
; PRINT: Specifies whether the output window will be poped up.
;
; OPTIONAL OUTPUTS:
; A: The coefficients of the fit. A is a three to six
; element vector as described under PURPOSE.
;
; RESTRICTIONS:
; The peak or minimum of the Gaussian must be the largest
; or smallest point in the Y vector.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; EXAMPLE:
;
; X = [ ...]
; Y = [ ...]
; GAUSSFITGRAF,X,Y,NTERMS=4,/PRINT
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 10-03-97.
; xx-xx-xxbkc comment
;-
if n_params() lt 2 then begin
str="Usage: gaussfitGraf, X, Y [,A] [,ESTIMATES=estimates] [,NTERMS=nterms] [,/PRINT]"
str=[str,'', $
'F(X) = A0 * exp( -Z^2 / 2 ) [ + A3 + A4 * X + A5 * X^2 ]',$
' where Z = (X - A1) / A2']
res=widget_message(str,/info,title='FITTING Info')
return
end
if keyword_set(nterms) then begin
if nterms lt 3 then nterms = 3
if nterms gt 6 then nterms = 6
yfit = gaussfit(x,y,A,nterms=nterms)
endif else yfit = gaussfit(x,y,A)
curv=make_array(n_elements(y),2)
curv(0,1) = float(yfit)
curv(0,0) = float(y)
fname = 'gaussfit'
title = 'Non-linear Least-square Fit of Gaussian'
get_curvefit_function,fname,comment
for i=0,n_elements(A)-1 do comment=[comment,'A'+strtrim(i,2)+'='+ $
string(A(i))]
yres = yfit - y
goodness = goodness_fit(yres,n_elements(A))
comment=[comment,'','GOODNESS OF FIT = '+string(goodness)]
; plot,x,yfit
; oplot,x,y,PSYM=7
plot1d,x,curv,/curvfit,title=title,comment=comment,width=500,/stamp, $
/symbol,Group=group,wtitle='GAUSSFIT',report='fitting.rpt'
if keyword_set(print) then begin
OPENW,unit,'fitting.rpt',/GET_LUN,ERROR=err
if err ne 0 then begin
res = widget_message(!err_string,/info,title='FITTING Info')
return
end
vec = moment(y, mdev=md, sdev=sd)
mean = vec(0)
variance = vec(1)
skew = vec(2)
kurtosis = vec(3)
sdev = replicate(sd, n_elements(x))
printf,unit,''
printf,unit,'GAUSSFIT - ',title
printf,unit,''
printf,unit,' MEAN=',mean
printf,unit,' SDEV=',sqrt(variance)
printf,unit,' VARIANCE=',variance
printf,unit,''
for i=0,n_elements(comment)-1 do printf,unit,comment(i)
printf,unit,''
printf,unit,' X Y YFIT YFIT-Y'
for i=0,n_elements(x)-1 do printf,unit,x(i),y(i),yfit(i),yres(i)
FREE_LUN,unit
; xdisplayfile,'fitting.rpt',title='GAUSSFIT'
end
END
PRO curvefitgraf,x,y,Weights,A,sigma,test=test,print=print,function_name=function_name,noderivative=noderivative,itmax=itmax,tol=tol,GROUP=group
;+
; NAME:
; CURVEFITGRAF
;
; PURPOSE:
;
; Then calls PLOT1D to graph the calculated results with the raw
; data in a pop-up window.
;
; CATEGORY:
; Curve fitting with plot.
;
; CALLING SEQUENCE:
;
; CURVEFITGRAF,X,Y,Weights [,A ] [,Sigma] [,/PRINT] $
; [,FUNCTION_NAME='funct'] $
; [,/NODERIVATIVE] [,ITMAX=20] [,TOL=1.e-3]
;
;
; INPUTS:
; X: Position X vector
; Y: Data value Y vector
; Weights: A row vector of weights, the same length as Y. Defaults to 1.
; Instrumental weighting-Gaussian : Weights(i) = 1./sigma(i) ^2
; Statistical weighting-Poisson : Weights(i) = 1./y(i)
; A: The coefficients of the fit. The number of elements in A
; must be exactly the same as that defined in the function_name.
; If not specified, the fitting function should provide the
; initial default.
;
; KEYWORD PARAMETERS:
; PRINT: Specifies whether the output window will be poped up.
; FUNCTION_NAME: The name of the procedure function to fit. If omitted,
; NODERIVATIVE: If this keyword is set then the partial derivatives will be
; calculated by CURVEFIT by forward differences. Otherwise
; the procedure function should provide the partial
; derivatives calculation. Defaults nodevivative is not set.
; The procedure function must be written as in 'FUNCT' as
; described in IDL 'CURVEFIT' restrictions.
; ITMAX: Maximum number of iterations. Default = 20.
; TOL: The convergence tolerance. Default = 1.e-3. The routine
; returns when the relative decrease in chi-squared is less
; than TOL.
;
; OPTIONAL OUTPUTS:
; A: Returns the coefficients of the fit.
; Sigma: A vector of standard deviations for the parameters in A.
;
; RESTRICTIONS:
; The function to be fit must be defined and called FUNCT,
; unless the FUNCTION_NAME keyword is supplied. This function,
; (actually written as a procedure) must accept values of
; X (the independent variable), and A (the fitted function's
; parameter values), and return F (the function's value at
; X), and PDER (a 2D array of partial derivatives).
; For an example, see FUNCT in the IDL User's Libaray.
; A call to FUNCT is entered as:
; FUNCT, X, A, F, PDER
; where:
; X = Variable passed into CURVEFIT. It is the job of the user-written
; function to interpret this variable.
; A = Vector of NTERMS function parameters, input.
; F = Vector of NPOINT values of function, y(i) = funct(x), output.
; PDER = Array, (NPOINT, NTERMS), of partial derivatives of funct.
; PDER(I,J) = DErivative of function at ith point with
; respect to jth parameter. Optional output parameter.
; PDER should not be calculated if the parameter is not
; supplied in call. If the /NODERIVATIVE keyword is set in the
; call to CURVEFIT then the user routine will never need to
; calculate PDER.
;
; PROCEDURE:
; Before accessing this routine, the 'ez_fit.pro' must be loaded into
; IDL and the path to 'ez_fit.pro' must be in the IDL search path.
;
; For more information please refer the PROCEDURE section of the
; CURVEFIT in IDL online help.
;
; EXAMPLE:
;
; X = [ ...]
; Y = [ ...]
; CURVEFITGRAF,X,Y,Weights,A,Sigma,/PRINT
;
; For more information please refer the EXAMPLE section of the CURVEFIT
; in IDL online help.
;
; MODIFICATION HISTORY:
; Written by: Ben-chin K. Cha, 10-15-97.
; xx-xx-xxbkc comment
;-
fname = 'funct'
if keyword_set(function_name) then fname=string(function_name)
if keyword_set(test) then begin
fname = 'gfunct'
x=findgen(10)
y=[12.,11.,10.2,9.4,8.7,8.1,7,5,6.9,6.5,6.1]
A =[ 10., -0.1, 2.0]
endif else begin