Skip to content

Instantly share code, notes, and snippets.

@npielawski
Created November 24, 2019 12:13
Show Gist options
  • Save npielawski/df979124a570976bae0149312005e250 to your computer and use it in GitHub Desktop.
Save npielawski/df979124a570976bae0149312005e250 to your computer and use it in GitHub Desktop.
Perceptive Probability
import numpy as np
def perceptive_probability(probability, average="median"):
""" This function translates a probability [0;100] to a human readable string (phrase).
The readable probabilities have been computed from data taken from the
following github repository: https://github.com/zonination/perceptions/
The repository contains a poll where people assigned a numerical probability
to phrases. This data was used to find different types of averages of the
distributions for each phrase.
This function computes the 1 nearest neighbour of a probability to find its
associated sentence.
Args:
probability (int): The probability from 0 to 100.
method (string): The method for computing the average.
Available options: ["mean", "median", "mode"]
Returns (str):
The closest phrase associated to the given probability.
Example:
>>> perceptive_probability(7)
'Highly unlikely'
>>> perceptive_probability(87, "mean")
'Highly likely'
"""
probabilities = {
"mean": np.array([5, 10, 14, 15, 18, 19, 27, 29, 49, 58, 68, 71, 72, 79, 86, 92]),
"median": np.array([2, 5, 10, 15, 15, 20, 25, 26, 50, 60, 70, 70, 70, 80, 90, 95]),
"mode": np.array([5, 5, 10, 10, 10, 20, 25, 25, 50, 60, 70, 70, 80, 80, 90, 95])
}
phrases = [
"Almost no chance", "Highly unlikely", "Chances are slight", "Little chance",
"Improbable", "Unlikely", "Doubtful", "Probably not",
"About even", "Better than even", "Believable", "Probable",
"Likely", "Very good chance", "Highly likely", "Almost certain"
]
neighbours_dist = np.abs(probabilities[average] - probability)
nearest_neighbour = np.argmin(neighbours_dist)
return phrases[nearest_neighbour]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment