BasicsAveragesGoogleMetaJane Street
Problem
Implement weighted_average(values, weights) returning ∑iwi∑iwixi as a Python float.
Why It Matters: Weighted averages appear everywhere from portfolio returns and course GPAs to attention pooling and inverse-variance meta-analysis.
Function Signature
import numpy as np
def weighted_average(values, weights) -> float:
Sample Input & Output
Example 1: Unnormalized Weights
Input:
values = [80.0, 90.0, 100.0], weights = [1.0, 2.0, 1.0]
Expected Output:
90.0
Explanation: (80*1 + 90*2 + 100*1) / (1 + 2 + 1) = 360 / 4 = 90.0.
Example 2: Heavy Weight on One Value
Input:
values = [10.0, 50.0], weights = [0.9, 0.1]
Expected Output:
14.0
Explanation: 10*0.9 + 50*0.1 = 14.0, pulled strongly toward 10.0.
Constraints & Edge Cases
- Return a Python
float(sum(weights) > 0). - Weights are non-negative with
sum(weights) > 0. - Notice weights do not have to sum to
1.0—always divide bysum(weights).
Constraints & Evaluation Criteria
- Return a Python `float` (`sum(weights) > 0`).
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
values=[80, 90, 100], weights=[0.2, 0.3, 0.5]
Expected Output / Criteria
93.0