Skip to contents

A hierarchy with \(L\) upper levels is said to be balanced if each variable at level \(l\) has at least one child at level \(l+1\). When this doesn't hold, the hierarchy is unbalanced. This function transforms an aggregation matrix of an unbalanced hierarchy into an aggregation matrix of a balanced one. This function is used to reconcile forecasts with cslcc, which operates exclusively with balanced hierarchies.

Usage

balance_hierarchy(agg_mat, nodes = "auto", sparse = TRUE)

Arguments

agg_mat

A (\(n_a \times n_b\)) numeric matrix representing the cross-sectional aggregation matrix. It maps the \(n_b\) bottom-level (free) variables into the \(n_a\) upper (constrained) variables.

nodes

A (\(L \times 1\)) numeric vector indicating the number of variables in each of the upper \(L\) levels of the hierarchy. The default value is the string "auto" which calculates the number of variables in each level.

sparse

Option to return sparse matrices (default is TRUE).

Value

A list containing four elements:

bam

The balanced aggregation matrix.

agg_mat

The input matrix.

nodes

A (\(L \times 1\)) numeric vector indicating the number of variables in each of the \(L\) upper levels of the balanced hierarchy.

id

The identification number of each variable in the balanced hierarchy. It may contains duplicated values.

Examples

#    Unbalanced     ->      Balanced
#        T                     T
#    |-------|             |-------|
#    A       |             A       B
#  |---|     |           |---|     |
# AA   AB    B          AA   AB    BA
A <- matrix(c(1, 1, 1,
              1, 1, 0), 2, byrow = TRUE)
obj <- balance_hierarchy(agg_mat = A, nodes = c(1, 1))
obj$bam
#> 3 x 3 sparse Matrix of class "dgCMatrix"
#>           
#> [1,] 1 1 1
#> [2,] 1 1 .
#> [3,] . . 1