Covariance vs Correlation Scale Invariance

Quick Overview

Implement a pandas function that computes the covariance and Pearson correlation of two columns before and after multiplying one of them by a constant. It tests how unit changes affect each statistic and how well you handle edge cases such as a negative or zero factor.

Covariance vs Correlation Scale Invariance

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `cov_vs_corr_scale_invariance(df, col_a, col_b, factor)` to show that covariance changes with units while correlation does not. The function takes a DataFrame, two numeric column names and a float `factor`, and returns a `float64` `pd.Series` named `value`. Requirements: - The index is exactly `['cov_before', 'cov_after', 'corr_before', 'corr_after']`, in that order. - `cov_before` and `corr_before` are the covariance and Pearson correlation of `col_a` vs `col_b` as given. - `cov_after` and `corr_after` are the same two statistics after multiplying `col_a` by `factor`. - Use `Series.cov` and `Series.corr`. Example with `factor = 100` (dollars to cents on `col_a`): ```text cov_before 12000.0 cov_after 1200000.0 corr_before 0.87 corr_after 0.87 Name: value, dtype: float64 ``` Covariance grows by the factor of 100, and the correlation stays at 0.87. ```hint Where the factor goes Write correlation as covariance divided by the two standard deviations, then track what multiplying `col_a` by `factor` does to each of the three pieces. ``` ### Constraints & Assumptions - Both columns are numeric and have no missing values. - Covariance uses the pandas default `ddof=1`. ### What a Strong Answer Covers - Computes all four values with `Series.cov` and `Series.corr` and returns them in the pinned index order, as `float64`, named `value`. - Scales only `col_a` and leaves the caller's DataFrame unmodified. - Shows algebraically why covariance scales by `factor` and correlation does not. - Names the edge cases: a negative or zero `factor`, a constant column, and floating-point differences in the last digits. ### Follow-up Questions - Why does multiplying `col_a` by `factor` leave the correlation unchanged? - What if the analyst still wants a covariance they can compare across pairs of variables? - What happens to both statistics when `factor` is negative, or zero?

Overview: Implement a pandas function that computes the covariance and Pearson correlation of two columns before and after multiplying one of them by a constant. It tests how unit changes affect each statistic and how well you handle edge cases such as a negative or zero factor.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement cov_vs_corr_scale_invariance(df, col_a, col_b, factor) to show that covariance changes with units while correlation does not. The function takes a DataFrame, two numeric column names and a float factor, and returns a float64 pd.Series named value.

Requirements:

  • The index is exactly ['cov_before', 'cov_after', 'corr_before', 'corr_after'] , in that order.
  • cov_before and corr_before are the covariance and Pearson correlation of col_a vs col_b as given.
  • cov_after and corr_after are the same two statistics after multiplying col_a by factor .
  • Use Series.cov and Series.corr .

Example with factor = 100 (dollars to cents on col_a):

cov_before      12000.0
cov_after     1200000.0
corr_before         0.87
corr_after          0.87
Name: value, dtype: float64

Covariance grows by the factor of 100, and the correlation stays at 0.87.

Constraints & Assumptions

  • Both columns are numeric and have no missing values.
  • Covariance uses the pandas default ddof=1 .

What a Strong Answer Covers Guidance

  • Computes all four values with Series.cov and Series.corr and returns them in the pinned index order, as float64 , named value .
  • Scales only col_a and leaves the caller's DataFrame unmodified.
  • Shows algebraically why covariance scales by factor and correlation does not.
  • Names the edge cases: a negative or zero factor , a constant column, and floating-point differences in the last digits.

Follow-up Questions Guidance

  • Why does multiplying col_a by factor leave the correlation unchanged?
  • What if the analyst still wants a covariance they can compare across pairs of variables?
  • What happens to both statistics when factor is negative, or zero?