Problem · Math

Daily Temperature By Town

HardTwo Sigma logoTwo SigmaFULLTIMEOA

Problem statement

For this exercise, assume the input contains daily temperatures for NYC and six predictor towns named Town1 through Town6. Produce exactly six analysis rows.

  1. Among NYC and all six towns, find the place with the largest population standard deviation of temperature.
  2. Take the median NYC temperature among rows where Town2 is between 90 and 100, inclusive. Use 0 when there are no such rows, then round to the nearest integer.
  3. For each town independently, fit ordinary least squares with an intercept to predict NYC from that town. Sum the absolute values of the six slope coefficients, excluding intercepts, then round to the nearest integer.
  4. Find the single town whose intercept-based model has the smallest training mean squared error.
  5. Find the pair of towns whose joint intercept-based model has the smallest training mean squared error.
  6. Select five towns by forward greedy selection. Starting with no towns, repeatedly add the unused town that gives the smallest training mean squared error when fit jointly with the already selected towns and an intercept.

For every exact score tie, choose the lexicographically smaller place name or comma-joined town list. Round halfway values away from zero. All supplied predictor matrices have a unique least-squares solution.

Return columns metric, towns, and value in the exact row order above. Use null for towns on the two numeric-only rows and for value on the maximum-standard-deviation row. Pair names are comma-joined in lexicographic order; the five-town list is comma-joined in greedy selection order. The value for the last three selection rows is that model's training mean squared error.

Table schema

Pandas

Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.

temperatures

One row per observed day, with NYC as the regression target and six town predictors.

ColumnTypeNullableDescription
day_idPKIntegerNo
NYCDecimalNo
Town1DecimalNo
Town2DecimalNo
Town3DecimalNo
Town4DecimalNo
Town5DecimalNo
Town6DecimalNo

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
metricTextNo
townsTextYes
valueDecimalYes

Row order: must match exactly. Numeric tolerance: 0.000001.

Constraints

  • The temperatures table has between 8 and 1000 rows.
  • day_id values are distinct; input row order does not affect the result.
  • Every temperature is a finite decimal with absolute value at most 10^6.
  • NYC and all six town columns contain no missing values.
  • Every tested predictor matrix, including the intercept column, has a unique least-squares solution.
  • Decimal results are compared with absolute tolerance 10^-6.

More Two Sigma problems