Grade of Service Probability Function Python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












Consider the following typical probability scenario:



Probability Scenario



I defined this function to handle that scenario, I'm curious if Python has a more efficient method to handle this, or if this is the best way:



from scipy.special import binom

def grade_of_service(n, p, c):
prob = 0
k = c + 1
while k <= n:
prob += binom(n, k)*(p**k)*((1-p)**(n-k))
k += 1
return prob


EDIT: Here's an example solution from the author of this example: "If n = 100, p=0.1, and c=15, the probability of interest turns out to be 0.0399."
My probability does round to this result. But due to the roundoff error / numerical precision, it doesn't seem to matter if k = n or k = n+1.







share|improve this question





















  • Do you have some example inputs we can test with? Preferably edge cases.
    – Peilonrayz
    Jun 21 at 22:05











  • @Peilonrayz, I edited to provide an example solution from the example author. I don't have any edge cases yet but will mess with some numbers to see if I can find some.
    – Hanzy
    Jun 21 at 22:11
















up vote
3
down vote

favorite












Consider the following typical probability scenario:



Probability Scenario



I defined this function to handle that scenario, I'm curious if Python has a more efficient method to handle this, or if this is the best way:



from scipy.special import binom

def grade_of_service(n, p, c):
prob = 0
k = c + 1
while k <= n:
prob += binom(n, k)*(p**k)*((1-p)**(n-k))
k += 1
return prob


EDIT: Here's an example solution from the author of this example: "If n = 100, p=0.1, and c=15, the probability of interest turns out to be 0.0399."
My probability does round to this result. But due to the roundoff error / numerical precision, it doesn't seem to matter if k = n or k = n+1.







share|improve this question





















  • Do you have some example inputs we can test with? Preferably edge cases.
    – Peilonrayz
    Jun 21 at 22:05











  • @Peilonrayz, I edited to provide an example solution from the example author. I don't have any edge cases yet but will mess with some numbers to see if I can find some.
    – Hanzy
    Jun 21 at 22:11












up vote
3
down vote

favorite









up vote
3
down vote

favorite











Consider the following typical probability scenario:



Probability Scenario



I defined this function to handle that scenario, I'm curious if Python has a more efficient method to handle this, or if this is the best way:



from scipy.special import binom

def grade_of_service(n, p, c):
prob = 0
k = c + 1
while k <= n:
prob += binom(n, k)*(p**k)*((1-p)**(n-k))
k += 1
return prob


EDIT: Here's an example solution from the author of this example: "If n = 100, p=0.1, and c=15, the probability of interest turns out to be 0.0399."
My probability does round to this result. But due to the roundoff error / numerical precision, it doesn't seem to matter if k = n or k = n+1.







share|improve this question













Consider the following typical probability scenario:



Probability Scenario



I defined this function to handle that scenario, I'm curious if Python has a more efficient method to handle this, or if this is the best way:



from scipy.special import binom

def grade_of_service(n, p, c):
prob = 0
k = c + 1
while k <= n:
prob += binom(n, k)*(p**k)*((1-p)**(n-k))
k += 1
return prob


EDIT: Here's an example solution from the author of this example: "If n = 100, p=0.1, and c=15, the probability of interest turns out to be 0.0399."
My probability does round to this result. But due to the roundoff error / numerical precision, it doesn't seem to matter if k = n or k = n+1.









share|improve this question












share|improve this question




share|improve this question








edited Jun 21 at 22:10
























asked Jun 21 at 21:53









Hanzy

929




929











  • Do you have some example inputs we can test with? Preferably edge cases.
    – Peilonrayz
    Jun 21 at 22:05











  • @Peilonrayz, I edited to provide an example solution from the example author. I don't have any edge cases yet but will mess with some numbers to see if I can find some.
    – Hanzy
    Jun 21 at 22:11
















  • Do you have some example inputs we can test with? Preferably edge cases.
    – Peilonrayz
    Jun 21 at 22:05











  • @Peilonrayz, I edited to provide an example solution from the example author. I don't have any edge cases yet but will mess with some numbers to see if I can find some.
    – Hanzy
    Jun 21 at 22:11















Do you have some example inputs we can test with? Preferably edge cases.
– Peilonrayz
Jun 21 at 22:05





Do you have some example inputs we can test with? Preferably edge cases.
– Peilonrayz
Jun 21 at 22:05













@Peilonrayz, I edited to provide an example solution from the example author. I don't have any edge cases yet but will mess with some numbers to see if I can find some.
– Hanzy
Jun 21 at 22:11




