Algorithm Search⌘K

Algorithmcore

Truncated channel inversion

Also called channel inversion power control.

Power policy for a fading channel: send nothing below a cutoff SNR, and above it send power inversely proportional to the channel, so the receiver sees a constant SNR and a fixed-rate code works.

Time
O(K log K) worst
Space
O(K) extra
Where
K = the number of channel states
Properties
deterministicfixed rateneeds csitnot optimal
Origin
Andrea J. Goldsmith, Pravin P. Varaiya, 1997

Truncated channel inversion is a power policy for a fading channel whose gain the transmitter knows. Let γ\gamma be the SNR that the receiver would see at the average power Pˉ\bar P. When γ\gamma is below a cutoff γ0\gamma_0, the transmitter sends nothing. Otherwise it sends the power P(γ)=Pˉσ/γP(\gamma) = \bar P \sigma/\gamma, the inverse of the channel, so the received SNR is the constant σ\sigma. The average power limit fixes σ=1/E[1/γ; γ≥γ0]\sigma = 1/\mathbb{E}[1/\gamma;\, \gamma \ge \gamma_0], and a single code for the AWGN channel at SNR σ\sigma then gives the rate R(γ0)=Pr⁡(γ≥γ0)log⁡2(1+σ)R(\gamma_0) = \Pr(\gamma \ge \gamma_0)\log_2(1 + \sigma). The cutoff is chosen to maximize this rate. Without truncation (γ0=0\gamma_0 = 0), channel inversion has zero rate on Rayleigh fading, since E[1/γ]\mathbb{E}[1/\gamma] is infinite there. Goldsmith and Varaiya (1997) compared it with water-filling over time, which is optimal: inversion is simple and keeps one fixed rate, and on Rayleigh fading it loses about a third to two thirds of a bit against water-filling between 10 and 20 dB.

In plain words

A wireless channel gets stronger and weaker all the time. If the sender knows how strong it is, it can push more power when the channel is weak and less when it is strong. Then the receiver always gets the same signal level, and one simple fixed code works all the time.

The trouble is that a very weak channel needs a huge amount of power to fix. So the sender gives up when the channel is too weak and sends nothing then. The level at which it gives up is a trade-off: give up too often and little time is used, give up too rarely and power is wasted on bad moments.

The problem

The SNR γ>0\gamma > 0 is random with a known distribution, and the transmitter knows its current value. A policy P(γ)≥0P(\gamma) \ge 0 must satisfy E[P(γ)]≤Pˉ\mathbb{E}[P(\gamma)] \le \bar P. Truncated inversion uses the received SNR σ\sigma in every state with γ≥γ0\gamma \ge \gamma_0 and nothing else; given the cutoff, the largest such σ\sigma is 1/S(γ0)1/S(\gamma_0) with S(γ0)=E[γ−11{γ≥γ0}]S(\gamma_0) = \mathbb{E}[\gamma^{-1} \mathbf{1}\{\gamma \ge \gamma_0\}]. The task is to find γ0\gamma_0 that maximizes R(γ0)=Pr⁡(γ≥γ0)log⁡2(1+1/S(γ0))R(\gamma_0) = \Pr(\gamma \ge \gamma_0)\log_2(1 + 1/S(\gamma_0)). For a distribution with KK states, such as a quantized channel or a measured sample, the best cutoff is one of the states.

The algorithm

The states are γk>0\gamma_k > 0 with probabilities πk\pi_k (equal values merged first).

  1. TRUNCATED-INVERSION(γ[1..K], π[1..K])
  2. 1sort the states so that γ[1] > γ[2] > .. > γ[K]
  3. 2S ← 0, Q ← 0, best ← (0, none, none)
  4. 3for k = 1 to K do ▷ serve the k strongest states
  5. 4S ← S + π_k / γ_k, Q ← Q + π_k ▷ S = E[1/γ; γ ≥ γ_k], Q = Pr(γ ≥ γ_k)
  6. 5R ← Q · log2(1 + 1/S)
  7. 6if R > best rate then best ← (R, γ_k, 1/S)
  8. 7(R, γ0, σ) ← best
  9. 8return γ0, σ, R, and P(γ)/P̄ = σ/γ for γ ≥ γ0, 0 otherwise

