Blame view

src/idl/dustem_brute_force_fit.pro 10 KB
f8c3f95f   Jean-Philippe Bernard   added limits and ...
1
FUNCTION dustem_brute_force_fit,sed $
849fd436   Jean-Philippe Bernard   improved
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
							   							 ,table_name $
							   							 ,filters $
							   							 ,fixed_parameters_description=fixed_parameters_description $
							   							 ,fixed_parameters_values=fixed_parameters_values $
							   							 ,normalize=normalize $
							   							 ,params_hit=params_hit $
							   							 ,params_uncertainties=params_uncertainties $
							   							 ,params_min=params_min $
							   							 ,params_max=params_max $
							   							 ,fact=fact $
							   							 ,chi2=chi2 $
							   							 ,rchi2=rchi2 $
							   							 ,best_sed=best_sed $
							   							 ,best_grid_sed=best_grid_sed $
							   							 ,show_sed=show_sed $
							   							 ,nostop=nostop $
441c3551   Jean-Philippe Bernard   improved
18
							   							 ,reset=reset $
026c2ba5   Jean-Philippe Bernard   improved
19
							   							 ,weighted_params=weighted_params $
33859661   Jean-Philippe Bernard   brought up to dat...
20
21
22
							   							 ,weighted_sed=weighted_sed $
							   							 ,weighted_chi2=weighted_chi2 $
							   							 ,rweighted_chi2=rweighted_chi2 $
9b37a060   Jean-Philippe Bernard   improved for smoo...
23
							   							 ,all_chi2s=all_chi2s $
33859661   Jean-Philippe Bernard   brought up to dat...
24
25
							   							 ,weightning=weightning $
							   							 ,marginalize_method=marginalize_method $
9b37a060   Jean-Philippe Bernard   improved for smoo...
26
							   							 ,status=status
6ff7d98f   Jean-Philippe Bernard   First commit
27

dbf82b16   Jean-Philippe Bernard   chaaged the plot
28
29
30
31
32
33
34
35
;+
; NAME:
;    dustem_brute_force_fit
; PURPOSE:
;    does brut force fit from a pre-computed model grid
; CATEGORY:
;    DustEM
; CALLING SEQUENCE:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
36
;    res=dustem_brute_force_fit(sed,table_name,filters[,/normalize][,fact=][,chi2=][,rchi2=][rchi2][best_sed=][,/show_sed][,/nostop][,/reset])
dbf82b16   Jean-Philippe Bernard   chaaged the plot
37
; INPUTS:
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
38
;    sed  = sed to be fit. Should contain all filters provided to the routine trhough input variable filter
f8c3f95f   Jean-Philippe Bernard   added limits and ...
39
40
;    table_name = name of the fits file to be used as a grid (see dustem_make_sed_table.pro for creation)
;    filters    = filters to be used in the grid table (only used if !dustem_grid does not exist or if /reset)
dbf82b16   Jean-Philippe Bernard   chaaged the plot
41
; OPTIONAL INPUT PARAMETERS:
849fd436   Jean-Philippe Bernard   improved
42
43
;    fixed_parameters_description = if set, describes parameters of the table that should be considered fixed.
;    fixed_parameters_values      = values of parameters of the table that should be considered fixed.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
44
; OUTPUTS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
45
;    res = best values of the free parameters. Not that linear parameters to the sed should be multiplied by fact.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
46
; OPTIONAL OUTPUT PARAMETERS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
47
;    fact = best scaling factor. Such that best_sed*fact is for NH=1.e20 H/cm2 (as in the grid). IF sed is already normalized to NH=1e20 H/cm2, no need to set /normalize
b4dbd43c   Jean-Philippe Bernard   chaaged the plot
48
49
;    chi2 = chi2 of the best fit
;    rchi2 = reduced chi2 of the best fit
f8c3f95f   Jean-Philippe Bernard   added limits and ...
50
51
52
53
54
55
;    best_grid_sed = best SED found in the grid
;    best_sed = best fit SED matching input SED (such that best_sed=best_grid_sed*fact)
;    params_hit= 1 if parameter hits the top, -1 if hits the bottom, 0 otherwise
;    params_uncertainties = 90% confidence level uncertainty on parameters
;    params_min = 90% lower limit of parameters
;    params_max = 90% upper limit of parameters
934cfd91   Jean-Philippe Bernard   improved custom g...
56
;    weighted_params = likelyhood exp(-chi2/2) weighted best parameter values
33859661   Jean-Philippe Bernard   brought up to dat...
57
;    weighted_sed = likelyhood exp(-chi2/2) weighted best sed
dbf82b16   Jean-Philippe Bernard   chaaged the plot
58
59
; ACCEPTED KEY-WORDS:
;    help                  = if set, print this help
f8c3f95f   Jean-Philippe Bernard   added limits and ...
60
61
62
;    normalize = if set, the best match is done based on the shape of the SEDs only, instead of their absolute values. fact will be computed.
;    show_sed = if set, plot the seds
;    nostop   = if set, do not stop
dbf82b16   Jean-Philippe Bernard   chaaged the plot
63
64
65
66
67
68
69
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    None
; RESTRICTIONS:
;    The DustEMWrap IDL code must be installed
; PROCEDURE:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
70
;    Scans the grid SEDs to find the best fit (lowest chi2)
dbf82b16   Jean-Philippe Bernard   chaaged the plot
71
; EXAMPLES
f8c3f95f   Jean-Philippe Bernard   added limits and ...
72
;    
dbf82b16   Jean-Philippe Bernard   chaaged the plot
73
; MODIFICATION HISTORY:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
74
;    Written by JPB Jan 2024
dbf82b16   Jean-Philippe Bernard   chaaged the plot
75
76
77
78
79
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.  
;-

