# RILYBOT 4: Linear Regression

This description covers just one motor and the light sensor. The same technique is used for the other inputs and outputs.

During training mode the robot continually reads the motor speed and the light level. Each reading generates two values, and these values can be treated as coordinates of a point on a two-dimensional graph. After a few seconds of collecting data, such a graph might look like this:

light | . | . ' | . ' | : ' . | .. | ' .' | ' | . . '. | . ' | +--------------------------- motorIn this example the motor speed goes down as the light goes up. The set of data can be approximated by a straight line, and the common technique called linear regression is used to compute the coefficients of a polynomial that gives the output value as a function of the input value. The line is selected so as to minimize the chi-squared measure, which is the sum of the squares of the errors in the output values that would result from using the line as a substitute for the real data.

The chi-squared linear regression method can compute the coefficients of the line from the following five sums:

- the number of data points n

- the sum of the motor readings M_{i}

- the sum of the light readings L_{i}

- the sum of the squares of the light readings L_{i}^{2}

- the sum of the products of the two readings L_{i}^{.}M_{i}

These five sums are all that is required, no matter how many data points are taken. This means that it is not necessary to store multiple data values during training mode. The only concern that limits the number of data points that can be taken is the maximum value of the sum.

Unfortunately, in NQC on the RCX, variables are limited to the range [-32768, 32767]. I could have implemented some multi-precision math (shift and add is all you need) to compute intermediate results and get an acceptable answer, but I decided it wasn't worth the effort.

This page was written in the "embarrassingly readable" markup language RHTF, and was last updated on 2014 Dec 11. s.27