Withe help of @ Welcome to Stack Overflow, I managed to truncate the Poisson distribution using upper limit.When I used the function so called truncated Poisson, which is user defined function, it worked with single value entry, which I have shown in the code below:
import scipy.stats as sct
import pandas as pd
def truncated_Poisson(mu, max_value, size):
temp_size = size
while True:
temp_size *= 2
temp = sct.poisson.rvs(mu, size=temp_size)
truncated = temp[temp <= max_value]
if len(truncated) >= size:
return truncated[:size]
mu = 2.5
max_value = 10
print(truncated_Poisson(mu, max_value, 1))
Unfortunately, I threw me an error when I applied it in the data frame as follow:
data = pd.DataFrame()
data['Name'] = ['A','B','C','D','E']
data ['mu'] = [0.5,1.2,2,2.5,2.8]
max_value = 5
size = 1
data ['Pos'] = truncated_Poisson(data.mu,max_value,size = 1)
The error statement is
ValueError: size does not match the broadcast shape of the parameters.
Can anyone advise me how to use that function in dataframe?
Thanks
Zep.
As I understand, you want to call truncated_Poisson
with the same parameter and each of the mu
from the data. You can do this for example by using .apply
:
data['Pos'] = data.mu.apply(lambda mu: truncated_Poisson(mu, max_value, size=1))
>>> data
Name mu Pos
0 A 0.5 [0]
1 B 1.2 [0]
2 C 2.0 [3]
3 D 2.5 [4]
4 E 2.8 [3]