IF keyword_set(help) THEN BEGIN
f8c3f95f   Jean-Philippe Bernard   added limits and ...
80
81
  doc_library,'dustem_brute_force_fit'
  params=0.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
82
83
84
  goto,the_end
ENDIF

6ff7d98f   Jean-Philippe Bernard   First commit
85
;stop
88f84272   Jean-Philippe Bernard   changed
86
;sed must be normalized to NH=1e20 H/cm2
6ff7d98f   Jean-Philippe Bernard   First commit
87

f8c3f95f   Jean-Philippe Bernard   added limits and ...
88
;==== test for the existence of a loaded grid and initialize if needed.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
89
;==== only filters contained in the filters variable are kept in the grid, and the sum of grid seds are recomputed accordingly
6ff7d98f   Jean-Philippe Bernard   First commit
90
defsysv,'!dustem_grid',0,exist=exist
f8c3f95f   Jean-Philippe Bernard   added limits and ...
91
IF exist EQ 0 or keyword_set(reset)THEN BEGIN
9b37a060   Jean-Philippe Bernard   improved for smoo...
92
93
94
95
96
    dustem_define_grid,table_name,filters=filters,status=status
    IF status NE 1 THEN BEGIN
    	params=-1
    	goto,the_end
    ENDIF
6ff7d98f   Jean-Philippe Bernard   First commit
97
98
ENDIF

9f184234   Jean-Philippe Bernard   improved
99
;seds=!dustem_grid.seds
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
100
101
102
103
104
105
106
107
108
seds=(*!dustem_grid).st_seds
params=(*!dustem_grid).st_params
totals=(*!dustem_grid).sed_totals
Nseds=(*!dustem_grid).Nsed
Nfilters=(*!dustem_grid).Nfilters
Nparams=(*!dustem_grid).Nparams
table_params=(*!dustem_grid).params
table_pmins=(*!dustem_grid).pmin_values
table_pmaxs=(*!dustem_grid).pmax_values
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
109

33859661   Jean-Philippe Bernard   brought up to dat...
110
111
112
113
114
use_weightning='rellikelyhood'
IF keyword_set(weightning) THEN BEGIN
	use_weightning=weightning
ENDIF

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
115
;=== marginalize table for fixed parameters, if any
33859661   Jean-Philippe Bernard   brought up to dat...
116
117
118
;stop
use_marginalize_method='NEAREST'
IF keyword_set(marginalize_method) THEN use_marginalize_method=marginalize_method
849fd436   Jean-Philippe Bernard   improved
119
IF keyword_set(fixed_parameters_description) THEN BEGIN
33859661   Jean-Philippe Bernard   brought up to dat...
120
	dustem_marginalize_grid,fixed_parameters_description,fixed_parameters_values,seds,Nseds,params,Nparams,table_params,table_pmins,table_pmaxs,method=use_marginalize_method
