# Testing goodness of fit of data and function

I have a data set which I obtained from experiment e.g.

$$\begin{array}{c|c|c} \text{x} & \text{y} \\ \hline \\1 & 1.01 \\2 & 3.99 \\3 & 8.86 \\4 & 16.02 \end{array}$$

etc. I would like to statistically test if this experimental data fits with a theoretical function including random fluctuation

e.g. $y=2^x$

How would I go about doing this?

-
I think $y=2^x$ might be a better model? – copper.hat Jan 29 '13 at 21:29
@copper.hat: no not really; see $x=3$. – Ron Gordon Jan 29 '13 at 21:34
@rlgordonma: You are right... – copper.hat Jan 29 '13 at 22:14
$y=x^2$ is a far better fit – Ross Millikan Jan 29 '13 at 22:53

You can compute the mean squared error: $$\sqrt{\frac{\sum_{k=1}^n (y_k - 2^x_k)^2}{n}}$$ this gives a measure of the distance of the points from the curve $y=x^{2}$

-
But how would you use the mean squared error to show that the data fits with a 5% significance or something like that? – joshlk Jan 29 '13 at 21:47
You cannot, if you don't fix which is the expected variance of your variable. – Emanuele Paolini Jan 29 '13 at 21:52
Do you mean I need to specify if my error is Gaussian or not? – joshlk Jan 29 '13 at 21:55

If you can assess your experimental errors and model them as a normal distribution with standard deviation $\sigma$ (a dangerous assumption), you can use the chi-squared test. For each data point $(x_i,y_i)$ you calculate the $y_{mi}$ the model predicts. Then sum up $\frac {(y_i-y_{mi})^2}{\sigma^2}$. You expect it to be about the number of data points less the number of parameters in your fit.

Comparing your data to fits $x^2$ and $2^x$ gives

$$\begin{array}{c|c|c} \text{x} & \text{y} & \text {2^x} & \text {2^x error}&\text {x^2}&\text {x^2 error}\\ \hline \\1 & 1.01 &2 &0.99 & 1 & 0.01 \\2 & 3.99 &4 & -0.01 & 4 & -0.01 \\3 & 8.86 &8 & -0.86 & 9 & 0.14 \\4 & 16.02 &16 & 0.02 & 16 & 0.02 \end{array}$$

If we think our experimental error is $\pm 0.1$, the $\chi^2$ of $x^2$ is $\frac {0.01^2+(-0.01)^2+0.14^2+0.02^2}{0.1^2}=\frac {0.0202}{0.01}=2.02$

-