Skip to contents

Applies temperature scaling to a probability distribution using the softmax function.

Usage

temperature(w, temp)

Arguments

w

A numeric vector of probabilities that must sum to 1. All values must be between 0 and 1 (inclusive). The function will validate that the sum equals 1 within floating-point precision (tolerance of 1e-10).

temp

A single numeric value specifying the temperature parameter. Must be non-negative (>= 0).

  • temp > 1: Makes the distribution more uniform (smoother, less peaked)

  • temp < 1: Makes the distribution less uniform (sharper, more peaked)

  • temp = 1: No change to the original distribution

  • temp = 0: Returns a vector with 1 at the position of the maximum probability and 0 otherwise

Value

A numeric vector of the same length as w containing the temperature-scaled probabilities. The returned vector will sum to 1 and all values will be between 0 and 1.

Examples


w <- runif(4)
w <- w / sum(w)

w
#> [1] 0.01373785 0.13930836 0.22925414 0.61769965

temperature(w, temp = 0)
#> [1] 0 0 0 1
temperature(w, temp = 0.5)
#> [1] 0.0004159713 0.0427740085 0.1158403779 0.8409696423
temperature(w, temp = 1)
#> [1] 0.01373785 0.13930836 0.22925414 0.61769965