849fd436   Jean-Philippe Bernard   improved
121
122
ENDIF

0d19c43e   Jean-Philippe Bernard   improved
123
124
125
126
127
128
129
130
131
;help,seds,Nseds,params,Nparams,table_params,table_pmins,table_pmaxs
;SEDS            STRUCT    = -> <Anonymous> Array[400]
;NSEDS           LONG      =          400
;PARAMS          STRUCT    = -> <Anonymous> Array[400]
;NPARAMS         LONG      =            3
;TABLE_PARAMS    STRING    = Array[3]
;TABLE_PMINS     DOUBLE    = Array[3]
;TABLE_PMAXS     DOUBLE    = Array[3]

a31eb1cd   Jean-Philippe Bernard   unstopped
132
;stop
0d19c43e   Jean-Philippe Bernard   improved
133

849fd436   Jean-Philippe Bernard   improved
134
;=== These are the full arrays needed for calculations
6ff7d98f   Jean-Philippe Bernard   First commit
135
chi2s=dblarr(Nseds)
f8c3f95f   Jean-Philippe Bernard   added limits and ...
136
137
138
grid_seds=dblarr(Nseds,Nfilters)
total_seds=dblarr(Nseds)
grid_param_values=dblarr(Nseds,Nparams)
6ff7d98f   Jean-Philippe Bernard   First commit
139
140
141
142

;=== compute sed grid Array
FOR i=0L,Nseds-1 DO BEGIN
	FOR j=0L,Nfilters-1 DO BEGIN
9f184234   Jean-Philippe Bernard   improved
143
144
		;grid_seds[i,j]=seds[i].(j+Nparams)
		grid_seds[i,j]=seds[i].(j)
6ff7d98f   Jean-Philippe Bernard   First commit
145
	ENDFOR
9f184234   Jean-Philippe Bernard   improved
146
147
	;total_seds[i]=seds[i].total
	total_seds[i]=totals[i]
6ff7d98f   Jean-Philippe Bernard   First commit
148
149
150
151
ENDFOR
;=== compute the parameter values Array
FOR i=0L,Nseds-1 DO BEGIN
	FOR j=0L,Nparams-1 DO BEGIN
9f184234   Jean-Philippe Bernard   improved
152
153
		;grid_param_values[i,j]=seds[i].(j)
		grid_param_values[i,j]=params[i].(j)
6ff7d98f   Jean-Philippe Bernard   First commit
154
155
156
157
	ENDFOR
ENDFOR
;stop

f8c3f95f   Jean-Philippe Bernard   added limits and ...
158
;=== loop over grid lines to compute chi2s
849fd436   Jean-Philippe Bernard   improved
159
;we do actually store the total of grid sed for each grid sed into the grid fits file
6ff7d98f   Jean-Philippe Bernard   First commit
160
161
162
;we could also store the total of observed seds in the observed sed save fits file (!)
facts=dblarr(Nseds)
facts[*]=1.d0
026c2ba5   Jean-Philippe Bernard   improved
163
;stop
6ff7d98f   Jean-Philippe Bernard   First commit
164
165
IF keyword_set(normalize) THEN BEGIN
	FOR i=0L,Nseds-1 DO BEGIN
026c2ba5   Jean-Philippe Bernard   improved
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		ref_sed=reform(grid_seds[i,*])
		;total_ref_sed1=total_seds[i]
		total_ref_sed2=total(ref_sed)
		;diff=abs(total_ref_sed1-total_ref_sed2)
		;print,diff
		;total_ref_sed=total_ref_sed1    ;This does not work. I'm not sure why, as it is supposed to be the same pre-computed ...
		total_ref_sed=total_ref_sed2    ;This does !!		
		;IF diff GT 0.1 THEN stop
		facts[i]=total(sed.stokesI)/total_ref_sed
    this_ref_sed=ref_sed*facts[i]
    this_sed=sed.stokesI
    this_var=sed.sigmaII
		;this_sed=sed.stokesI/facts[i]
		;this_var=sed.sigmaII/facts[i]^2
