# Dynamic Model Averaging in Economics and Finance with fDMA: A Package for R
## 3 Theoretical Framework
### Dynamic Model Averaging (DMA)
Suppose that the conditional distribution of the state in time \(t-1\) knowing the data up to time \(t-1\), i.e., \(Y^{t-1}=\{y_{1},\ldots,y_{t-1}\}\), is known from \(p(\theta_{t-1},L_{t-1}|Y^{t-1})=\sum_{k=1}^{K}[p(\theta_{t-1}^{(k)}|L_{t-1}=k,Y ^{t-1})p(L_{t-1}=k|Y^{t-1})]\), where the conditional distribution of \(\theta_{t-1}^{(k)}\) is approximated by \(\theta_{t-1}^{(k)}|L_{t-1}=k,Y^{t-1}\sim N(\hat{\theta}_{t-1}^{(k)},E_{t-1}^{( k)})\). Two steps are used in the approximation. First, \(L_{t}\) is predicted. Next, \(\theta_{t}^{(k)}|L_{t}\) is predicted. Let \(\pi_{t-1|t-1,k}=p[L_{t-1}=l|Y^{t-1}]\). Then \(\pi_{t|t-1,k}=\sum_{l=1}^{K}\pi_{t-1|t-1,l}p_{k,l}\), where \(\pi_{t|t-1,k}=p[L_{t}=k|Y^{t-1}]\). However, the approximation can be used, i.e.,  
\[\pi_{t|t-1,k}\approx\frac{(\pi_{t-1|t-1,k})^{\alpha}}{\sum_{l=1}^{K}(\pi_{t- 1|t-1,l})^{\alpha}} \tag{2}\]  
where \(\alpha\) is a _forgetting factor_, i.e., a fixed number between 0 and 1. Further, \(\pi_{t|t-1,k}\) are called _posterior probabilities_. During the numerical approximations, zeros can emerge in Equation (2). Therefore, following [1], in the package Equation (2) is replaced by  
\[\pi_{t|t-1,k}=\frac{(\pi_{t-1|t-1,k})^{\alpha}+c}{\sum_{l=1}^{K}\left((\pi_{t -1|t-1,l})^{\alpha}+c\right)}\quad, \tag{3}\]  
where \(c=0.001\cdot 2^{m}\). Next, \(\theta_{t}^{(k)}|L_{t}=k,Y^{t-1}\sim\mathcal{N}(\hat{\theta}_{t-1}^{(k)},E_{t -1}^{(k)}+W_{t}^{(k)})\), where  
\[E_{t-1}^{(k)}+W_{t}^{(k)}\approx E_{t-1}^{(k)}\cdot\lambda^{-1}\quad, \tag{4}\]  
where \(\lambda\) is the next _forgetting factor_ between 0 and 1. Additionally, \(\pi_{t|t,k}=p[L_{t}=l|Y^{t}]\) can be computed in the following way  
\[\pi_{t|t,k}=\frac{\pi_{t|t-1,k}f_{k}(y_{t}|Y^{t-1})}{\sum_{l=1}^{K}\pi_{t|t-1, l}f_{l}(y_{t}|Y^{t-1})}\quad, \tag{5}\]where \(f_{k}(y_{t}|Y^{t-1})\) is the predictive density of \(k\)-th model at \(y_{t}\). But this is given as the density of  
\[\mathcal{N}((x_{t}^{(k)})^{\top}\hat{\theta}_{t-1}^{(k)},V_{t}^{(k)}+(x_{t}^{(k )})^{\top}E_{t-1}^{(k)}\lambda^{-1}x_{t}^{(k)})\quad. \tag{6}\]  
Variance \(V_{t}^{(k)}\) is updated by the recursive moment estimation, i.e.,  
\[V_{t}^{(k)}=(1-t)^{-1}\hat{V}_{t-1}^{(k)}+t^{-1}[(e_{t}^{(k)})^{2}-(x_{t}^{(k) })^{\top}E_{t-1}^{(k)}\lambda^{-1}x_{t}^{(k)}]\quad, \tag{7}\]  
where  
\[e_{t}^{(k)}=y_{t}-(x_{t}^{(k)})^{\top}\hat{\theta}_{t-1}^{(k)} \tag{8}\]  
is the error term from the one-ahead prediction of \(k\)-th model. If the updated \(V_{t}^{(k)}\leq 0\), then it is taken \(V_{t}^{(k)}=\hat{V}_{t-1}^{(k)}\). Moreover, with respect to \(\theta_{t}^{(k)}|L_{t}=k\) and \(Y^{t}\sim\mathcal{N}(\hat{\theta}_{t}^{(k)},E_{t}^{(k)})\) regression coefficients are updated in the following way  
\[y_{t}=(x_{t}^{(k)})^{\top}\theta_{t}^{(k)}+\epsilon_{t}^{(k)}\quad, \tag{1}\]0  
On the other hand, \(E_{t}^{(k)}\) is updated in the following way  
\[y_{t}=(x_{t}^{(k)})^{\top}\theta_{t}^{(k)}+\epsilon_{t}^{(k)}\quad, \tag{1}\]1  
In order to set the above recursive computations some initial values have to be set. Assuming that initially all \(K\) models are equally probable (i.e., setting the non-informative prior) the below formula is given  
\[y_{t}=(x_{t}^{(k)})^{\top}\theta_{t}^{(k)}+\epsilon_{t}^{(k)}\quad, \tag{1}\]2  
for every \(k=1,\ldots,K\), and the vectors of regression coefficients are initially set to zeros, i.e.,  
\[y_{t}=(x_{t}^{(k)})^{\top}\theta_{t}^{(k)}+\epsilon_{t}^{(k)}\quad, \tag{1}\]3  
Moreover,  
\[E_{0}^{(k)}=\left[ \begin{array}{cccc}\beta^{2}+\text{Var}(y)&0&\ldots&0\\ 0&\frac{\text{Var}(y)}{\text{Var}(x_{1}^{(k)})}&\ldots&0\\ \vdots&\vdots&\ddots&0\\ 0&\ldots&\ldots&\frac{\text{Var}(y)}{\text{Var}(x_{j_{k}}^{(k)})}\end{array}  \right]\quad, \tag{13}\]  
where \(j_{k}\) is the number of dependent variables in \(k\)-th model and \(\beta\) is the estimated intercept term in a linear regression model with \(y\) as the dependent variable and whole \(x\) as the independent variables. It can happen during the numerical estimations that some \(\text{Var}(x_{i}^{(k)})\) will be approximated to 0.