harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0045harbor download openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0045Task: Implement an easier Python package at /app/ containing a lightweight statistics toolkit
Overview
You will create a small Python package named pcwawc (accessible as pcwawc.runningstats) with one module that provides three simple statistics utilities:
- RunningStats
- ColorStats
- MovingAverage
The goal is to provide clean, dependency-free implementations that are easy to understand and test. You will also place a minimal package initializer.
What to implement and behavior (simplified but practical)
1) Package layout
- Create a package named pcwawc accessible as pcwawc.runningstats
- File structure:
- /app/pcwawc/__init__.py (minimal)
- /app/pcwawc/runningstats.py (contains the implementations for all three classes)
2) Class: RunningStats
Purpose: maintain a running statistics summary for a stream of numbers.
Public API:
- __init__(self) -> None
- Initialize an empty statistics collector.
- push(self, value: float) -> None
- Incorporate a new value into the running statistics.
- mean(self) -> float
- Return the current mean of all pushed values. If no values have been pushed, return 0.0.
- variance(self) -> float
- Return the sample variance (i.e., divide by n-1). If fewer than 2 values have been pushed, return 0.0.
- standard_deviation(self) -> float
- Return the standard deviation corresponding to the variance.
Implementation notes (simplified but robust):
- Use a numerically stable incremental approach (Welford’s algorithm is fine; you can implement it directly).
- Keep track of count, running mean, and M2 to compute variance efficiently.
- Edge cases:
- If no values: mean = 0.0, variance = 0.0, standard_deviation = 0.0
- If only one value: mean equals that value; variance = 0.0; standard_deviation = 0.0
Example expectation (for guidance and tests):
- After pushing 17.0, 19.0, 24.0, mean should be 20.0; variance should be 13.0; standard_deviation ≈ 3.605551275
3) Class: ColorStats
Purpose: accumulate statistics separately for red, green, and blue channels.
Public API:
- __init__(self) -> None
- Initialize three independent RunningStats instances (one per channel).
- push(self, r: float, g: float, b: float) -> None
- Update each channel’s statistics with the corresponding value.
- mean(self) -> tuple of floats (mean_r, mean_g, mean_b)
- Return the per-channel means in the order (R, G, B).
- variance(self) -> tuple of floats (var_r, var_g, var_b)
- Return the per-channel variances in the same order.
- standard_deviation(self) -> tuple of floats (sd_r, sd_g, sd_b)
- Return the per-channel standard deviations in the same order.
Implementation notes:
- Use three independent RunningStats instances internally (one for each color channel).
4) Class: MovingAverage
Purpose: maintain a sliding moving average and provide a gradient measure that reflects the trend over the last up to window_size values.
Public API:
- __init__(self, window_size: int) -> None
- window_size > 0 defines how many recent values to consider.
- push(self, value: float) -> None
- Add a new value to the window (maintaining only the most recent window_size values).
- mean(self) -> float
- Return the mean of the current window. If the window is empty, return 0.0.
- gradient(self) -> float
- Return the gradient computed as the slope of the least-squares linear regression fit to the current window values.
- For a window with N values:
- Use x = 1, 2, ..., N and y = the window values in chronological order.
- Compute slope b via the standard least-squares formula.
- If N < 2, return 0.0 (no meaningful gradient).
Implementation notes (simplified):
- Maintain a fixed-size sliding window using collections.deque (maxlen = window_size) and keep a running sum for mean convenience (optional).
- When computing gradient, you can recompute sums from the current window values (N is at most window_size; this is fine for clarity).
- Use the standard formulas:
- Σy = sum of window values
- Σxy = sum(i * y_i) for i = 1..N
- Σx = N(N+1)/2
- Σx2 = N(N+1)(2N+1)/6
- slope b = (N * Σxy - Σx * Σy) / (N * Σx2 - (Σx)^2)
- If the denominator is 0 (e.g., all x are identical in degenerate cases), return 0.0.
Notes and hints
- The tests will import from pcwawc.runningstats import RunningStats, ColorStats, MovingAverage, so ensure the package exposes these classes via pcwawc/runningstats.py and a minimal __init__.py.
- All code should be pure Python with no external dependencies.
- Focus on clarity and correctness over micro-optimizations.
- The sample behavior to guide testing:
- RunningStats with inputs 17.0, 19.0, 24.0 should yield mean 20.0, variance 13.0, standard_deviation ≈ 3.605551275
- ColorStats should correctly aggregate three channels independently.
- MovingAverage with a window_size of 3 should produce a gradient sequence matching common simple linear regression with x = 1..N.
Deliverable
- A self-contained Python package located at /app/ containing:
- __init__.py (minimal)
- runningstats.py (containing the three classes: RunningStats, ColorStats, MovingAverage)
Difficulty adjustment notes
- This version reduces edge-case handling and complexity.
- It provides straightforward, well-documented implementations with clear guidance and a small API surface, suitable for quick understanding and testing.