Reputation: 1007
im new to python and im trying to solve this problem for school.
Airlines find that each passenger who reserves a seat fails to turn up with probability q independently of the other passengers. So Airline A always sell n tickets for their n−1 seat aeroplane while Airline B always sell 2n tickets for their n − 2 seat aeroplane. Which is more often over-booked?
This is my code:
import random
arrive = [True, False]
def airline_a(q, n, sims):
sim_counter = 0
reserved_seats = []
overbooked = 0
for x in range(n):
reserved_seats.append(x)
while sim_counter != sims:
passengers = 0
for x in range(len(reserved_seats)):
success = random.choices(arrive, weights=(100 - (q * 100), q * 100), k=1)
if success == True:
passengers += 1
if passengers > n - 1:
overbooked += 1
sim_counter += 1
print("Results: ")
print(f"Probability that Airline A is overbooked: ", overbooked / sims)
airline_a(.7, 100, 1000)
I am supposed to do a similar function for airline B and compare both values, however i came across an issue The problem is that the output is always 0. I dont quite understand what I'm doing wrong.
Upvotes: 2
Views: 228
Reputation: 1279
The main problem with the code is that you are trying to compare a list to a boolean beacuse the random.choice()
function return a list.
So, first of all you have to take the first element of the success
list:
success = random.choices(arrive, weights=(100 - (q * 100), q * 100), k=1)
if success[0]:
passengers += 1
or
success = random.choices(arrive, weights=(100 - (q * 100), q * 100), k=1)[0]
if success:
passengers += 1
The second thing to keep in mind, maybe the most important, is that you will always receive 0% as a result, this is because the way the logic is developed, it is statistically almost impossible that no individual does not show up and with only 1 individual who does not show up, in case A, there will not be people in overbookin
EXAMPLE
I performed some tests modifying the percentage of probability that a person will not show up.
I would like to mention that 0.7
is a 70%
probability and is very high in this context, this means that on 100 people, in average for every simulation those that will result present on the flight will be 30, that's why there are never overbooked people.
The first result other than 0.0%
I had, is by setting q = 0.05
and I received as a result an overbooking probability of 0.007
(which is 0.7%
).
PLANE B
I will not write you the solution to simulate the case of Company B, it is to modify 1 line of code and it is better that you do it since that' s an assignment. However I did some tests for this second case too, and here the situation becomes statistically much more interesting.
By setting a q = 0.4
we receive as probability of overbooking on average 0.97
(97%
) and with a q = 0.5
on average 0.45
(45%
).
Upvotes: 2
Reputation: 28595
the random.choices()
is returning a list, so it'll never be success == True
as it's returning success == ['True']
. Try fixing to this:
success = random.choices(arrive, weights=(100 - (q * 100), q * 100), k=1)[0]
And secondly, you might need to adjust your functionion slightly. Your max seats are 100...Then out of 100 you select .30 True, .70 False. To be overbooked you need ALL pasangers to come back as True (so that you'll have passengers > n -1 (which is 100 passengers > 99).
You'd have to change your q to be a very small probability (Ie, a high probablility a passanger DOES show so that you can get cases where all 100 show up).
Think of it this way, if you flip a coin 100 times, you need it to come up with 100 HEADS to be greater than 100-1. If the coin is weighted to give you .30 HEADS and .70 TAILS... while THEORETICALLY it could happen (you can calculate out that probability), you will "never" experimentally/simulate get 100 HEADS. You'd need something like a weight to be .999 HEADS and .001 TAILS for a chance to get 100 HEADS out of 100 tosses.
import random
arrive = [True, False]
def airline_a(q, n, sims):
sim_counter = 0
reserved_seats = []
overbooked = 0
for x in range(n):
reserved_seats.append(x)
alpha = 100 - (q * 100)
beta = q * 100
while sim_counter != sims:
passengers = 0
for x in range(len(reserved_seats)):
success = random.choices(arrive, weights=(alpha, beta), k=1)[0]
if success == True:
passengers += 1
if passengers > n - 1:
overbooked += 1
sim_counter += 1
print("Results: ")
print(f"Probability that Airline A is overbooked: ", overbooked / sims)
airline_a(.001, 100, 1000)
Gave me an output of:
Results:
Probability that Airline A is overbooked: 0.906
You can solve this mathematically by the way instead of a simulation.
Upvotes: 2
Reputation: 822
The problem with your code is this line: if success == True:
. If you try printing success
, it outputs either [True]
or [False]
. You can fix this by taking the first value of success
in the if
statement like this:
if success[0] == True:
passengers += 1
and to simplify the code a bit:
if success[0]:
passengers += 1
Change your code to the following to fix the error, however the count for the variable passengers
will remain below the variable n
, still resulting 0, so try and reduce the value of n or by reducing the value of q
:
import random
arrive = [True, False]
def airline_a(q, n, sims):
sim_counter = 0
reserved_seats = []
overbooked = 0
for x in range(n):
reserved_seats.append(x)
while sim_counter != sims:
passengers = 0
for x in range(len(reserved_seats)):
success = random.choices(arrive, weights=(100 - (q * 100), q * 100), k=1)
if success[0]:
passengers += 1
if passengers > n - 1:
overbooked += 1
sim_counter += 1
print("Results: ")
print(f"Probability that Airline A is overbooked: ", overbooked / sims)
airline_a(.7, 100, 1000)
For an explanation on random.choices()
visit the following link
Upvotes: 1