STAT 540 Homework 6

Section 001, Fall 2026  |  Due: Wednesday, October 21, 2026 (before class)  |  Total Points: 80

Author

Your Name

Published

Assigned Oct 14, 2026

Important📅 Due: Wednesday, October 21, 2026 — before class

Compile your homework as a webpage (html). Then open it in a browser and print it to a .pdf file. Upload this pdf file and your Quarto (*.qmd) file to Blackboard. Please use the Python Quarto homework template (python_hw_template.qmd) to write your homework solutions. Work on the homework independently.

Instructions

This is the Python counterpart of Homework 1. Everything you need comes from Lectures 1–2 and the Python Data Science Handbook (Chapter 2, “Introduction to NumPy”).

  • Write your Python code (commands) for each task and present your results clearly — a code cell that runs but prints nothing earns no credit.
  • Answer the “Is it a …?” questions with evidence: run a function whose output settles the question, then state your conclusion in a sentence.
  • Start every code cell from the setup cell below; later parts reuse objects created in earlier parts.
  • Where a question asks you to find a function “on your own”, say in one sentence how you found it (help(), dir(), the docs, the textbook).
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Use this generator (and this seed) everywhere so your numbers are reproducible.
rng = np.random.default_rng(540)

Problem 1. NumPy arrays and their types (20 points)

Write your Python code for the following tasks and present your results clearly.

1(a) — 2 points

Generate an array x with 30 random numbers from the uniform distribution on \((0,1)\), i.e. \(U(0,1)\). Show the first 5 values of x you get. Hint: rng.uniform

# YOUR CODE HERE

1(b) — 3 points

Use built-in NumPy functions (find them on your own if you don’t know them yet) to calculate the sample mean, median, and standard deviation of x.

Note: np.std() divides by \(n\) by default, while the sample standard deviation divides by \(n-1\). Report the sample standard deviation and state which argument you used to get it (?np.std).

# YOUR CODE HERE

1(c) — 3 points

Run the command y = (x > 0.5). Is y a numerical array? Confirm or disprove it by executing some appropriate Python/NumPy functions, then write one sentence explaining what you found.

# YOUR CODE HERE

1(d) — 5 points

Run the command z = np.column_stack((x, y)). Is z a NumPy array or a pandas DataFrame? Is the second column of z a Boolean column or a numerical column? Show the output of pd.DataFrame(z).describe() and explain what happened to y when it was stacked with x.

# YOUR CODE HERE

1(e) — 3 points

What method or function can be used to change y into an array of strings? Demonstrate it and print the dtype of the result.

# YOUR CODE HERE

1(f) — 4 points

Run the command xx = np.diag(x). Is xx a one-dimensional array or a two-dimensional matrix? Find the “length” of xx by running both len(xx) and xx.size. Why do the two answers differ?

# YOUR CODE HERE

Problem 2. Lists, tuples and dictionaries (30 points)

2(a) — 6 points

Create a list scores holding the five numbers 88, 72, 95, 61, 79, and a tuple limits holding the two numbers 60 and 90. Print both, and print the type() of each.

# YOUR CODE HERE

2(b) — 8 points

Create a dictionary grades that maps each of the five student names "Ana", "Ben", "Cleo", "Dev", "Eli" to the corresponding score in scores. Print Cleo’s score by using her name as the key. Hint: zip

# YOUR CODE HERE

2(c) — 8 points

Using scores, print (i) the last element with negative indexing, (ii) the first three elements with slicing, and (iii) every other element starting from the second, using step slicing.

# YOUR CODE HERE

2(d) — 8 points

Run scores * 2 and np.array(scores) * 2. The two results are different — show both and explain in one or two sentences why.

# YOUR CODE HERE

Problem 3. Functions, conditionals and plotting (30 points)

3(a) — 12 points

Write a function summary_stats(a) that takes a NumPy array and returns a dictionary with keys "n", "mean", "sd", "min", and "max". Apply it to x from Problem 1 and print the dictionary.

def summary_stats(a):
    # YOUR CODE HERE
    pass

# YOUR CODE HERE

3(b) — 8 points

Write a function letter_grade(score) that returns

  • "A" if the score is 90 or above,
  • "B" if the score is at least 80 but below 90,
  • "C" if the score is at least 70 but below 80,
  • "F" otherwise.

Apply it to every value in scores from Problem 2 and print each student’s name with their letter grade.

def letter_grade(score):
    # YOUR CODE HERE
    pass

# YOUR CODE HERE

3(c) — 10 points

Make a histogram of x with 10 bins. Add a vertical dashed line at the sample mean, a title, and axis labels. Hint: ax.hist, ax.axvline

fig, ax = plt.subplots(figsize=(7, 4))

# YOUR CODE HERE

plt.tight_layout()
plt.show()


Before you submit

Due: Wednesday, October 21, 2026, before class.