849fd436   Jean-Philippe Bernard   improved
180
	  ;chi2s[i]=total((sed.stokesI-grid_seds[i,*])^2/sed.sigmaII)
026c2ba5   Jean-Philippe Bernard   improved
181
	  chi2s[i]=total((this_sed-this_ref_sed)^2/this_var)
6ff7d98f   Jean-Philippe Bernard   First commit
182
183
184
	ENDFOR
ENDIF ELSE BEGIN
	FOR i=0L,Nseds-1 DO BEGIN
849fd436   Jean-Philippe Bernard   improved
185
	   chi2s[i]=total((sed.stokesI-grid_seds[i,*])^2/sed.sigmaII)
6ff7d98f   Jean-Philippe Bernard   First commit
186
187
	ENDFOR
ENDELSE
0d19c43e   Jean-Philippe Bernard   improved
188

026c2ba5   Jean-Philippe Bernard   improved
189
all_chi2s=chi2s
6ff7d98f   Jean-Philippe Bernard   First commit
190
191
192
chi2=min(chi2s)
ind=where(chi2s EQ chi2,count)

026c2ba5   Jean-Philippe Bernard   improved
193
194
195
196
;stop
;print,facts[14406],facts[ind[0]]
;print,chi2s[14406],chi2s[ind[0]]

0d19c43e   Jean-Philippe Bernard   improved
197
params=reform(grid_param_values[ind[0],*])  ;these are the best fit parameters
f8c3f95f   Jean-Philippe Bernard   added limits and ...
198
fact=facts[ind[0]]   ; This is the corresponding scaling factor
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
199
Nfreedom=Nfilters-Nparams-1   ;This is the degree of freedom
f8c3f95f   Jean-Philippe Bernard   added limits and ...
200
201
202
203
rchi2=chi2/Nfreedom        ;This is the reduced chi2
best_grid_sed=reform(grid_seds[ind[0],*])   ;This is the best SED fround in the grid.
best_sed=best_grid_sed*fact  ;This is the best scaled grid SED matching the input SED.

441c3551   Jean-Philippe Bernard   improved
204
;=== compute Likelyhood weighted params values
96d87510   Jean-Philippe Bernard   modified after di...
205
epsilon=1.d-20
0bbec005   Jean-Philippe Bernard   improved
206
weighted_params=dblarr(Nparams)
33859661   Jean-Philippe Bernard   brought up to dat...
207
208
209
210
211
212
CASE use_weightning OF
	'1/chi2'      :weights=1.d0/(chi2s+epsilon)
	'1/sqrt(chi2)':weights=1.d0/sqrt(chi2s+epsilon)
	'likelyhood'  :weights=exp(-1.d0*chi2s/2.)
	'rellikelyhood':weights=exp(-1.d0*(chi2s/chi2)/2.)
ENDCASE
934cfd91   Jean-Philippe Bernard   improved custom g...
213
;weights=1.d0/(chi2s+epsilon)
96d87510   Jean-Philippe Bernard   modified after di...
214
;weights=1.d0/sqrt(chi2s+epsilon)
33859661   Jean-Philippe Bernard   brought up to dat...
215
;IF use_weightning EQ 'likelyhood' THEN weights=exp(-1.d0*chi2s/2.)    ;This is the likelyhood
0bbec005   Jean-Philippe Bernard   improved
216
tot_weights=total(weights)
96d87510   Jean-Philippe Bernard   modified after di...
217
;stop
441c3551   Jean-Philippe Bernard   improved
218
FOR i=0L,Nparams-1 DO BEGIN
934cfd91   Jean-Philippe Bernard   improved custom g...
219
220
221
222
	;weighted_params[i]=total(reform(1.d0*grid_param_values[*,i])*weights)/tot_weights
	;Trying a log approach
  logweighted_param=total(reform(1.d0*alog10(grid_param_values[*,i])*weights))/tot_weights
	weighted_params[i]=10^logweighted_param
