0

I have developed a probabilistic function of variant choice. I copy a reproducible example below. The function takes a list of n variants and a list of n probabilities. Then it creates a list of floating-point numbers, which determine n ordered numeric breakpoints, one for each variant. Then it generates a random number and the corresponding choice according to the set of ordered numeric breakpoints.

from __future__ import division
from random import random
from bisect import bisect

def choice(variant, probs):
    prob_cumulative = list()
    aux = 0
    for p in probs:
        aux += p
        prob_cumulative.append(aux)
    r = random() * prob_cumulative[-1]
    op = bisect(prob_cumulative, r)
    print(aux)
    print(prob_cumulative)
    print(r)
    print(op)
    return variants[op]

variants=["S","M","N","G"]
probs=[0.8,0,0.1,0.1]
print(choice(variants, probs))

I would like to be able to describe this function using formal maths. I appreciate any answer or suggestion.

pyring
  • 347
  • 2
  • 17
  • 2
    Mathematically speaking, probability is a measure such that the measure of the whole set is equal to 1. In other words, probabilities of partitions of the set should add up to 1. That's why cumulative probability rises until it reaches value 1 -- which is clearly not the case with whatever you're trying to do here. Hence, your code is not really describing a probability function. I suggest you start by reading some wiki article, for example https://en.wikipedia.org/wiki/Cumulative_distribution_function – gstukelj Dec 12 '19 at 12:21
  • Since you have tagged your question `python-3.x`, the line `from __future__ import division` seems pointless. – John Coleman Dec 12 '19 at 12:28
  • Thanks. I have fixed that. My goal was to develop a function to pick a variants according to their probability. The function seems to work, but my concern is if this procedure is mathematically correct, if there is a better way to do it, and if I can describe this formally. – pyring Dec 12 '19 at 12:32
  • 1
    The question isn't clear. What do you mean by "describe this function using formal maths"? It is a simulation of a discrete probability distribution. If all you want to do is pick a single item, `bisect` is superfluous and the code can be made much shorter. – John Coleman Dec 12 '19 at 12:32
  • See [python - Generate random numbers with a given (numerical) distribution](https://stackoverflow.com/q/4265988/4996248) for Python solutions for this. If you are using Python 3.6 or above, `random.choices()` implements the functionality that you are trying to create. – John Coleman Dec 12 '19 at 12:41
  • Yes, it is a discrete probability distribution. And a code that simulates variant choice according to that distribution. So, given such distribution, I wonder how I can formally report variant choice in the way it was coded. – pyring Dec 12 '19 at 12:45
  • Are you asking how a mathematician would describe the function you've written? Isn't it just "sampling from a categorical distribution" (https://en.wikipedia.org/wiki/Categorical_distribution)? – Ahmed Fasih Dec 25 '19 at 18:59

0 Answers0