Linear regression

In statistics, linear regression is an approach for modeling the relationship between a scalar dependent variable y and one or more explanatory variables (or independent variable) denoted X. The case of one explanatory variable is called simple linear regression. For more than one explanatory variable, the process is called multiple linear regression.

SCaVis offers many reach classes for linear and non-linear regressions.
To perform a linear regression, use the class LinReg. The example below shows
how to use this class to perform the linear regression:

Linear regression with HFitter

Now we can do a bit more flexible fit defining a linear function analytically.
We will use HFitter which allows to define any fit function.
We simulate a linear dependence of the variable Y on X using random numbers:

Non-linear fitting

Data fits can be done by using the Java class
HFitter.
But before, one needs to construct a function and then set initial values
for free parameters. Fitting can be done either in the interactive mode or using
a script.

By default, HFitter fits data using chi2 methods. It is important to specify errors on Y-values of input data. You can print the
available methods as:

from jhplot import *
f=HFitter()print f.getFitMethod()

which prints “leastsquares”, “cleverchi2”, “chi2”, “bml” fitting methods. You can set the fit method when initialize the fitter:

from jhplot import *
f=HFitter("leastsquares")

In this case, instead “chi2”, “leastsquares” fit method is used. In this case, you can fit “X-Y” data arrays without specifying errors on Y.

Let's make a simple chi2 fit of experimental data using an analytic function <m>Tu + (Ta - Tu) * exp(-kk * x)</m>,
where parameters Tu, Ta and kk need to be determined by fitting data stored as array. The data has experimental (statistical or
systematic) uncertainties. This is mandatory for chi2 minimization.
The resulting fit is shown below (example is provided by Klaus Rohe).

This image is generated by the code given below where we use P1D container to store input data.
You can access fit errors and the fit quality (chi2/ndf) as described by the HFitter class.

Unregistered users have a limited access to this section.
You can unlock advanced pages after becoming a full member.

Let use somewhat different approach and fit a Gaussian using the chi2 fit:

The code which performs this fit is shown below:

Unregistered users have a limited access to this section.
You can unlock advanced pages after becoming a full member.

Below we will illustrate how to perform a rather complicated fit using the chi2 method.
The fit will be done in several steps.
In this example we fit data which can be described by multiple Gaussians, in which next Gaussians fit takes the values from the previous fit:

Numerical interpolation

One can use numerical way to describe data using interpolation and smoothing
One example is shown on this figure:

where we attempted to smooth data using a non-analytical approach. Such approach is often considered for various predictions when to find an appropriate analytical function is
difficult or impossible. SCaVis provides a several flexible methods to smooth data and perform interpolation. The code of this example is given below:

Unregistered users have a limited access to this section.
You can unlock advanced pages after becoming a full member.

Interactive fit

Data can be fitted using many predefined functions in an more interactive way.
Let's create a few data containers (1D array, 1D histograms) and start HPlotJas plotter based on JAS2 program from Slac:

This will bring up a “JAS” program with all objects shown on the left side.

You can expand the tree (left) and click on each object to plot on the canvas. Then try to fit the data: using the mouse pop-up dialog, press “Add function” (for example, a Gaussian function) and click on “Fit”. The program will perform chi2 minimisation and you will see a Gaussian line on to of the data. You can adjust the initial values of the function by dragging 3 points of this function.

In the above example we used pre-built functions to perform fits. You can add your own custom fit function to the menu and use it
for fitting as well. You can do this directly in the Jython script and pass this function to HplotJas canvas.
Below we show an example in which we create 2 custom functions (a new Gaussian and a parabola) and passed them to the HPlotJas for interactive fitting.

Unregistered users have a limited access to this section.
You can unlock advanced pages after becoming a full member.

Advanced fitting of data

Section Advanced fitting discusses how to
perform non-linear fit in Java using data in many dimensions and using
any complex function that can be defined
not as a string, but totally programically.