---
# System prepended metadata

title: Pseudocode for Online Normalization

---

# Pseudocode for Online Normalization

## Preface

**Recommendation:** Start with the `compute_recommendation_mapping` function and then fill in the blanks with the helper functions. 

## Pythonic Pseudocode

```python=

def get_all_available_risk_values(): 
    return [human.risk_value_today for human in humans if human.has_app]

# Compute the prevelance from the infectiousness predictor. 
def compute_prevelance(is_infectious_threshold): 
    # `humans` is a list of all humans in the sim
    number_of_humans = len(humans)
    number_of_infectious_humans = 0

    for human in humans: 
        # risk_value_today comes from the transformer (or oracle or ...)
        if human.has_app and human.risk_level_today > is_infectious_threshold: 
            number_of_infectious_humans += 1
    
    prevelance = number_of_infectious_humans / number_of_humans
    return prevelance


def compute_expected_risk(is_infectious_threshold): 
    # `humans` is a list of all humans in the sim
    number_of_humans = len(humans)
    number_of_infectious_humans = 0

    for human in humans: 
        # risk_value_today comes from the transformer (or oracle or ...)
        if human.has_app and human.risk_value_today > is_infectious_threshold: 
            number_of_infectious_humans += 1
    
    prevelance = number_of_infectious_humans / number_of_humans
    return prevelance


# This is the function that computes the daily recommendation levels
def compute_recommendation_mapping(
    leeway=1, 
    is_infectious_threshold, 
    relative_fraction_orange_to_red, 
    relative_fraction_yellow_to_red,  
): 
    # Use the function defined above
    prevelance = compute_prevelance(is_infectious_threshold)
    
    # Compute the fraction of folks to put in red, orange, yellow, and green
    fraction_in_red = prevelance * (1 + leeway)
    fraction_in_orange = relative_fraction_orange_to_red * fraction_in_red
    fraction_in_yellow = relative_fraction_yellow_to_red * fraction_in_red
    fraction_in_green = 1 - (fraction_in_red + fraction_in_orange + fraction_in_yellow)
    
    assert fraction_in_green > 0
    
    # Compute the percentiles of the risk level 
    # (this is how we'll split the histogram)
    percentile_in_green = fraction_in_green
    percentile_in_yellow = percentile_in_green + fraction_in_yellow
    percentile_in_orange = percentile_in_yellow + fraction_in_orange
    
    all_risk_values = get_all_available_risk_values()
    
    # We have green_threshold < yellow_threshold < orange_threshold < 1. 
    # We now split the histogram accordingly. 
    green_threshold = percentile(of=all_risk_values, at=percentile_at_green)
    yellow_threshold = percentile(of=all_risk_values, at=percentile_at_yellow)
    orange_threshold = percentile(of=all_risk_values, at_percentile_at_orange)
    
    # Below green_threshold --> rec level green. 
    # Above green threshold but below yellow_threshold --> rec level yellow. 
    # Above yellow_threshold but below orange_threshold --> rec level orange.
    # Above orange_threshold --> rec level red. 
    
    return (green_threshold, yellow_threshold, orange_threshold)

```