| Home | Trees | Indices | Help |
|
|---|
|
|
An assembly is a collection of fitting functions. This provides the model representation that is the basis of the park fitting engine.
Models can range from very simple one dimensional theory functions to complex assemblies of multidimensional datasets from different experimental techniques, each with their own theory function and a common underlying physical model.
First define the models you want to work with. In the example below we will use an example of a simple multilayer system measured by specular reflection of xrays and neutrons. The gold depth is the only fitting parameter, ranging from 10-30 A. The interface depths are tied together using expressions. In this case the expression is a simple copy, but any standard math functions can be used. Some model developers may provide additional functions for use with the expression.
Example models:
import reflectometry.model1d as refl
xray = refl.model('xray')
xray.incident('Air',rho=0)
xray.interface('iAu',sigma=5)
xray.layer('Au',rho=124.68,depth=[10,30])
xray.interface('iSi',sigma=5)
xray.substrate('Si',rho=20.07)
datax = refl.data('xray.dat')
neutron = refl.model('neutron')
neutron.incident('Air',rho=0)
neutron.interface('iAu',sigma='xray.iAu')
neutron.layer('Au',rho=4.66,depth='xray.Au.depth')
neutron.interface('iSi',sigma='xray.iSi')
neutron.substrate('Si',rho=2.07)
datan = refl.data('neutron.dat')
As you can see from the above, parameters can be set to a value if the parameter is fixed, to a range if the parametemr is fitted, or to a string expression if the parameter is calculated from other parameters. See park.Parameter.set for further details.
Having constructed the models, we can now create an assembly:
import park assembly = park.Assembly([(xray,datax), (neutron,datan)])
Note: this would normally be done in the context of a fit using fit = park.Fit([(xray,datax), (neutron,datan)]), and later referenced using fit.assembly.
Individual parts of the assembly are accessable using the model number 0, 1, 2... or by the model name. In the above, assembly[0] and assembly['xray'] refer to the same model. Assemblies have insert and append functions for adding new models, and "del model[idx]" for removing them.
Once the assembly is created computing the values for the system is a matter of calling:
assembly.eval() print "Chi**2",assembly.chisq print "Reduced chi**2",assembly.chisq/assembly.degrees_of_freedom plot(arange(len(assembly.residuals)), assembly.residuals)
This defines the attributes residuals, degrees_of_freedom and chisq, which is what the optimizer uses as the cost function to minimize.
assembly.eval uses the current values for the parameters in the individual models. These parameters can be changed directly in the model. In the reflectometry example above, you could set the gold thickness using xray.layer.Au.depth=156, or something similar (the details are model specific). Parameters can also be changed through the assembly parameter set. In the same example, this would be assembly.parameterset['xray']['Au']['depth']. See parameter set for details.
In the process of modeling data, particularly with multiple datasets, you will sometimes want to temporarily ignore how well one of the datasets matches so that you can more quickly refine the model for the other datasets, or see how particular models are influencing the fit. To temporarily ignore the xray data in the example above use:
assembly.parts[0].isfitted = False
The model itself isn't ignored since its parameters may be needed to compute the parameters for other models. To reenable checking against the xray data, you would assign a True value instead. More subtle weighting of the models can be controlled using assembly.parts[idx].weight, but see below for a note on model weighting.
Changing the weight is equivalent to scaling the error bars on the given model by a factor of weight/n where n is the number of data points. It is better to set the correct error bars on your data in the first place than to adjust the weights. If you have the correct error bars, then you should expect roughly 2/3 of the data points to lie within one error bar of the theory curve. If consecutive data points have largely overlapping errorbars, then your uncertainty is overestimated.
Another case where weights are adjusted (abused?) is to compensate for systematic errors in the data by forcing the errorbars to be large enough to cover the systematic bias. This is a poor approach to the problem. A better strategy is to capture the systematic effects in the model, and treat the measurement of the independent variable as an additional data point in the fit. This is still not statistically sound as there is likely to be a large correlation between the uncertainty of the measurement and the values of all the other variables.
That said, adjusting the weight on a dataset is a quick way of reducing its influence on the entire fit. Please use it with care.
| Classes | |
|
Fitness Container for theory and data. |
|
|
Assembly Collection of fit models. |
|
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Mar 16 15:03:12 2009 | http://epydoc.sourceforge.net |