1. The (more) correct definition of the $\widehat{a_i}$'s should be $$\widehat{a_{n,i}}:=\frac1n\,\sum_{j=1}^n g_i(X_j).$$ 2. So, $$\widehat{a_{n,i}}=\int_{\mathbb R}\mu_n(t) g_i(t)\,dt,$$ where $$\mu_n(t):=\frac1n\,\sum_{j=1}^n \delta_{X_j}(t)$$ and $\delta_x$ is the Dirac probability measure at $x$, viewed as the (say) [Schwartz distribution][1]. So, the $\widehat{a_{n,i}}$'s may be viewed as the "coordinates" of the Schwartz distribution $\mu_n$ in the basis $(g_i)$ of $L_2(\mathbb R)$. So, if we had $\sum_i \widehat{a_{n,i}}^2<\infty$, we would also have $\mu_n\in L_2(\mathbb R)$, which is of course false. So, $\sum_i \widehat{a_{n,i}}^2=\infty$ for any basis $(g_i)$ of $L_2(\mathbb R)$, and hence $\sum_i (a_i-\widehat{a_{n,i}})^2=\infty$ for any real $a_i$'s such that $\sum_i a_i^2<\infty$. 3. Another view at why your idea of estimation of the density $f$ did not succeed is that the sequence $(\mu_n)$ of the [(empirical) probability measures][2] converges to the probability measure (say $\mu$) with density $f$ only weakly, and the probability measure $\mu_n$ does not even have a density, to converge to $f$ in any sense or to do anything else. 4. Generally, it appears unnatural to estimate a pdf in an $L^2$ framework. The natural framework should be $L^1$. This is the view advocated (I think persuasively) by some authors, including [Devroye and Gyorfi][3], who write that their book "develops, from first principles, the ``natural'' theory for density estimation, L1, and shows why the classical L2 theory masks some fundamental properties of density estimates". [1]: https://en.wikipedia.org/wiki/Distribution_(mathematics) [2]: https://en.wikipedia.org/wiki/Empirical_measure [3]: https://www.google.com/books/edition/Nonparametric_Density_Estimation/ZVALbrjGpCoC?hl=en