I have a set of points $\boldsymbol{p}_i \in \mathbb{R}^d$ ($i=1,\cdots, n$) and I need to perform line fitting. However, some points matter more in this process. More precisely, if we denote with $distance\left(\boldsymbol{p}_i, \ell\right)$ the distance from the $i$-th point to a line $\ell$, I want to find the line that minimizes the cost
$$ f(\ell) = \sum_{i=1}^n w_i \left[distance\left(\boldsymbol{p}_i, \ell\right)\right]^2 $$
wherein $w_i\in\mathbb{R}$ is the non-negative weight associated to the $i$-th point.
My intuition (plus some tinkering with "trivial" cases) told me that the solution can be found using a modified version of the "standard" SVD-based approach. The algorithm would be:
Calculate the barycenter of the points $$\boldsymbol{g} = \frac{\sum w_i\boldsymbol{p}_i}{\sum{w_i}}$$
Build a $n\times d$ matrix $\boldsymbol{A}$ as $$ \boldsymbol{A} = \begin{bmatrix} \sqrt{w_1}\left(\boldsymbol{p}_1 - \boldsymbol{g}\right) \\ \cdots \\ \sqrt{w_n}\left(\boldsymbol{p}_n - \boldsymbol{g}\right) \end{bmatrix} $$ (what I mean is that the vectors $\sqrt{w_i}\left(\boldsymbol{p}_i - \boldsymbol{g}\right)$ are the rows of the matrix)
Perform a SVD, $\boldsymbol{A} = \boldsymbol{U}\boldsymbol{S}\boldsymbol{V}^*$
The best-fitting line is the one passing through $\boldsymbol{g}$ and having $\boldsymbol{v_1}$ (first row of $\boldsymbol{V}$) as direction
Can someone confirm that the result is true in all dimensions and possibly provide a proof for it?
For those interested, the analytical solution in the 2D-case can be obtained by considering the generic 2D line given by the equation $$ x \sin\theta - y \cos\theta + \rho = 0 $$ for which the cost function writes as $$ f(\theta, \rho) = \sum w_i\left(x_i \sin\theta - y_i \cos\theta + \rho\right)^2 $$
After solving $\frac{\partial f}{\partial \theta} = 0$ and $\frac{\partial f}{\partial \rho} = 0$ one finds the four candidates: $$ \begin{cases} \theta_{j} = \frac{1}{2}\arctan\left(\frac{2\mu_{11}}{\mu_{20}-\mu_{02}}\right) + \frac{j\pi}{2} \\ \rho_j = - x_g\sin\theta_j + y_g\cos\theta_j \end{cases} $$
for $j=0,1,2,3$, with $(x_g, y_g)$ the barycenter of the points and $\mu_{pq} = \sum_{i=1}^n w_i (x_i-x_g)^p (y_i-y_g)^q$ (centered moments). Of these solutions, the pairs $(0, 2)$ and $(1,3)$ correspond to the same lines. Furthermore, between these two lines one is the best fitting we are looking for, while the other is the worst fitting passing through the barycenter.
I verified using a PC that the line obtained by the "weighted SVD" is the same as the analytical solution for a lot of random points and weights. Not a proof, but enough to convince me that the result is likely true.