This project has moved. For the latest updates, please go here.

Recording Uncertain Measurements

A very common kind of experiment in the physical sciences is to record a measurement, with an error bar, for each value of a dependent variable as we change an independent variable. For example, we might measure the intensity of scattered light as a function of angle, or the concentration of a chemical in a reaction chamber as a function of time. These kinds of experimental data can be recorded and analyzed using the UncertainMeasurmementSample classes.

// Create a new sample and add some vaguely oscilatory data
UncertainMeasurementSample data = new UncertainMeasurementSample();
data.Add(2.0,2.5,0.5);
data.Add(3.0,2.0,1.0);
data.Add(5.0,-1.0,0.5);
data.Add(8.0,-4.0,0.5);
data.Add(10.0,-1.5,0.5);
data.Add(12.0, 0.0, 1.0);

You can remove one or all data points from your set using the Remove or Clear methods.

Fitting

You fit your data to various model forms. For example, the following code computes the line that best fits the data.

// fit a line to the data
FitResult line = data.FitToLine();

// write out the best-fit parameters (with uncertainties) and the chi-suqared value
Console.WriteLine("intercept = {0}", line.Parameter(0));
Console.WriteLine("slope = {0}", line.Parameter(1));
Console.WriteLine("chi-squared = {0}", line.GoodnessOfFit.Statistic);

UncertainMeasurementSample defines methods for fitting data to a constant (FitToConstant), a proportionality (FitToProportionality), a polynomial of arbitrary degree (FitToPolynomial), or an arbitrary user-defined function (FitToFunction).

The following sample code defines a 3-parameter oscilatory model function and fits the data to it.

// Define an oscilatory model
// a[0] ~ amplitude, a[1] ~ period, a[2] ~ phase
Func<double[], double, double> model = (double[] a, double x) => a[0] * Math.Sin(2.0 * Math.PI * x / a[1] + a[2]);

// Fit the model to the data, providing an initial guess for the parameters
FitResult oscilation = data.FitToFunction(model, new double[] { 2.0, 10.0, 0.0 });

// Print the results
for (int i = 0; i < oscilation.Dimension; i++) {
  Console.WriteLine(oscilation.Parameter(i));
}

If your model is a linear function of the model parameters, you can use the FitToLinearFunction method for a faster fit.

Multivariate and other independent variables

Sometimes the independent variable does not consist of a single number. For example, you may have experimental data on the temperature of oceanwater around the globe, in which case the indepdent variable is the latitude and longitude of each measurement, a pair of numbers. Your indepdent variable may even been some complicated non-numeric object such as a date, and enumeration value, and a matrix. In these cases, you should use the generic UncertainMeasurementSample<T> class, with T set to the type of your independent variable. Methods such as FitToLine will not be available to you, since they depend on the indepdent variable being a single real number. But the FitToFunction method is still available to you, as long as you can express your model via a Func<double[],T,double> that maps a given T to a predicted value for a given set of parameters.

Data Without Uncertainties

If you do not have uncertainties for you independent variable measurements, but instead need to impute them from the data, you can do so by setting all uncertainties to the same aribtrary value, say 1.0. The computed parameter values will be correct. The computed uncertainties should be scaled by by the chi-square value. The chi-square test will no longer constitute an independent test of the quality of your fit. Alternatively, you can use the regression methods of the BivariateSample and MultivariateSample classes, which may be more appropriate to your data.

Last edited Sep 10, 2012 at 9:30 AM by ichbin, version 4

Comments

No comments yet.