Scale covariance

A cover is useful as a set of natural scales only if it does not depend on the units in which the inputs are expressed. This page states when the covers computed by this package have this property.

Covariance

Rescaling the inputs, x → D*x for a positive diagonal D, changes the Hessian to inv(D)*H*inv(D). Rescaling the output, f → c*f with c > 0, changes it to c*H. A Hessian cover is covariant if it transforms the same way: h → inv(D)*h and h → sqrt(c)*h. For a Jacobian cover, rescaling the inputs by D and the outputs by C should give b → inv(D)*b and a → C*a, up to the scalar factor that a Jacobian cover leaves free.

In the following example, the program g is f with its inputs expressed in different units:

julia> using DerivativeCovers

julia> f(x) = x[1] * x[2] + exp(x[2]);

julia> x0 = [2.0, 0.5];

julia> D = [10.0, 0.1];

julia> g(y) = f(y ./ D);

julia> cover_hessian(g, D .* x0).h ≈ cover_hessian(f, x0).h ./ D
true

julia> cover_hessian(x -> 9 * f(x), x0).h ≈ 3 * cover_hessian(f, x0).h
true

The covers of this package are covariant on any program in which every operation can choose its local cover from the values of its arguments. The rest of this page explains what this means and what happens otherwise.

Choosing among equally valid local covers

The product x * y has second partial derivatives [0 1; 1 0]. Any pair (η₁, η₂) with η₁ * η₂ ≥ 1 covers this matrix, so the operation has a one-parameter family of equally valid local covers, and the choice determines how its contribution is distributed between x and y. A choice based on a fixed number, such as η₁ = η₂ = 1, would not be covariant: if x is measured in millimeters instead of meters, its share of the cover should change by a factor of 1000.

The package resolves the choice using the magnitudes of the arguments. For a product, it chooses η₁ = sqrt(|y| / |x|) and η₂ = sqrt(|x| / |y|), which transform correctly under any rescaling of x and y. In the example above, x0 = [2.0, 0.5] gives η₁ = 0.5 and η₂ = 2; the second entry of the cover adds sqrt(exp(0.5)) ≈ 1.284 for the exp term:

julia> cover_hessian(f, x0).h
2-element Vector{Float64}:
 0.5
 3.2840254166877414

Other operations with a family of local covers, such as division and powers with a variable exponent, are resolved in the same way. This choice is made by split_block.

When argument values carry no scale

The rule above fails when an argument is zero: η₂ = sqrt(|x| / |y|) is infinite at y = 0, and no choice based on the argument values is both finite and covariant. At such operations the package uses declared scales for the inputs instead. The scales keyword, accepted by every cover function, supplies one positive scale per input; the default, nothing, sets them all to 1, which amounts to asserting that the inputs share units. Every cover reports, in its nguarded field, the number of operations at which it used the declared scales:

julia> x1 = [2.0, 0.0];

julia> cover_hessian(f, x0).nguarded
0

julia> cover_hessian(f, x1).nguarded
1

julia> cover_hessian(f, x1).h
2-element Vector{Float64}:
 1.0
 2.0

julia> cover_hessian(f, x1; scales=[1.0, 100.0]).h
2-element Vector{Float64}:
 10.0
  1.1

The declared scales affect only which valid cover is chosen, never validity. When the inputs are rescaled, a cover that depends on declared scales is covariant only if the scales are rescaled along with them, scales → D*scales:

julia> cover_hessian(g, D .* x1; scales=D).h .* D
2-element Vector{Float64}:
 1.0
 2.0

A cover with nguarded == 0 does not depend on the scales keyword at all. Two covers of the same program in different units are covariant, up to rounding, whenever both report nguarded == 0. The count belongs to the cover, not to the program: it is taken at the evaluation point, so a program can report 0 at one point and a positive count at another.

guards_any_block predicts, from a recorded program alone, whether a cover of it might use the declared scales:

julia> t, out = tape_function(f, x1);

julia> DerivativeCovers.guards_any_block(t)
true

The prediction can err in both directions; the nguarded count of a cover is the authority on what that cover did.

The test for "carries no scale" is not limited to exact zeros. An argument pair whose magnitudes differ by more than a factor ratio is treated the same way, so that a value that is zero only up to roundoff does not lead to an enormous local cover. The ratio keyword sets this threshold; the default, sqrt(eps()) for Float64, is described in split_ratio. Because this test compares two values that may have different units, a rescaling can move an operation across the threshold, and covariance then holds only approximately.

Near such points the cover can be discontinuous. For a quotient x / y, as x approaches zero the local cover remains covariant but grows without bound; at x = 0 it drops to the finite cover given by the declared scales.

Scales from the program's curvature

Passing scales = CurvatureScales() derives the declared scales from the program instead. A first sweep covers the program without the operations that would read declared scales, and each input whose cover entry h[i] from that sweep is positive receives the scale 1 / h[i]. A cover transforms as h → inv(D) * h, so these scales transform as the inputs do, and the resulting cover is covariant without any scales from the caller:

julia> f2(x) = f(x) + x[1]^2;

julia> g2(y) = f2(y ./ D);

julia> cover_hessian(f2, x1).nguarded
1

julia> cover_hessian(g2, D .* x1).h .* D ≈ cover_hessian(f2, x1).h
false

julia> h = cover_hessian(f2, x1; scales=CurvatureScales()).h;

julia> cover_hessian(g2, D .* x1; scales=CurvatureScales()).h .* D ≈ h
true

An input that receives curvature only through those operations has no scale the program determines. f itself is an example: at x1, the only curvature of x[1] is in the product x[1] * x[2], whose second argument vanishes. Where such an input's scale would change the result, CurvatureScales throws an error naming it, and the scales must be declared explicitly:

julia> cover_hessian(f, x1; scales=CurvatureScales())
ERROR: ArgumentError: CurvatureScales: input(s) [1] receive curvature only through blocks whose split falls back on declared scales (parent nodes [1, 2]), so the program does not determine their scales relative to the other inputs. Pass declared scales explicitly, `scales = w`, for this computation.
[...]

The first sweep is the cover's own sweep, so on a program with nguarded == 0 the derivation costs nothing; otherwise it adds one more cover sweep, roughly doubling the cost. By default the derivation runs one round. Further rounds re-derive the scales from the cover the previous round produced; they change the result only where an operation's argument depends on several inputs, and converge quickly. See CurvatureScales for the keywords that control them. The derivation needs a recorded program, so forward_cover does not accept it.

Jacobian covers

The default start=:logscale of cover_jacobian produces products a[k] * b[j] that transform exactly under rescaling of the inputs, the outputs, and the intermediate values of the program. The :equal start instead treats all outputs alike: permuting the outputs permutes the cover, but the products do not transform exactly under rescaling.