Lab

8.4.2. Lab#

  • Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.

  • Sign in from the account menu before you start working on the assignment.

  • Write and run your code directly in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

### Question 1: Checking a Probability Model
# The support team models how long a ticket takes to resolve.
#   1. Check the model: add up the probability column, rounded to 2 decimals.
#   2. Use the complement to find the probability that a ticket is NOT
#      resolved the same day, rounded to 2 decimals.
# Print exactly:
# Total probability: 1.0
# P(not same day): 0.45
import numpy as np
import pandas as pd

tickets = pd.DataFrame({
    "outcome": ["same day", "next day", "2+ days"],
    "probability": [0.55, 0.30, 0.15],
})
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

tickets = pd.DataFrame({
    "outcome": ["same day", "next day", "2+ days"],
    "probability": [0.55, 0.30, 0.15],
})

total = round(tickets['probability'].sum(), 2)
p_same_day = tickets.loc[tickets['outcome'] == 'same day', 'probability'].sum()
p_not_same_day = round(1 - p_same_day, 2)

print("Total probability:", total)
print("P(not same day):", p_not_same_day)
Total probability: 1.0
P(not same day): 0.45
### Question 2: Expected Revenue
# New subscribers choose one of three plans with the probabilities below.
#   1. Compute the expected monthly revenue per subscriber: the sum of
#      price times probability, rounded to 2 decimals.
#   2. Use it to estimate the monthly revenue from 2,000 new subscribers.
# Print exactly:
# Expected revenue per subscriber: 24.5
# Expected revenue from 2000 subscribers: 49000.0
import numpy as np
import pandas as pd

plans = pd.DataFrame({
    "plan": ["Basic", "Plus", "Pro"],
    "price": [10, 25, 60],
    "probability": [0.5, 0.3, 0.2],
})
subscribers = 2000
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

plans = pd.DataFrame({
    "plan": ["Basic", "Plus", "Pro"],
    "price": [10, 25, 60],
    "probability": [0.5, 0.3, 0.2],
})
subscribers = 2000

per_subscriber = round((plans['price'] * plans['probability']).sum(), 2)

print("Expected revenue per subscriber:", per_subscriber)
print("Expected revenue from", subscribers, "subscribers:", per_subscriber * subscribers)
Expected revenue per subscriber: 24.5
Expected revenue from 2000 subscribers: 49000.0
### Question 3: Mean or Median?
# Operations wants one number for a "typical" delivery time (in days).
#   1. Compute the mean and the median of days.
#   2. If the mean is more than 20% above the median, the data are skewed
#      to the right, so report "median" as the typical value; otherwise report "mean".
# Print exactly:
# Mean: 6.5
# Median: 4.0
# Typical value: median
import numpy as np
import pandas as pd

deliveries = pd.DataFrame({
    "days": [3, 4, 4, 3, 5, 4, 22, 3, 6, 11],
})
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

deliveries = pd.DataFrame({
    "days": [3, 4, 4, 3, 5, 4, 22, 3, 6, 11],
})

mean_days = deliveries['days'].mean()
median_days = deliveries['days'].median()

if mean_days > 1.2 * median_days:
    typical = "median"
else:
    typical = "mean"

print("Mean:", mean_days)
print("Median:", median_days)
print("Typical value:", typical)
Mean: 6.5
Median: 4.0
Typical value: median
### Question 4: Spread of Daily Sales
# A store manager wants to know how much daily sales vary.
#   1. Compute the 25th and 75th percentiles of sales with np.percentile().
#   2. Compute the interquartile range (IQR): 75th percentile minus 25th percentile.
#   3. Compute the standard deviation with np.std(), rounded to 1 decimal.
# Print exactly:
# 25th percentile: 1125.0
# 75th percentile: 1475.0
# IQR: 350.0
# Standard deviation: 189.7
import numpy as np
import pandas as pd

daily = pd.DataFrame({
    "sales": [1000, 1200, 1100, 1500, 1300, 1400, 1600, 1300, 1500, 1100],
})
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

daily = pd.DataFrame({
    "sales": [1000, 1200, 1100, 1500, 1300, 1400, 1600, 1300, 1500, 1100],
})

q25 = np.percentile(daily['sales'], 25)
q75 = np.percentile(daily['sales'], 75)

print("25th percentile:", q25)
print("75th percentile:", q75)
print("IQR:", q75 - q25)
print("Standard deviation:", round(np.std(daily['sales']), 1))
25th percentile: 1125.0
75th percentile: 1475.0
IQR: 350.0
Standard deviation: 189.7
### Question 5: Simulating the Standard Error
# These are the values (in dollars) of 40 recent orders. Treat them as the population.
#   1. Using the rng below, draw 1,000 random samples of 25 orders each, with replacement,
#      in one call: rng.choice(orders, size=(1000, 25)). Each row is one sample.
#      Compute the mean of each row with .mean(axis=1).
#   2. Compute the mean of the 1,000 sample means, rounded to 2 decimals.
#   3. Compute the simulated standard error: the standard deviation of the 1,000
#      sample means with ddof=1, rounded to 2 decimals.
#   4. Compute the formula standard error: np.std(orders) / np.sqrt(25), rounded to 2 decimals.
# Print exactly:
# Mean of sample means: 41.13
# Simulated SE: 4.33
# Formula SE: 4.38
import numpy as np
import pandas as pd

orders = np.array([42, 18, 65, 30, 27, 55, 21, 38, 90, 24, 33, 47, 15, 29, 60, 36, 22, 51, 26, 120,
                   34, 19, 44, 28, 73, 31, 25, 39, 58, 23, 35, 48, 20, 66, 32, 27, 41, 85, 30, 37])
rng = np.random.default_rng(8)
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

orders = np.array([42, 18, 65, 30, 27, 55, 21, 38, 90, 24, 33, 47, 15, 29, 60, 36, 22, 51, 26, 120,
                   34, 19, 44, 28, 73, 31, 25, 39, 58, 23, 35, 48, 20, 66, 32, 27, 41, 85, 30, 37])
rng = np.random.default_rng(8)

samples = rng.choice(orders, size=(1000, 25))      # 1,000 samples, one per row
sample_means = samples.mean(axis=1)

print("Mean of sample means:", round(sample_means.mean(), 2))
print("Simulated SE:", round(sample_means.std(ddof=1), 2))
print("Formula SE:", round(np.std(orders) / np.sqrt(25), 2))
# The simulated and formula standard errors agree closely: sample means of 25 orders
# vary about $4.4 around the population mean, far less than single orders (SD about $21.9).
Mean of sample means: 41.13
Simulated SE: 4.33
Formula SE: 4.38