プログラムの中で下記の例のδの値を変えるには、least_sq.hのDELTAの値を変えてください。
To change the value of δ in the example below, please change the value of DELTA in least_sq.h
Y = X Aという行列式において、YとXが分かっている状態で、Aを推定する問題を考えます。このときに力を発揮するのが最小二乗法です。右に示す正規方程式XTX A=XTYと逆行列を使う最小二乗法は、あまり勧められていません。その理由は、簡単に言うと、(XTX)-1を適切に求めることが難しい場合があるからです。そこで、QR分解を使う最小二乗法が勧められています。
In the determinant Y = X A, we consider the problem of estimating A when Y and X are known. The least square method is a powerful tool for this case. The least square method, which uses the normal equation XTX A=XTY and the inverse matrix shown on the right, is not recommended. The reason for this is simply that there are cases where it is difficult to calculate (XTX)-1 appropriately. Therefore, the least square method using QR decomposition is recommended.
左の例に示すY = X Aを考えます。Xは3行2列の行列です。XTXの行列を見ると、δの値がゼロに近いと1+δ2はほぼ1となり、XTXは特異行列に非常に近くなります。特異行列とは、逆行列が存在しない行列のことです。そんな行列があるときは、正規方程式と逆行列を用いて最小二乗法を行うことが難しくなります。
ここでは左に示す簡単な例を使いながら、正規方程式と逆行列を使う最小二乗法で得られる結果とQR分解を使う最小二乗法で得られる結果を比較して、どんなことが起こるのかを見てみます。
Consider Y = X A as shown in the example on the left. X is a matrix with 3 rows and 2 columns. We can find that the value of 1+δ2 in the XTX matrix is approximately 1, when the value of δ is close to zero. This means that XTX is very close to a singular matrix. A singular matrix is a matrix whose inverse does not exist. When such matrices are present, it becomes difficult to perform the least square method using the normal equation and the inverse matrix.
Using the simple example shown on the left, we will compare the results obtained by the least square method using normal equation and inverse matrix with those obtained by the least square method using QR decomposition, and see what happens.
右の表は、上記のYとXと色々なδ の値を用いて、推定したAの値をまとめたものです。小数点以下最大で10桁までの結果を示します。正しいAの値は、1.0と2.0です。
正規方程式と逆行列を用いた最小二乗法によって推定されたAの値は、δの値が10から10-3までは正しい値が推定されています。しかし、δの値が10-3より小さな値になるにつれ、Aの値は正しい値から少しずつズレていき、10-8になったとき異常に大きな値となり、それ以降はどんどん小さな値になっていきます。一方で、QR分解を用いた最小二乗法では、全てのδの値で正しい値が求められています。
実際に最小二乗法を使う場面で、ここに示したような場面がどのくらいあるか分かりませんが、この結果を見るとQR分解を用いた最小二乗法を使った方が安心できそうです。
The table on the right shows the results of estimating the value of A using Y, X and various values of δ, where the results are shown to a maximum of 10 decimal places and the correct values of A are 1.0 and 2.0.
The values of A estimated by the least square method using the normal equation and the inverse matrix are correct for the value of δ from 10 to 10-3, but deviates from the correct value for smaller values of δ. The value becomes abnormally large at 10-8, after which the values become smaller and smaller. On the other hand, the least square method using the QR decomposition finds the correct value for all values of δ.
Although I do not know how often such a case occurs in actual situations where the least squares method is used, the result indicates that it would be safer to use the least square method with QR decomposition.