Homework

8.4.3. Homework#

  • 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.

1. A Valid Model?

A shipping team writes down this model for one delivery: on time with probability 0.80, 1 day late with probability 0.15, and 3 or more days late with probability 0.10. This is a valid probability model.

Hide code cell source

### Solution
# False
# The probabilities add up to 1.05. In a valid model, the probabilities of all outcomes add up to exactly 1.

2. Reading an Expected Value

The expected late-delivery credit is $1.75 per shipment. This means a typical shipment costs the store $1.75 in credits.

Hide code cell source

### Solution
# False
# Each shipment costs $0, $5, or $20. The expected value is the long-run average credit over many shipments, not the cost of a typical one.

3. Mean vs Median

A company pays most employees between $50,000 and $80,000, but a few executives earn over $1 million. The median salary describes a typical employee better than the mean salary does.

Hide code cell source

### Solution
# True
# The few very large salaries pull the mean up. The median is the middle value, so it is not affected by how large the top salaries are.

4. Two Surveys

A random sample of 100 customers shows 72% are satisfied. A second random sample of 100 customers, taken the same week, shows 68%. The difference shows that customer satisfaction changed.

Hide code cell source

### Solution
# False
# Two random samples from the same population give different results by chance. That is sampling variability, not evidence of a change.

5. Bigger Samples

If a manager surveys 400 customers instead of 100, the standard error of the sample proportion is cut in half.

Hide code cell source

### Solution
# True
# The standard error is proportional to 1 / sqrt(n). Four times the sample size gives half the standard error.
### 6. Return Rate
# Each row of orders is one order. Use the data to estimate probabilities.
#   1. Compute the proportion of orders that were returned.
#   2. Use the complement to compute the proportion that were kept.
# Print exactly:
# Return rate: 0.15
# Kept rate: 0.85
import numpy as np
import pandas as pd

orders = pd.DataFrame({
    "returned": ["No", "No", "Yes", "No", "No", "No", "No", "No", "Yes", "No",
                 "No", "No", "No", "No", "No", "Yes", "No", "No", "No", "No"],
})

### Your code starts here.



### Your code ends here.

Hide code cell source

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

orders = pd.DataFrame({
    "returned": ["No", "No", "Yes", "No", "No", "No", "No", "No", "Yes", "No",
                 "No", "No", "No", "No", "No", "Yes", "No", "No", "No", "No"],
})

return_rate = (orders['returned'] == 'Yes').mean()

print("Return rate:", return_rate)
print("Kept rate:", round(1 - return_rate, 2))
Return rate: 0.15
Kept rate: 0.85
### 7. Items per Order
# The warehouse recorded how many items were in each of 10 orders.
#   1. Use value_counts(normalize=True) to get the empirical distribution of items.
#   2. Print the proportion of orders with exactly 2 items.
#   3. Print the average number of items per order.
# Print exactly:
# Proportion with 2 items: 0.4
# Average items: 2.1
import numpy as np
import pandas as pd

items = pd.Series([1, 2, 2, 3, 1, 2, 4, 2, 1, 3])

### Your code starts here.



### Your code ends here.

Hide code cell source

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

items = pd.Series([1, 2, 2, 3, 1, 2, 4, 2, 1, 3])

distribution = items.value_counts(normalize=True)

print("Proportion with 2 items:", distribution[2])
print("Average items:", items.mean())
Proportion with 2 items: 0.4
Average items: 2.1
### 8. Call Wait Times
# A call center recorded wait times in minutes.
#   1. Compute the median wait time.
#   2. Compute the 90th percentile of wait times with np.percentile().
# Print exactly:
# Median wait: 4.5
# 90th percentile: 12.1
import numpy as np
import pandas as pd

waits = pd.Series([2, 3, 3, 4, 4, 5, 6, 7, 12, 13])

### Your code starts here.



### Your code ends here.

Hide code cell source

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

waits = pd.Series([2, 3, 3, 4, 4, 5, 6, 7, 12, 13])

print("Median wait:", waits.median())
print("90th percentile:", round(np.percentile(waits, 90), 1))
Median wait: 4.5
90th percentile: 12.1
### 9. One Large Order
# Daily revenue for a week, including one unusually large corporate order.
#   1. Compute the mean and median of revenue.
#   2. Drop the largest value and compute the mean and median again.
#   3. Which statistic changed more?
# Print exactly:
# Mean with large order: 1100.0
# Mean without: 600.0
# Median with large order: 600.0
# Median without: 600.0
import numpy as np
import pandas as pd

revenue = pd.Series([500, 650, 600, 550, 700, 600, 4100])

### Your code starts here.



### Your code ends here.

Hide code cell source

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

revenue = pd.Series([500, 650, 600, 550, 700, 600, 4100])

without = revenue[revenue != revenue.max()]

print("Mean with large order:", revenue.mean())
print("Mean without:", without.mean())
print("Median with large order:", revenue.median())
print("Median without:", without.median())
# The single large order moves the mean by 500 but does not move the median at all.
Mean with large order: 1100.0
Mean without: 600.0
Median with large order: 600.0
Median without: 600.0
### 10. Precision of an Average
# Order values have a standard deviation of $12. The standard error of a sample
# mean is sd / sqrt(n).
#   1. Compute the standard error of the mean order value for n = 36 orders.
#   2. Find the sample size that cuts that standard error in half, and compute its SE.
# Print exactly:
# SE with n=36: 2.0
# SE with n=144: 1.0
import numpy as np
import pandas as pd

sd = 12

### Your code starts here.



### Your code ends here.

Hide code cell source

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

sd = 12

print("SE with n=36:", sd / np.sqrt(36))
print("SE with n=144:", sd / np.sqrt(144))
SE with n=36: 2.0
SE with n=144: 1.0