For Rayleigh fading with mean SNR γˉ\bar\gamma, the sums have the closed form S(γ0)=E1(γ0/γˉ)/γˉS(\gamma_0) = E_1(\gamma_0/\bar\gamma)/\bar\gamma and Pr⁡(γ≥γ0)=e−γ0/γˉ\Pr(\gamma \ge \gamma_0) = e^{-\gamma_0/\bar\gamma}, where E1E_1 is the exponential integral, and the best cutoff is found by a one-dimensional search.

Example

Four states. Let γ∈{8,4,2,0.5}\gamma \in \{8, 4, 2, 0.5\}, each with probability 1/41/4.

served states SS σ=1/S\sigma = 1/S Pr⁡(served)\Pr(\text{served}) rate Qlog⁡2(1+σ)Q\log_2(1 + \sigma)
8 0.03125 32.000 0.25 1.2611
8, 4 0.09375 10.667 0.50 1.7722
8, 4, 2 0.21875 4.571 0.75 1.8585
8, 4, 2, 0.5 0.71875 1.391 1.00 1.2578

The best cutoff is γ0=2\gamma_0 = 2, with σ=32/7≈4.571\sigma = 32/7 \approx 4.571 (6.60 dB) and the rate 1.8585 bits per channel use. The powers are P/Pˉ=(4/7,8/7,16/7,0)P/\bar P = (4/7, 8/7, 16/7, 0) for γ=(8,4,2,0.5)\gamma = (8, 4, 2, 0.5), and their mean is 1. Full inversion, the last row, must spend most of its power on the state γ=0.5\gamma = 0.5 and gets only 1.2578.

Rayleigh fading. For Rayleigh fading with mean SNR γˉ\bar\gamma, compare the best truncated inversion with the ergodic capacity without transmitter knowledge, Elog⁡2(1+γ)\mathbb{E}\log_2(1 + \gamma), with water-filling over time, and with an AWGN channel at the same mean SNR (values computed by the test code):

γˉ\bar\gamma best γ0\gamma_0 outage σ\sigma truncated inversion no CSIT water-filling AWGN
0 dB -1.21 dB 0.531 4.74 dB 0.934 0.860 1.029 1.000
5 dB 1.82 dB 0.382 7.34 dB 1.658 1.716 1.845 2.057
10 dB 4.80 dB 0.261 10.45 dB 2.660 2.907 2.979 3.459
20 dB 11.18 dB 0.123 18.01 dB 5.267 5.884 5.896 6.658

At 10 dB the policy gives up in 26% of the time and loses 0.32 bits against water-filling. At low SNR it beats the rate without transmitter knowledge, because it moves power to the good states.

Correctness

Claim. The policy returned uses the average power exactly Pˉ\bar P, gives the received SNR σ\sigma in every state it serves, and its cutoff maximizes R(γ0)R(\gamma_0) over all γ0>0\gamma_0 > 0.

Key steps. With P(γ)/Pˉ=σ/γP(\gamma)/\bar P = \sigma/\gamma on the served states, the mean power is σS\sigma S, so σ=1/S\sigma = 1/S uses the budget exactly, and no larger constant SNR fits. For a discrete distribution, every cutoff serves some set of the strongest states, and the rate depends only on that set, so the KK cutoffs at the states cover all cases. The loop keeps SS and QQ as running sums over the strongest kk states, so line L5 computes each candidate rate exactly once, and line L6 keeps the largest. The full proof is in the proofs folder.

Proof: Truncated channel inversion meets the power constraint, holds the received SNR constant, and the scan finds the best cutofffull

Setting. The channel SNR at the average power Pˉ\bar P is a random variable γ>0\gamma > 0. A power policy P(γ)≥0P(\gamma) \ge 0 must satisfy E[P(γ)]≤Pˉ\mathbb{E}[P(\gamma)] \le \bar P, and in state γ\gamma the received SNR is γP(γ)/Pˉ\gamma P(\gamma)/\bar P. For a cutoff γ0>0\gamma_0 > 0, let

