Create solver_standard_deviation.py
Browse files- solver_standard_deviation.py +69 -0
solver_standard_deviation.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from statistics import pstdev
|
| 5 |
+
from typing import Optional, List
|
| 6 |
+
|
| 7 |
+
from models import SolverResult
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _nums(text: str) -> List[float]:
|
| 11 |
+
return [float(x) for x in re.findall(r"-?\d+(?:\.\d+)?", text)]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def solve_standard_deviation(text: str) -> Optional[SolverResult]:
|
| 15 |
+
lower = (text or "").lower()
|
| 16 |
+
|
| 17 |
+
if "standard deviation" not in lower and "std dev" not in lower:
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
nums = _nums(lower)
|
| 21 |
+
|
| 22 |
+
# Conceptual rule: adding same constant does not change SD
|
| 23 |
+
if any(p in lower for p in ["add the same", "increased by the same", "decreased by the same", "plus a constant"]):
|
| 24 |
+
result = "unchanged"
|
| 25 |
+
return SolverResult(
|
| 26 |
+
domain="quant",
|
| 27 |
+
solved=True,
|
| 28 |
+
topic="standard_deviation",
|
| 29 |
+
answer_value=result,
|
| 30 |
+
internal_answer=result,
|
| 31 |
+
steps=[
|
| 32 |
+
"Adding or subtracting the same constant shifts all values equally.",
|
| 33 |
+
"The spread does not change, so standard deviation is unchanged.",
|
| 34 |
+
],
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Conceptual rule: multiplying all by a constant scales SD
|
| 38 |
+
if any(p in lower for p in ["multiplied by", "scaled by", "increase by the same percent", "decrease by the same percent"]):
|
| 39 |
+
m = re.search(r"(multiplied by|scaled by)\s*(-?\d+(?:\.\d+)?)", lower)
|
| 40 |
+
if m:
|
| 41 |
+
factor = abs(float(m.group(2)))
|
| 42 |
+
return SolverResult(
|
| 43 |
+
domain="quant",
|
| 44 |
+
solved=True,
|
| 45 |
+
topic="standard_deviation",
|
| 46 |
+
answer_value=f"multiplied by {factor:g}",
|
| 47 |
+
internal_answer=f"multiplied by {factor:g}",
|
| 48 |
+
steps=[
|
| 49 |
+
"When every value is multiplied by a constant, standard deviation is multiplied by the absolute value of that constant.",
|
| 50 |
+
],
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Numeric SD if list-like
|
| 54 |
+
if len(nums) >= 2:
|
| 55 |
+
result = pstdev(nums)
|
| 56 |
+
return SolverResult(
|
| 57 |
+
domain="quant",
|
| 58 |
+
solved=True,
|
| 59 |
+
topic="standard_deviation",
|
| 60 |
+
answer_value=f"{result:g}",
|
| 61 |
+
internal_answer=f"{result:g}",
|
| 62 |
+
steps=[
|
| 63 |
+
"Find the mean.",
|
| 64 |
+
"Compute squared deviations from the mean.",
|
| 65 |
+
"Average them and take the square root.",
|
| 66 |
+
],
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return None
|