Smooth Degree-Day Hinges: PyTorch Architecture

Navigation:

Overview & Formula Tokens

Smooth softplus heating and cooling degree-day hinges are exposed via hdd_cdd() (aliases dd() and smooth_hinge()):

# C-infinity softplus heating and cooling degree-day hinges
formula = "Y ~ hdd_cdd(temperature, sharpness=5.0, quad=True)"

Parameter Specification

DegreeDayEffect (hdd_cdd())

Parameter

Type

Default

Description

heat_thresholds

Sequence[float] or str

(-0.2, 0.0)

Heating reference thresholds.

cool_thresholds

Sequence[float] or str

(0.3, 0.5)

Cooling reference thresholds.

sharpness

float

10.0

Softplus sharpness \(\beta > 0\).

quad

bool

True

Append quadratic saturation terms.

Complexity & Performance

  • Pointwise in the temperature: \(\mathcal{O}(B\,T\,(n_{\text{heat}} + n_{\text{cool}}))\) time and memory.

State, Complexity and Limits

  • Class and file: DegreeDayEffect (alias SmoothHingeEffect), src/tam/model/spectrum/_inertia.py. Label: (BETA).

  • State: none. Layout: any shape, one row of features per input value.

  • Thresholds: kept in ascending order inside each family (the penalty couples neighbours); the columns follow that order.

  • Penalty: squared hinges weigh 4, a first-difference coupling acts inside the heating family and inside the cooling family; one threshold per side gives a diagonal matrix. One penalty coordinate.

  • Extrapolation: all modes of the router are available.

Code

Feature map of DegreeDayEffect (build_feature_map):

    def build_feature_map(self, x_col: torch.Tensor) -> torch.Tensor:
        """Builds smooth softplus degree day columns."""
        x_exp = x_col.unsqueeze(-1)
        cols: List[torch.Tensor] = []

        # Heating degree-day hinges: softplus(u - T, beta)
        for u in self.heat_thresholds:
            z = float(u) - x_exp
            hdd = F.softplus(z, beta=self.sharpness)
            cols.append(hdd)
            if self.quad:
                cols.append(hdd ** 2)

        # Cooling degree-day hinges: softplus(T - v, beta)
        for v in self.cool_thresholds:
            z = x_exp - float(v)
            cdd = F.softplus(z, beta=self.sharpness)
            cols.append(cdd)
            if self.quad:
                cols.append(cdd ** 2)

        return torch.cat(cols, dim=-1)

Penalty of DegreeDayEffect (build_penalty_matrix):

    def build_penalty_matrix(self) -> torch.Tensor:
        r"""Builds the penalty: a ridge with the quadratic columns weighted 4, plus a first-difference coupling of the hinges of one family.

        .. math::

            P = \lambda\,\bigl(\operatorname{diag}(w) + D_1^\top D_1|_{\text{heating hinges}} + D_1^\top D_1|_{\text{cooling hinges}}\bigr),
            \qquad w = 1 \text{ (hinge)},\; 4 \text{ (squared hinge)}

        The coupling acts on the linear hinges ordered by threshold, inside each family only; with one threshold per side there is none.
        """
        n_coeffs = self.get_n_coeffs()
        dtype = torch.get_default_dtype()
        weights = torch.ones(n_coeffs, device=TORCH_DEVICE, dtype=dtype)
        P = torch.zeros(n_coeffs, n_coeffs, device=TORCH_DEVICE, dtype=dtype)
        step = 2 if self.quad else 1
        position = 0
        for count in (len(self.heat_thresholds), len(self.cool_thresholds)):
            cols = [position + step * i for i in range(count)]
            if self.quad:
                for c in cols:
                    weights[c + 1] = 4.0
            if count > 1:
                D1 = torch.diff(torch.eye(count, device=TORCH_DEVICE, dtype=dtype), n=1, dim=0)
                C = D1.T @ D1
                idx = torch.tensor(cols, device=TORCH_DEVICE)
                P[idx.unsqueeze(1), idx.unsqueeze(0)] += C
            position += step * count
        P = P + torch.diag(weights)
        return self.lambda_p * P

Parsing of the dd(...) token in the factory:

        elif ttype in ('hdd_cdd', 'dd', 'smooth_hinge', 'degreeday', 'degree_day'):
            heat_raw = params_resolved.get('heat_thresholds', params_resolved.get('heat', (-0.2, 0.0)))
            cool_raw = params_resolved.get('cool_thresholds', params_resolved.get('cool', (0.3, 0.5)))
            sharpness = float(params_resolved.get('sharpness', params_resolved.get('beta', 10.0)))
            quad_raw = params_resolved.get('quad', True)
            quad = _as_bool(quad_raw)
            extrap_val = params_resolved.get('extrapolate', 'continue')
            effects_list.append(DegreeDayEffect(
                feature_name=feature_name,
                heat_thresholds=heat_raw,
                cool_thresholds=cool_raw,
                sharpness=sharpness,
                quad=quad,
                lambda_p=lambda_p,
                extrapolate=extrap_val,
            ))