441c3551   Jean-Philippe Bernard   improved
223
ENDFOR
33859661   Jean-Philippe Bernard   brought up to dat...
224
225
226
227
228
229
230
231
232
233
;===compute best weighted SED
weighted_sed=fltarr(Nfilters)
FOR i=0L,Nfilters-1 DO BEGIN
	weighted_sed[i]=total(grid_seds[*,i]*weights)/tot_weights
ENDFOR
;=== computed weighted chi2
weighted_chi2=total((sed.stokesI-weighted_sed)^2/sed.sigmaII)
rweighted_chi2=weighted_chi2/Nfreedom
;stop

441c3551   Jean-Philippe Bernard   improved
234

dbf429a6   Jean-Philippe Bernard   improved
235
;stop
849fd436   Jean-Philippe Bernard   improved
236
;==== Check bumping to table edges.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
237
238
params_hit=intarr(Nparams)
FOR i=0L,Nparams-1 DO BEGIN
dbf429a6   Jean-Philippe Bernard   improved
239
240
241
242
243
244
	diff_min=abs(params[i]-table_pmins[i])/table_pmins[i]*100.
	diff_max=abs(params[i]-table_pmaxs[i])/table_pmaxs[i]*100.
	IF diff_min LE 1. THEN params_hit[i]=-1
	IF diff_max LE 1. THEN params_hit[i]=1
  ;IF params[i] LE table_pmins[i] THEN params_hit[i]=-1
  ;IF params[i] GE table_pmaxs[i] THEN params_hit[i]=1
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
245
ENDFOR
4176fb1e   Jean-Philippe Bernard   fixed a bug
246

849fd436   Jean-Philippe Bernard   improved
247
;==== Compute parameter uncertainties
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
248
;stop
f8c3f95f   Jean-Philippe Bernard   added limits and ...
249
conf_level=90./100.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
250
251
252
253
254
255
256
257
258
;print,Nfreedom,conf_level
IF Nfreedom NE 0. THEN BEGIN
	dchi2=delta_chi2(Nfreedom,conf_level)
ENDIF ELSE BEGIN
	message,'Nfreedom = 0. Beware of meaningless fit',/continue
	stop
	message,'seting Nfreedom = 1. Beware of meaningless fit',/continue
  dchi2=delta_chi2(1.,conf_level)
ENDELSE
f8c3f95f   Jean-Philippe Bernard   added limits and ...
259
ind=where(chi2s LE chi2+dchi2,count)
2d100dd8   Jean-Philippe Bernard   fixed another sma...
260
261
params_min=fltarr(Nparams)
params_max=fltarr(Nparams)
f8c3f95f   Jean-Philippe Bernard   added limits and ...
262
FOR i=0L,Nparams-1 DO BEGIN
0d19c43e   Jean-Philippe Bernard   improved
263
264
	params_min[i]=min(grid_param_values[ind[0],i])
	params_max[i]=max(grid_param_values[ind[0],i])
f8c3f95f   Jean-Philippe Bernard   added limits and ...
265
266
ENDFOR
params_uncertainties=params_max-params_min
e62a03b3   Jean-Philippe Bernard   chaaged the plot
267

6ff7d98f   Jean-Philippe Bernard   First commit
268
269
270
IF keyword_set(show_sed) THEN BEGIN
	xtit='Wavelength [mic]'
	ytit='SED (model=red,data=blue)'
e62a03b3   Jean-Philippe Bernard   chaaged the plot
271
	;should also plot uncertainties
ad631268   Jean-Philippe Bernard   chaaged the plot
272
273
274
275
	minv=la_min([sed.stokesI,best_sed])
	maxv=la_max([sed.stokesI,best_sed])
	cgplot,sed.wave,sed.stokesI,psym='Filled Circle',color='blue',/ylog,/ysty,xtit=xtit,ytit=ytit,/xlog,yrange=[minv,maxv]
	cgoplot,sed.wave,best_sed,psym='Filled Square',color='red'
b4dbd43c   Jean-Philippe Bernard   chaaged the plot
276
	if not keyword_set(nostop) THEN stop
6ff7d98f   Jean-Philippe Bernard   First commit
277
278
ENDIF

f12d8052   Jean-Philippe Bernard   chaaged the plot
279
280
the_end:

6ff7d98f   Jean-Philippe Bernard   First commit
281
282
RETURN,params
END