Sample Standard Deviation From Scratch

Basics
BasicsStandard DeviationGoogleMetaJane Street

Problem

Implement sample_std(x) returning the sample standard deviation s=1n−1∑i=1n(xi−xˉ)2s = \sqrt{\frac{1}{n-1}\sum_{i=1}^n (x_i - \bar{x})^2} (ddof=1) as a Python float for an array of length >= 2.

Why It Matters: Standard deviation expresses spread in the same physical units as the data (dollars, milliseconds, meters) and drives z-scores and confidence intervals.

Function Signature

import numpy as np

def sample_std(x) -> float:

Sample Input & Output

Example 1: Standard Sample

Input:

x = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]

Expected Output:

2.13809

Explanation: Mean is 5.0, sum of squared deviations is 32.0, so s=32/7≈2.13809s = \sqrt{32 / 7} \approx 2.13809.

Example 2: Two-Element Sample

Input:

x = [10.0, 14.0]

Expected Output:

2.828427

Explanation: Deviations from mean 12.0 are ±2.0; s=(4+4)/1=8≈2.828427s = \sqrt{(4 + 4)/1} = \sqrt{8} \approx 2.828427.

Constraints & Edge Cases

  • Return unbiased sample standard deviation (ddof=1) as a float.
  • Remember np.std defaults to ddof=0—you MUST pass ddof=1 for sample standard deviation.
  • Return a Python float.

Constraints & Evaluation Criteria

  • Return unbiased sample standard deviation (`ddof=1`) as a float.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[10, 12, 14, 16]
Expected Output / Criteria
2.5820