TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Percentiles / Quantiles

Probability and Statistics
Easy

Compute requested percentiles with linear interpolation. After sorting nnn values, convert percentile qqq to a zero-based position:

r=q100(n−1)r = \frac{q}{100}(n-1)r=100q​(n−1)

Let l=⌊r⌋l=\lfloor r\rfloorl=⌊r⌋, u=⌈r⌉u=\lceil r\rceilu=⌈r⌉, and w=r−lw=r-lw=r−l. Interpolate between the sorted values:

Pq=(1−w)xl+wxuP_q = (1-w)x_l + wx_uPq​=(1−w)xl​+wxu​

Apply this calculation to every value in q and return a NumPy array in the same order as the requested percentiles.

Loading visualization...

Examples

Input: x = [1, 2, 3, 4], q = [25, 50, 75]

Output: [1.75, 2.5, 3.25]

Explanation: The three percentile positions fall between adjacent sorted values and are linearly interpolated.

Input: x = [1, 2, 3, 4, 5], q = [50]

Output: [3.0]

Input: x = [4, 1, 3, 2], q = [25, 75]

Output: [1.75, 3.25]

Hint 1

Use positions = q / 100.0 * (x.size - 1).

Hint 2

Use np.floor and np.ceil to locate the interpolation neighbors.

Requirements

  • Sort the data once
  • Compute zero-based percentile positions
  • Interpolate between the lower and upper neighboring values
  • Return a NumPy array

Constraints

  • x is a nonempty one-dimensional numeric list
  • Every value in q is between 0 and 100
  • Use NumPy only
Try Similar Problems
Mean Median ModeSample Var StdExpected Value DiscreteBootstrap MeanWinsorization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Percentiles / Quantiles

Probability and Statistics
Easy

Compute requested percentiles with linear interpolation. After sorting nnn values, convert percentile qqq to a zero-based position:

r=q100(n−1)r = \frac{q}{100}(n-1)r=100q​(n−1)

Let l=⌊r⌋l=\lfloor r\rfloorl=⌊r⌋, u=⌈r⌉u=\lceil r\rceilu=⌈r⌉, and w=r−lw=r-lw=r−l. Interpolate between the sorted values:

Pq=(1−w)xl+wxuP_q = (1-w)x_l + wx_uPq​=(1−w)xl​+wxu​

Apply this calculation to every value in q and return a NumPy array in the same order as the requested percentiles.

Loading visualization...

Examples

Input: x = [1, 2, 3, 4], q = [25, 50, 75]

Output: [1.75, 2.5, 3.25]

Explanation: The three percentile positions fall between adjacent sorted values and are linearly interpolated.

Input: x = [1, 2, 3, 4, 5], q = [50]

Output: [3.0]

Input: x = [4, 1, 3, 2], q = [25, 75]

Output: [1.75, 3.25]

Hint 1

Use positions = q / 100.0 * (x.size - 1).

Hint 2

Use np.floor and np.ceil to locate the interpolation neighbors.

Requirements

  • Sort the data once
  • Compute zero-based percentile positions
  • Interpolate between the lower and upper neighboring values
  • Return a NumPy array

Constraints

  • x is a nonempty one-dimensional numeric list
  • Every value in q is between 0 and 100
  • Use NumPy only
Try Similar Problems
Mean Median ModeSample Var StdExpected Value DiscreteBootstrap MeanWinsorization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.