# Chi-squared Goodness of Fit - very small expected values

I am trying to calculate chi-squared value for my fitted data using:

$$\chi^2 = \sum_i^n{\frac{(y-f(x))^2}{f(x)}}$$

where $f(x)$ are theoretical values from fitted function and $y$ are observations.

However, if expected values are too small, I receive (logically) very large numbers. For example if I calculate chi-squared for values that are listed below, I'll receive (in C++): -inf as a result. But statistical software which I use as a reference outputs chi-squared as 0.39.

Please can you suggest what I am doing wrong?

Example data follows:

y:  | f(x):
0  |  0.000233516
0  |  0.000748074
0  |  0.00226688
1  |  0.00649784
1  |  0.0176183
1  |  0.0451873
1  |  0.109628
0  |  0.251586
0  |  0.546141
0  |  1.12145
0  |  2.17825
1  |  4.00215
3  |  6.9556
6  | 11.4349
17  | 17.7821
22  | 26.1572
42  | 36.3961
41  | 47.9043
61  | 59.6417
79  | 70.2394
83  | 78.2468
82  | 82.4535
74  | 82.1877
81  | 77.4925
58  | 69.1145
73  | 58.3087
39  | 46.5322
34  | 35.1261
14  | 25.082
24  | 16.9414
19  | 10.8241
16  |  6.5417
11  |  3.73977
4  |  2.02234
4  |  1.03448
4  |  0.500544
4  |  0.229097
2  |  0.0991863
2  |  0.04062
2  |  0.0157356
1  |  0.00576612
2  |  0.00199866
1  |  0.000655315
0  |  0.000203244
0  |  5.96265e-05
0  |  1.65469e-05
0  |  4.34361e-06
0  |  1.07855e-06
0  |  2.53329e-07
1  |  5.62841e-08

• Neither answer is a correct implementation of your formula, since it's obvious from just the last cell alone that the actual chi-square statistic must exceed $1.776 \times 10^{7}$! It's a little difficult to guess what's wrong with either calculation without seeing what you did in both of them. Jan 29, 2014 at 13:21

The original setting of a chi-squared statistic is that you are comparing observed and expected counts over a set of categories. The statistic is the sum of (observed count $-$ expected count)$^2$ / expected count. In this case, the expected values (whether equivalent to counts or not) are all positive and so the statistic will be positive and finite. However, your result will be dominated by the last line, 1 5.62841e-08: I get the contribution from that to be 17767005.02 and the overall statistic to be 17771474.65, presumably with 50 $-$ 1 $=$ 49 df.
A small discrepancy is that your $y$ and $f(x)$ values don't have the same totals. That would be a requirement for chi-square testing.