Least Squares

From Tauwiki

Jump to: navigation, search

Contents

[edit] Least Squares

[edit] Overview

A program to calculate the best fit slope and offset for a linear fit to data. This program should inherit much of its look and feel from Interpolate.

[edit] Algorithm

[edit] Math

y = ax + b

we have to minimize the χ2 where

\chi^{2}={(\sum\frac{\Delta y_{i}}{\sigma_{i}})}^{2}

To do this we take the partial derivatives with respect to a and to b and are left with

b=\frac{1}{\Delta}(\sum\frac{x_{i}^{2}}{\sigma_{i}^{2}}\sum\frac{y_{i}}{\sigma_{i}^{2}}-\sum\frac{x_{i}}{\sigma_{i}^{2}}\sum\frac{x_{i}y_{i}}{\sigma_{i}^{2}})
a=\frac{1}{\Delta}(\sum\frac{1}{\sigma_{i}^{2}}\sum\frac{x_{i}y_{i}}{\sigma_{i}^{2}}-\sum\frac{x_{i}}{\sigma_{i}^{2}}\sum\frac{y_{i}}{\sigma_{i}^{2}})
\Delta=\sum\frac{1}{\sigma_{i}^{2}}\sum\frac{x_{i}^{2}}{\sigma_{i}^{2}}-{(\sum\frac{x_{i}}{\sigma_{i}^{2}})}^{2}

[edit] Programming

  1. Read x, y, and sigma from a file.
    1. Note that the file may have an arbitrary structure. The input format should make use of the methods developed for interpolate.
  2. Calculate a, b as outlined above.
  3. Print a, b, and χ2.
CSS 2.1 Xhtml 1.0 Last Modified: February 22, 2007 GooglePagerank