Jump to content

Module:DrivingRiskEquivalent

From RiskiPedia

Template:Module documentation

Description

Module:DrivingRiskEquivalent converts a probability (0–1) into the equivalent **miles driven** that would give the same risk of being in a **police-reported car crash**.

By default the result is **rounded to one significant digit** and **formatted with commas**. An optional exact flag returns the **full-precision number**.

Syntax

{{#invoke:DrivingRiskEquivalent|miles_equivalent|0.probability|exact=1}}

Parameters

Parameter Position Type Description
probability 1 (or probability=0... / p=0...) number Probability between 0 and 1
exact exact=1 string true or 1 to return unrounded value

Returns

  • string (default) – Miles rounded to one significant digit, with commas (e.g. 50,000)
  • number (when exact=1) – Exact miles (e.g. 52910.052910053)


Usage in RiskiPedia

Perfect for giving intuitive context to tiny probabilities:

That's about the same risk as getting into a serious accident if you drive {{#invoke:DrivingRiskEquivalent|miles_equivalent|...probability...}} miles.

See Also

Template:Module footer


--[[
Module:DrivingRiskEquivalent
Returns the miles driven equivalent for a given probability, based on baseline US driving crash risk.

Reference: 
- NHTSA 2023 data: 6,138,000 police-reported motor vehicle traffic crashes
- Total VMT: 3,247 billion miles
- Crash rate: 1,890 crashes per billion VMT
- https://crashstats.nhtsa.dot.gov/Api/Public/Publication/813705
]]

local p = {}

-- Format an integer with commas as thousands separators
local function format_with_commas(n)
    local s = tostring(n)
    local sign, int = s:match("^([%-]?)(%d+)$")
    int = int:reverse():gsub("(%d%d%d)", "%1,")
    int = int:reverse():gsub("^,", "")
    return sign .. int
end

-- Round to one significant digit and format with commas
local function round_and_format(n)
    local exponent = math.floor(math.log10(n))
    local mantissa = n / (10 ^ exponent)
    local rounded_mant = math.floor(mantissa + 0.5)
    local rounded = rounded_mant * (10 ^ exponent)
    if rounded > 1000 then
        return format_with_commas(rounded)
    end
    return rounded
end

function p.miles_equivalent(frame)
    local args = frame.args
    local probability = args[1] or args.probability or args.p or 0
    local exact = (args.exact == "1" or args.exact == "true")
    
    -- Convert to number, default to 0 if invalid
    probability = tonumber(probability) or 0
    
    -- Validate range
    if probability < 0 or probability > 1 then
        return "Error: probability must be between 0 and 1"
    end
    
    -- Baseline crash rate: 1,890 crashes per billion miles
    local crash_rate_per_billion_miles = 1890
    
    -- Rate per mile = rate_per_billion / 1,000,000,000
    local rate_per_mile = crash_rate_per_billion_miles / 1000000000
    
    -- Miles = probability / rate_per_mile
    local miles = probability / rate_per_mile

    if exact then
        return miles
    end
    return round_and_format(miles)
end

return p