I have the following weighted residuals sum of squares (WRSS)
$$ \sum_{t=1}^n \sum_{i=1}^n \left(y_i - \boldsymbol{x}_i^T \hat{\boldsymbol{\beta}_t} \right)^2 (z_t - z_i) $$
where $y_i \in \mathbb{R}^{1 \times 1}$, $(z_t - z_i) \in \mathbb{R}^{1\times1}$, $\boldsymbol{x}_i \in \mathbb{R}^{p\times1}$, $\boldsymbol{\hat{\beta}}_t \in \mathbb{R}^{p\times1}$. I want to put it in matrix form to increase my computation speed (i.e., avoid for loops). I did the following
$$ \sum_{t=1}^n \left(\boldsymbol{y} - \boldsymbol{X} \boldsymbol{\hat{\beta}}_t \right)^T \mbox{diag} \left( z_t - z_1, \ldots, z_t - z_n \right) \left(\boldsymbol{y} - \boldsymbol{X} \boldsymbol{\hat{\beta}}_t \right) $$
I am not quite sure how to get rid of the summation over $t$. One suggestion
$$ \sum_{t=1}^n \left(\boldsymbol{y} - \boldsymbol{X} \boldsymbol{\hat{\beta}}_t \right)^T \left(\boldsymbol{I}_n z_t - \mbox{diag} (Z) \right) \left(\boldsymbol{y} - \boldsymbol{X} \boldsymbol{\hat{\beta}}_t \right) $$
Then separate the terms with $t$. I work this out but I could not get anything useful.
$$ \sum_{t=1}^n \left[ \boldsymbol{y}^T\big(\boldsymbol{I}_n z_t - \mbox{diag}(Z)\big)\boldsymbol{y} + \boldsymbol{\hat{\beta}}_t^T \boldsymbol{X}^T \big(\mbox{diag}(Z)-\boldsymbol{I}_n z_t\big) \boldsymbol{y} + \boldsymbol{y}^T \big(\mbox{diag}(Z)-\boldsymbol{I}_n z_t\big) \boldsymbol{X} \boldsymbol{\hat{\beta}}_t + \boldsymbol{\hat{\beta}}_t^T \boldsymbol{X}^T \big(\boldsymbol{I}_n z_t - \mbox{diag}(Z)\big) \boldsymbol{X}\boldsymbol{\hat{\beta}}_t\right] $$
The main goal for me is to avoid the for loops at all costs. Your help is appreciated!!
$ \def\bbR#1{{\mathbb R}^{#1}} \def\o{{\tt1}}\def\b{\beta} \def\L{\left}\def\R{\right}\def\LR#1{\L(#1\R)} \def\trace#1{\operatorname{Tr}\LR{#1}} \def\m#1{\left[\begin{array}{r}#1\end{array}\right]} $Collect the indexed vectors into matrices $$\eqalign{ B &= \m{\b_1&\b_2&\ldots&\b_n}\; &\in\bbR{p\times n} \\ X &= \m{x_1&x_2&\ldots&x_n} &\in\bbR{p\times n} \\ }$$ Use these and the all-ones vector $\o\in\bbR{n\times\o}$ to create the matrices
$$\eqalign{ C &= y\o^T-X^TB \;\;&\in\bbR{n\times n} \\ Q &= C\odot C &\in\bbR{n\times n} \\ }$$ where $(\odot)$ is the elementwise/Hadamard product.
Finally, the sum of squares can be written as $$\eqalign{ \LR{\o^TQz-z^TQ\o} \;=\; \o^T\big(Q-Q^T\big)z \\ }$$