Q(γ0)=Pr⁡(γ≥γ0),S(γ0)=E ⁣[1γ ; γ≥γ0]=E ⁣[1γ 1{γ≥γ0}].Q(\gamma_0) = \Pr(\gamma \ge \gamma_0), \qquad S(\gamma_0) = \mathbb{E}\!\left[\frac{1}{\gamma}\,;\, \gamma \ge \gamma_0\right] = \mathbb{E}\!\left[\frac{1}{\gamma}\,\mathbf{1}\{\gamma \ge \gamma_0\}\right].

Assume Q(γ0)>0Q(\gamma_0) > 0, so that 0<S(γ0)≤Q(γ0)/γ0<∞0 < S(\gamma_0) \le Q(\gamma_0)/\gamma_0 < \infty. Truncated channel inversion with cutoff γ0\gamma_0 is the policy

P(γ)Pˉ={σ(γ0)/γ,γ≥γ0,0,γ<γ0,σ(γ0)=1S(γ0),\frac{P(\gamma)}{\bar P} = \begin{cases} \sigma(\gamma_0)/\gamma, & \gamma \ge \gamma_0, \\ 0, & \gamma < \gamma_0, \end{cases} \qquad \sigma(\gamma_0) = \frac{1}{S(\gamma_0)},

and its rate is R(γ0)=Q(γ0)log⁡2(1+σ(γ0))R(\gamma_0) = Q(\gamma_0) \log_2(1 + \sigma(\gamma_0)) bits per channel use: a fixed code for the AWGN channel with SNR σ\sigma is used whenever γ≥γ0\gamma \ge \gamma_0.

Lemma 1 (power and received SNR). The policy uses the average power exactly Pˉ\bar P, the received SNR is σ(γ0)\sigma(\gamma_0) in every state with γ≥γ0\gamma \ge \gamma_0, and no policy that sends nothing below γ0\gamma_0 and gives the same received SNR σ′\sigma' in every state with γ≥γ0\gamma \ge \gamma_0 within the power Pˉ\bar P has σ′>σ(γ0)\sigma' > \sigma(\gamma_0).

Proof. E[P(γ)]/Pˉ=σ E[γ−11{γ≥γ0}]=σS=1\mathbb{E}[P(\gamma)]/\bar P = \sigma\, \mathbb{E}[\gamma^{-1} \mathbf{1}\{\gamma \ge \gamma_0\}] = \sigma S = 1. In a state with γ≥γ0\gamma \ge \gamma_0, the received SNR is γ⋅σ/γ=σ\gamma \cdot \sigma/\gamma = \sigma. A policy with received SNR σ′\sigma' on the same states must use P(γ)/Pˉ=σ′/γP(\gamma)/\bar P = \sigma'/\gamma there, so its average power is σ′SPˉ\sigma' S \bar P, and σ′S≤1\sigma' S \le 1 gives σ′≤σ\sigma' \le \sigma. □\square

Lemma 2 (finitely many candidates). Let γ\gamma take the distinct values γ(1)>γ(2)>⋯>γ(K)>0\gamma_{(1)} > \gamma_{(2)} > \cdots > \gamma_{(K)} > 0 with probabilities π1,…,πK>0\pi_1, \ldots, \pi_K > 0. For k=1,…,Kk = 1, \ldots, K put Qk=∑j≤kπjQ_k = \sum_{j \le k} \pi_j and Sk=∑j≤kπj/γ(j)S_k = \sum_{j \le k} \pi_j/\gamma_{(j)}, and Rk=Qklog⁡2(1+1/Sk)R_k = Q_k \log_2(1 + 1/S_k). Then {R(γ0):γ0>0, Q(γ0)>0}={R1,…,RK}\{R(\gamma_0) : \gamma_0 > 0,\ Q(\gamma_0) > 0\} = \{R_1, \ldots, R_K\}.

Proof. For γ0\gamma_0 with γ(k+1)<γ0≤γ(k)\gamma_{(k+1)} < \gamma_0 \le \gamma_{(k)} (with γ(K+1)=0\gamma_{(K+1)} = 0), the event γ≥γ0\gamma \ge \gamma_0 is the event that γ\gamma is one of γ(1),…,γ(k)\gamma_{(1)}, \ldots, \gamma_{(k)}, so Q(γ0)=QkQ(\gamma_0) = Q_k and S(γ0)=SkS(\gamma_0) = S_k, and R(γ0)=RkR(\gamma_0) = R_k. For γ0>γ(1)\gamma_0 > \gamma_{(1)}, Q(γ0)=0Q(\gamma_0) = 0. These intervals cover (0,∞)(0, \infty), and the choice γ0=γ(k)\gamma_0 = \gamma_{(k)} gives RkR_k. □\square

Lemma 3 (the scan). After the pass of lines L3 to L6 for the kk-th state in sorted order, S=SkS = S_k, Q=QkQ = Q_k, and the stored best rate is max⁡j≤kRj\max_{j \le k} R_j, stored with its cutoff γ(j)\gamma_{(j)} and σ=1/Sj\sigma = 1/S_j for the first jj that attains it.

Proof. Line L1 merges equal values, adding their probabilities, and sorts the distinct values in decreasing order. Before the first pass, S=Q=0S = Q = 0 and the best rate is 0. Line L4 adds πk/γ(k)\pi_k/\gamma_{(k)} and πk\pi_k, which gives SkS_k and QkQ_k from Sk−1S_{k-1} and Qk−1Q_{k-1}. Line L5 computes RkR_k, and line L6 keeps it if it is larger than all earlier ones. Every Rk>0R_k > 0, so the first pass always stores its state. □\square

Theorem. For a discrete distribution, TRUNCATED-INVERSION returns a cutoff γ0\gamma_0 that maximizes R(γ0)R(\gamma_0) over all γ0>0\gamma_0 > 0, the value σ(γ0)\sigma(\gamma_0), the rate R(γ0)R(\gamma_0), and the power of each state, and this policy uses the average power Pˉ\bar P and gives the constant received SNR σ(γ0)\sigma(\gamma_0) in every state it serves.

Proof. By Lemma 3, after the last pass the stored rate is max⁡kRk\max_k R_k, which by Lemma 2 is the largest rate over all cutoffs. Its cutoff is γ(k)\gamma_{(k)} and its σ\sigma is 1/Sk=σ(γ(k))1/S_k = \sigma(\gamma_{(k)}). Line L8 sets P/Pˉ=σ/γP/\bar P = \sigma/\gamma for γ≥γ0\gamma \ge \gamma_0 and 0 otherwise, which is the policy of Lemma 1. □\square

Remark (the Rayleigh closed form). For Rayleigh fading, γ\gamma has the density e−γ/γˉ/γˉe^{-\gamma/\bar\gamma}/\bar\gamma, so with t=γ/γˉt = \gamma/\bar\gamma,

S(γ0)=∫γ0∞e−γ/γˉγˉ γ dγ=1γˉ∫γ0/γˉ∞e−tt dt=E1(γ0/γˉ)γˉ,S(\gamma_0) = \int_{\gamma_0}^{\infty} \frac{e^{-\gamma/\bar\gamma}}{\bar\gamma\, \gamma}\, d\gamma = \frac{1}{\bar\gamma} \int_{\gamma_0/\bar\gamma}^{\infty} \frac{e^{-t}}{t}\, dt = \frac{E_1(\gamma_0/\bar\gamma)}{\bar\gamma},

and Q(γ0)=e−γ0/γˉQ(\gamma_0) = e^{-\gamma_0/\bar\gamma}. So σ=γˉ/E1(γ0/γˉ)\sigma = \bar\gamma/E_1(\gamma_0/\bar\gamma) and R(γ0)=e−γ0/γˉlog⁡2(1+γˉ/E1(γ0/γˉ))R(\gamma_0) = e^{-\gamma_0/\bar\gamma}\log_2(1 + \bar\gamma/E_1(\gamma_0/\bar\gamma)). As γ0→0\gamma_0 \to 0, E1→∞E_1 \to \infty and R→0R \to 0; this is why full inversion has zero rate on Rayleigh fading. As γ0→∞\gamma_0 \to \infty, Q→0Q \to 0 faster than log⁡2(1+σ)\log_2(1 + \sigma) grows, so R→0R \to 0. The function is continuous, so a best cutoff exists, and the implementation finds it by a grid on a log scale followed by a golden section search.

Remark (what the theorem does not say). The theorem is about threshold policies, which is what truncated channel inversion means. Among all policies with a constant received SNR on the served states, serving part of a state at random can do better on a discrete distribution. For a fixed served probability QQ, the cost SS is least when the served probability sits on the largest values of γ\gamma, as in the fractional knapsack problem, so the best such policy serves the top states and a fraction of one more. For a continuous distribution this fraction has no effect, and the threshold policy is the best of this class. Policies whose received SNR varies with γ\gamma can do better still: water-filling over time attains the capacity with side information at both ends, and R(γ0)R(\gamma_0) is at most that capacity.

Complexity

In the real RAM, line L1 sorts in O(Klog⁡K)O(K \log K) time, and the loop does O(1)O(1) work per state, so the time is O(Klog⁡K)O(K \log K) and the extra space is O(K)O(K). When the states arrive sorted, as in a table of quantized SNR levels, the time is O(K)O(K). For a continuous distribution with a closed form such as Rayleigh, each evaluation of R(γ0)R(\gamma_0) costs one evaluation of E1E_1, and a golden section search needs about log⁡1.618(1/ϵ)\log_{1.618}(1/\epsilon) evaluations. The policy is computed once per channel distribution; at run time it costs one division per channel state.

Properties

  • Constant received SNR. One fixed code and one fixed modulation work in every served state. This is its main practical advantage over water-filling, which needs a different rate in each state.
  • Zero rate without truncation on Rayleigh fading. E[1/γ]=∞\mathbb{E}[1/\gamma] = \infty for the exponential distribution, so full inversion has σ=0\sigma = 0. Diversity changes this: with L≥2L \ge 2 branches of maximal ratio combining, E[1/γ]\mathbb{E}[1/\gamma] is finite.
  • Not capacity-achieving. It spends the most power where the channel is weakest, the reverse of water-filling. Its rate is at most the water-filling capacity, which is at most log⁡2(1+E[γ])\log_2(1 + \mathbb{E}[\gamma]) by Jensen's inequality.
  • Outage. The cutoff sets the outage probability Pr⁡(γ<γ0)\Pr(\gamma < \gamma_0), which is 0.261 at the best cutoff for Rayleigh fading at 10 dB. A delay-sensitive service can fix the outage first and accept the rate it gets; this is the outage capacity.
  • Sensitive to estimation errors. A deep fade that the transmitter misreads leads to large power errors near the cutoff.

Variants and improvements

  • Full channel inversion. γ0=0\gamma_0 = 0, the zero-outage capacity log⁡2(1+1/E[1/γ])\log_2(1 + 1/\mathbb{E}[1/\gamma]). It is useful only when E[1/γ]\mathbb{E}[1/\gamma] is small, for example with diversity.
  • Fixed outage. Choose γ0\gamma_0 so that Pr⁡(γ<γ0)=pout\Pr(\gamma < \gamma_0) = p_{\mathrm{out}} instead of maximizing the rate.
  • Discrete rates. Adaptive modulation changes the constellation with γ\gamma instead of holding the SNR constant. Combined forms invert the channel inside each region of a constellation, so the received SNR is just at the threshold of the constellation in use.
  • Water-filling over time. P(γ)/Pˉ=max⁡(0,1/γ0−1/γ)P(\gamma)/\bar P = \max(0, 1/\gamma_0 - 1/\gamma) with a variable rate attains the capacity with side information at both ends; see water-filling.
  • Closed-loop power control. Cellular uplinks, such as CDMA, use fast power control steps to keep the received SNR near a target. This is a form of channel inversion that also solves the near-far problem; an outer loop moves the target.

When to use it

Use it when the transmitter knows the channel, the codec or the service wants a fixed rate, and a small outage is acceptable, as for voice. When the rate may vary, adaptive modulation or water-filling gives more. On channels with strong diversity, the loss from inversion is small, and full inversion without truncation may be enough.

Pitfalls

  • Inverting without a cutoff on Rayleigh fading. The required mean power is unbounded; a simulation with a finite sample hides this and shows a rate that falls as the sample grows.
  • Using the received SNR in place of the channel SNR. γ\gamma is the SNR at the average power, before the policy changes the power.
  • Peak power. Near the cutoff the power is Pˉσ/γ0\bar P \sigma/\gamma_0, which can be several times the mean. A real amplifier limits this, and the analysis must include the limit.
  • dB values. The sums use linear SNR values.
  • Cutoffs between states. For a discrete distribution, a cutoff between two states is the same as the next state above it; searching a fine grid wastes time without changing the answer.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

truncated_channel_inversion.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/truncated-channel-inversion/. The authors are listed in prov/PEF750.jsonl.

"""Truncated channel inversion over a fading channel.

gamma is the SNR the receiver would see at the average power. Truncated
channel inversion sends nothing when gamma < gamma0 and otherwise sends the
power P(gamma) / Pbar = sigma / gamma, so the received SNR is the constant
sigma. The average power constraint gives sigma = 1 / E[1/gamma; gamma >= gamma0],
and the rate is log2(1 + sigma) * Pr(gamma >= gamma0) bits per channel use.

truncated_inversion() works on a discrete distribution of states (for
example a quantized channel or an empirical sample) and finds the best cutoff
among the states in O(K log K). The Rayleigh functions use the closed form
sigma = gbar / E1(gamma0 / gbar) and Pr = exp(-gamma0 / gbar).
"""

import math

EULER_GAMMA = 0.5772156649015329


def truncated_inversion(gammas, probs=None):
    """Best cutoff for states gammas[k] > 0 with probabilities probs[k].

    Returns (gamma0, sigma, rate, powers), where powers[k] is P / Pbar in state k.
    probs defaults to equal weights (an empirical sample).
    """
    if not gammas or any(not g > 0 for g in gammas):
        raise ValueError("need at least one state and positive SNR values")
    if probs is None:
        probs = [1 / len(gammas)] * len(gammas)
    if len(probs) != len(gammas) or any(p < 0 for p in probs) or abs(sum(probs) - 1) > 1e-9:
        raise ValueError("probs must be a probability vector of the same length")
    merged = {}
    for g, p in zip(gammas, probs):
        merged[g] = merged.get(g, 0.0) + p
    states = sorted(((g, p) for g, p in merged.items() if p > 0), reverse=True)  # @line L1
    s, q, best = 0.0, 0.0, (0.0, None, None)  # @line L2
    for g, p in states:  # @line L3
        s, q = s + p / g, q + p  # @line L4
        rate = q * math.log2(1 + 1 / s)  # @line L5
        if rate > best[0]:  # @line L6
            best = (rate, g, 1 / s)
    rate, gamma0, sigma = best  # @line L7
    powers = [sigma / g if g >= gamma0 else 0.0 for g in gammas]
    return gamma0, sigma, rate, powers  # @line L8


def zero_outage_rate(gammas, probs=None):
    """Full channel inversion: log2(1 + 1 / E[1/gamma])."""
    if probs is None:
        probs = [1 / len(gammas)] * len(gammas)
    return math.log2(1 + 1 / sum(p / g for g, p in zip(gammas, probs)))


def expint_e1(x: float) -> float:
    """The exponential integral E1(x) for x > 0."""
    if not x > 0:
        raise ValueError("E1 needs x > 0")
    if x <= 1:
        total, term, k = -EULER_GAMMA - math.log(x), 1.0, 1
        while True:
            term *= -x / k
            add = -term / k
            total += add
            if abs(add) < 1e-17 * abs(total):
                return total
            k += 1
    # Continued fraction (modified Lentz), valid for x > 0, fast for x >= 1.
    tiny = 1e-300
    b = x + 1
    c = 1 / tiny
    d = 1 / b
    h = d
    for i in range(1, 1000):
        a = -i * i
        b += 2
        d = 1 / (a * d + b)
        c = b + a / c
        delta = c * d
        h *= delta
        if abs(delta - 1) < 1e-16:
            break
    return h * math.exp(-x)


def rayleigh_rate(gamma0: float, gbar: float) -> float:
    """Rate of truncated inversion with cutoff gamma0 on Rayleigh fading with mean SNR gbar."""
    if not gamma0 > 0 or not gbar > 0:
        raise ValueError("need gamma0 > 0 and gbar > 0")
    x = gamma0 / gbar
    sigma = gbar / expint_e1(x)
    return math.log2(1 + sigma) * math.exp(-x)


def rayleigh_best_cutoff(gbar: float, iters: int = 200) -> tuple[float, float]:
    """(gamma0, rate) of the best cutoff on Rayleigh fading: a log grid, then golden section."""
    grid = [gbar * 10 ** (e / 20) for e in range(-120, 41)]
    k = max(range(len(grid)), key=lambda j: rayleigh_rate(grid[j], gbar))
    lo = math.log(grid[max(k - 1, 0)])
    hi = math.log(grid[min(k + 1, len(grid) - 1)])
    r = (math.sqrt(5) - 1) / 2
    for _ in range(iters):
        a, b = hi - r * (hi - lo), lo + r * (hi - lo)
        if rayleigh_rate(math.exp(a), gbar) < rayleigh_rate(math.exp(b), gbar):
            lo = a
        else:
            hi = b
    g0 = math.exp((lo + hi) / 2)
    return g0, rayleigh_rate(g0, gbar)
test_truncated_channel_inversion.py 134 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/truncated-channel-inversion/. The authors are listed in prov/PEF750.jsonl.

import math
import random

import pytest

from truncated_channel_inversion import (
    expint_e1,
    rayleigh_best_cutoff,
    rayleigh_rate,
    truncated_inversion,
    zero_outage_rate,
)


def top_k_rates(gammas, probs):
    states = sorted(zip(gammas, probs), reverse=True)
    out, s, q = [], 0.0, 0.0
    for g, p in states:
        s, q = s + p / g, q + p
        out.append(q * math.log2(1 + 1 / s))
    return out


def water_filling_rate(gammas, probs):
    """Capacity with the SNR known at both ends: P(g)/Pbar = max(0, 1/g0 - 1/g)."""
    lo, hi = 1e-12, max(gammas) * 1e6
    for _ in range(300):
        mid = math.sqrt(lo * hi)
        power = sum(p * max(0.0, 1 / mid - 1 / g) for g, p in zip(gammas, probs))
        lo, hi = (lo, mid) if power < 1 else (mid, hi)
    g0 = math.sqrt(lo * hi)
    return sum(p * math.log2(g / g0) for g, p in zip(gammas, probs) if g > g0)


def test_page_example() -> None:
    gamma0, sigma, rate, powers = truncated_inversion([0.5, 2, 4, 8], [0.25] * 4)
    assert gamma0 == 2 and abs(sigma - 32 / 7) < 1e-12
    assert abs(rate - 0.75 * math.log2(1 + 32 / 7)) < 1e-12
    assert abs(rate - 1.858535) < 1e-6
    assert powers == [0.0, 16 / 7, 8 / 7, 4 / 7]
    assert [round(r, 4) for r in top_k_rates([0.5, 2, 4, 8], [0.25] * 4)] == [1.2611, 1.7722, 1.8585, 1.2578]
    assert abs(zero_outage_rate([0.5, 2, 4, 8]) - 1.257798) < 1e-6


def test_power_and_constant_received_snr() -> None:
    rng = random.Random(51)
    for _ in range(300):
        k = rng.randint(1, 30)
        gammas = [rng.expovariate(1 / 10) + 1e-3 for _ in range(k)]
        w = [rng.random() + 1e-3 for _ in range(k)]
        probs = [x / sum(w) for x in w]
        gamma0, sigma, rate, powers = truncated_inversion(gammas, probs)
        assert abs(sum(p * x for p, x in zip(probs, powers)) - 1) < 1e-9
        for g, x in zip(gammas, powers):
            if g >= gamma0:
                assert abs(g * x - sigma) < 1e-9 * sigma
            else:
                assert x == 0
        served = sum(p for g, p in zip(gammas, probs) if g >= gamma0)
        assert abs(rate - served * math.log2(1 + sigma)) < 1e-12
        assert abs(rate - max(top_k_rates(gammas, probs))) < 1e-12


def test_rate_lies_between_the_bounds() -> None:
    rng = random.Random(52)
    for _ in range(100):
        k = rng.randint(1, 40)
        gammas = [rng.expovariate(1 / 20) + 1e-2 for _ in range(k)]
        probs = [1 / k] * k
        _, _, rate, _ = truncated_inversion(gammas, probs)
        assert rate >= zero_outage_rate(gammas, probs) - 1e-12
        assert rate <= water_filling_rate(gammas, probs) + 1e-9
        assert water_filling_rate(gammas, probs) <= math.log2(1 + sum(gammas) / k) + 1e-9


def test_ties_are_merged() -> None:
    a = truncated_inversion([2, 2, 8, 0.5], [0.125, 0.125, 0.25, 0.5])
    b = truncated_inversion([2, 8, 0.5], [0.25, 0.25, 0.5])
    assert a[:3] == b[:3]


def test_e1_values() -> None:
    assert abs(expint_e1(1) - 0.21938393439552) < 1e-13
    assert abs(expint_e1(0.1) - 1.8229239584193906) < 1e-13
    assert abs(expint_e1(5) - 0.001148295591275) < 1e-15
    assert abs(expint_e1(1e-4) - (-0.5772156649015329 - math.log(1e-4) + 1e-4)) < 1e-8
    with pytest.raises(ValueError):
        expint_e1(0)


def test_rayleigh_closed_form_against_integration() -> None:
    gbar, gamma0 = 10.0, 3.0
    n, top = 200000, 400.0
    h = (top - gamma0) / n
    total = 0.0
    for i in range(n + 1):
        g = gamma0 + i * h
        w = 1 if i in (0, n) else (4 if i % 2 else 2)
        total += w * math.exp(-g / gbar) / (gbar * g)
    inv_mean = total * h / 3
    expected = math.log2(1 + 1 / inv_mean) * math.exp(-gamma0 / gbar)
    assert abs(rayleigh_rate(gamma0, gbar) - expected) < 1e-7


def test_rayleigh_check_values() -> None:
    gamma0, rate = rayleigh_best_cutoff(10.0)
    assert abs(rate - 2.6595) < 1e-4
    assert abs(10 * math.log10(gamma0) - 4.80) < 0.01
    ergodic = math.exp(0.1) * expint_e1(0.1) / math.log(2)
    assert abs(ergodic - 2.9065) < 1e-4
    assert rate < ergodic < math.log2(11)
    for g0 in (gamma0 * 0.8, gamma0 * 1.25):
        assert rayleigh_rate(g0, 10.0) < rate


def test_rayleigh_sample_matches_closed_form() -> None:
    rng = random.Random(53)
    sample = [rng.expovariate(1 / 10) for _ in range(40000)]
    _, _, rate, _ = truncated_inversion(sample)
    assert abs(rate - 2.6595) < 0.05


def test_bad_input() -> None:
    with pytest.raises(ValueError):
        truncated_inversion([])
    with pytest.raises(ValueError):
        truncated_inversion([1, 0])
    with pytest.raises(ValueError):
        truncated_inversion([1, 2], [0.5, 0.6])
    with pytest.raises(ValueError):
        rayleigh_rate(0, 1)

History

Channel inversion is the idea behind the power control of cellular CDMA systems, which keep the received power of every user at the same level. Goldsmith and Varaiya, in their 1997 paper on the capacity of fading channels with side information, derived the capacity when both ends know the channel, which uses water-filling in time, and compared it with the rates of channel inversion and truncated inversion, which keep the received SNR constant. On Rayleigh fading, inversion without truncation gives zero rate. Goldsmith's textbook calls the rate of channel inversion the zero-outage capacity and the rate of truncated inversion the outage capacity, and Tse and Viswanath make the same comparison with water-filling.

References

  1. Andrea J. Goldsmith and Pravin P. Varaiya, Capacity of fading channels with channel side information, IEEE Transactions on Information Theory 43(6), 1997, pages 1986 to 1992, the capacity with side information at both ends (water-filling in time), compared with the rates of channel inversion and truncated channel inversion
  2. Andrea Goldsmith, Wireless Communications, Cambridge University Press, 2005, Chapter 4 (capacity of fading channels: channel inversion, zero-outage capacity, truncated channel inversion and outage capacity)
  3. David Tse and Pramod Viswanath, Fundamentals of Wireless Communication, Cambridge University Press, 2005, Chapter 5 (fading channels with full channel side information, channel inversion compared with water-filling over time)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example