@Peilonrayz, I edited to provide an example solution from the example author. I don't have any edge cases yet but will mess with some numbers to see if I can find some.
– Hanzy
Jun 21 at 22:11










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Pure Python



  1. You should use a for loop rather than a while loop.

  2. You can use a generator comprehension to build all prob's.

  3. You can use sum to get the sum.

def grade_of_service(n, p, c):
return sum(binom(n, k)*(p**k)*((1-p)**(n-k)) for k in range(c+1, n+1))


Numpy and friends



  1. Use numpy.arange rather than range.

  2. Write out the equation same as above, just not in a comprehension.

  3. Change sum to numpy.sum.

import numpy


def grade_of_service(n, p, c):
k = numpy.arange(c+1, n+1)
return numpy.sum(binom(n, k)*(p**k)*((1-p)**(n-k)))


This has a problem with numbers that exceed a certain size, as numpy numbers are finite.






share|improve this answer





















  • Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
    – Hanzy
    Jun 21 at 22:17










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197013%2fgrade-of-service-probability-function-python%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Pure Python



  1. You should use a for loop rather than a while loop.

  2. You can use a generator comprehension to build all prob's.

  3. You can use sum to get the sum.

def grade_of_service(n, p, c):
return sum(binom(n, k)*(p**k)*((1-p)**(n-k)) for k in range(c+1, n+1))


Numpy and friends



  1. Use numpy.arange rather than range.

  2. Write out the equation same as above, just not in a comprehension.

  3. Change sum to numpy.sum.

import numpy


def grade_of_service(n, p, c):
k = numpy.arange(c+1, n+1)
return numpy.sum(binom(n, k)*(p**k)*((1-p)**(n-k)))


This has a problem with numbers that exceed a certain size, as numpy numbers are finite.






share|improve this answer





















  • Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
    – Hanzy
    Jun 21 at 22:17














up vote
1
down vote



accepted










Pure Python



  1. You should use a for loop rather than a while loop.

  2. You can use a generator comprehension to build all prob's.

  3. You can use sum to get the sum.

def grade_of_service(n, p, c):
return sum(binom(n, k)*(p**k)*((1-p)**(n-k)) for k in range(c+1, n+1))


Numpy and friends



  1. Use numpy.arange rather than range.

  2. Write out the equation same as above, just not in a comprehension.

  3. Change sum to numpy.sum.

import numpy


def grade_of_service(n, p, c):
k = numpy.arange(c+1, n+1)
return numpy.sum(binom(n, k)*(p**k)*((1-p)**(n-k)))


This has a problem with numbers that exceed a certain size, as numpy numbers are finite.






share|improve this answer





















  • Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
    – Hanzy
    Jun 21 at 22:17












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Pure Python



  1. You should use a for loop rather than a while loop.

  2. You can use a generator comprehension to build all prob's.

  3. You can use sum to get the sum.

def grade_of_service(n, p, c):
return sum(binom(n, k)*(p**k)*((1-p)**(n-k)) for k in range(c+1, n+1))


Numpy and friends



  1. Use numpy.arange rather than range.

  2. Write out the equation same as above, just not in a comprehension.

  3. Change sum to numpy.sum.

import numpy


def grade_of_service(n, p, c):
k = numpy.arange(c+1, n+1)
return numpy.sum(binom(n, k)*(p**k)*((1-p)**(n-k)))


This has a problem with numbers that exceed a certain size, as numpy numbers are finite.






share|improve this answer













Pure Python



  1. You should use a for loop rather than a while loop.

  2. You can use a generator comprehension to build all prob's.

  3. You can use sum to get the sum.

def grade_of_service(n, p, c):
return sum(binom(n, k)*(p**k)*((1-p)**(n-k)) for k in range(c+1, n+1))


Numpy and friends



  1. Use numpy.arange rather than range.

  2. Write out the equation same as above, just not in a comprehension.

  3. Change sum to numpy.sum.

import numpy


def grade_of_service(n, p, c):
k = numpy.arange(c+1, n+1)
return numpy.sum(binom(n, k)*(p**k)*((1-p)**(n-k)))


This has a problem with numbers that exceed a certain size, as numpy numbers are finite.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 21 at 22:11









Peilonrayz

24.3k336102




24.3k336102











  • Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
    – Hanzy
    Jun 21 at 22:17
















  • Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
    – Hanzy
    Jun 21 at 22:17















Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
– Hanzy
Jun 21 at 22:17




Thank you this will be more efficient in the case the variables get incredibly large, the generator will save me a lot of memory usage. In practical use if the probability meets a threshold is really all that matters, so as long as the precision is down to .001 it probably won’t make a difference. Since they are probabilities they will never exceed 1, and anything below .01 in practice probably doesn’t need to be that precise.
– Hanzy
Jun 21 at 22:17












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197013%2fgrade-of-service-probability-function-python%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation