# Least-square fit with uneven distribution of data

I'd like to perform least-squares fit to data which is unevenly distributed on the x-axis.

For example, if I was to bin the data, it would be something like

x = 0~5: 10 data points

x = 5~10: 20 data points

x = 10~15: 2 data points

x = 15~20: 4 data points

I want to fit a best-fit to this to predict future values, but the model of course performs poorly for high values of x.

I can bin the data as above and then assign weights to the data based on the density (sparsity) of values in each bin. These weights can then be used to weight the contribution to the total error by each point, but I wonder: Is this the standard way of dealing with this problem, or is there another method?

Incidentally, I will be implementing this using a python library, so any advice about practical implementation is also appreciated.

Edit: Added a picture for illustration.

Left: Well-distributed points leading to a reasonable estimation for all x.

Right: Points distributed to lower-x values, leading to poor estimation of y at high-x values

• The standard way is to use ordinary least squares with no weights at all. In what sense does this "perform poorly" for the higher values of $x$? (In fact, it will tend to fit those observed values more closely than it will fit the lower values. But that does not mean more poorly.) – whuber Feb 9 '15 at 23:43
• Thanks for the comment! Since there are more points at low x, the total error can be minimised by making the linear trend fit well at low x at the expense of high x, which only have a couple of points and therefore contribute only a little to the total error. If the data were truly linear then unweighted fitting in this case may be fine, but my data is not really linear. So perhaps rather than worrying about weighting, a better step would be to try fitting with a non-linear function? – Gerhard Feb 9 '15 at 23:57
• Also, I wonder if you could clarify further what you mean by "it will tend to fit those [higher x] observed values more closely than it will fit the lower [x] values. But that does not mean more poorly."? (Sorry if this is an elementary question.) – Gerhard Feb 10 '15 at 0:14
• Is X the dependent or independent variable? – robin.datadrivers Feb 10 '15 at 2:06
• x is independent. I've added a picture to make things clearer. – Gerhard Feb 10 '15 at 3:08