Wenn Sie das Produkt X und Y mit sqrt (Gewicht) Sie gewichteten kleinsten Quadrate berechnen kann. Sie können die Formel durch folgenden Link erhalten:
http://en.wikipedia.org/wiki/Linear_least_squares_%28mathematics%29#Weighted_linear_least_squares
hier ein Beispiel:
Daten vorbereiten:
import numpy as np
np.random.seed(0)
N = 20
X = np.random.rand(N, 3)
w = np.array([1.0, 2.0, 3.0])
y = np.dot(X, w) + np.random.rand(N) * 0.1
OLS:
from scipy import linalg
w1 = linalg.lstsq(X, y)[0]
print w1
Ausgabe:
[ 0.98561405 2.0275357 3.05930664]
WLS:
weights = np.linspace(1, 2, N)
Xw = X * np.sqrt(weights)[:, None]
yw = y * np.sqrt(weights)
print linalg.lstsq(Xw, yw)[0]
Ausgang:
[ 0.98799029 2.02599521 3.0623824 ]
Prüfergebnis von statsmodels:
import statsmodels.api as sm
mod_wls = sm.WLS(y, X, weights=weights)
res = mod_wls.fit()
print res.params
Ausgang:
[ 0.98799029 2.02599521 3.0623824 ]