Matrixes and a Ruby Method of Least Squares Implementation
I’ve been working on a little statistics side project for a while now. I’ve built a simple rails app which tracks the value of several pension funds, based on data it pulls from different websites.
Anyway, the app stores daily values for fund value. Here is a simple example of the data it’s storing:
1 2 3 4 5 6 

I thought it would be pretty cool to generate some statistics for performance over the previous week. To do this, we can look at standard statistics such as standard deviation, sample variance, and advanced statistics like trends.
Variance and Standard Deviation
Variance is handy because it tells you how how disparate your data is from the standard deviation. It creates a statistical measure so you can work out if a pension fund is fairly stable, or tends to fluctuate wildly.
A higher variance means the data is more variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 

Calculating standard deviation is just a case of square rooting the sample variance:
1 2 3 

Trends (Least Squares method)
Wouldn’t it be handy to know whether the fund is generally trending upwards, or downwards? For this, we need some more advanced measures. One popular method is the Method of Least Squares.
Broadly, this formula tries to produce a trend line by calculating the best line which has the least gap between the trend line and the points. I won’t delve in the details for the formula here, rather present my approach to accomplishing this in Ruby.
I’m not a computer scientist; I wasn’t familiar with the concept of a Matrix. They can be used to solve a simultaneous equation. I followed the (very simple!) math on this page, which yields these two equations:
This can be represented as:
To get a and b, you can provide the first matrix and the desired solution matrix:
1 2 3 4 

And here is how we derive these values, based on the input data above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 

Working example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
