Implement stationarity transforms and diagnostics in base R on EuStockMarkets

Quick Overview

Write base R functions that compute log returns, lagged differences, trailing rolling moments and block moments for a stock index series such as a column of EuStockMarkets. Tests input validation, window indexing, sample moments, exact return types and how to judge stationarity from the results.

Implement stationarity transforms and diagnostics in base R on EuStockMarkets

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write four functions in `/app/solution.R` that transform a numeric price series toward stationarity and check whether its moments drift over time. Use base R and `stats` only. A typical input is one column of the built-in `EuStockMarkets` dataset, such as `EuStockMarkets[, "DAX"]`. - `log_returns(prices)` returns `diff(log(prices))`. It calls `stop()` if `length(prices) < 2` or any price is `<= 0`. - `difference_series(x, lag = 1L, differences = 1L)` returns `as.numeric(diff(x, lag = lag, differences = differences))`. - `rolling_moments(x, window)` returns a `data.frame` with columns `end_index` (the values `window:length(x)`), `roll_mean` and `roll_sd` (sample standard deviation), each computed over the trailing window that ends at `end_index`. It calls `stop()` if `window < 2` or `window > length(x)`. - `block_stationarity(x, n_blocks)` splits `x` into contiguous blocks of size `length(x) %/% n_blocks`, drops the leftover elements at the end, and returns a `data.frame` with columns `block`, `mean` and `variance` (sample variance). It calls `stop()` if `n_blocks < 2` or the block size is below 2. The `block` and `end_index` columns are integer, and the moment columns are numeric. ```r log_returns(c(100, 110, 121)) # c(0.09531018, 0.09531018) difference_series(c(1, 3, 6, 10), lag = 1L, differences = 1L) # c(2, 3, 4) rolling_moments(c(1, 2, 3, 4), window = 2L) # end_index roll_mean roll_sd # 1 2 1.5 0.7071068 # 2 3 2.5 0.7071068 # 3 4 3.5 0.7071068 block_stationarity(c(1, 2, 3, 4, 5), n_blocks = 2L) # block mean variance # 1 1 1.5 0.5 # 2 2 3.5 0.5 # (the trailing element 5 is dropped) ``` ```hint Window indexing For the trailing window that ends at position `e`, work out its first index from `e` and `window`, then check it against the four-element example before writing the loop. ``` ```hint Return types `EuStockMarkets` columns are `ts` objects, and `diff()` keeps that class. Check the class and storage type of every value you return against the spec, including the integer columns. ``` ### Constraints & Assumptions - Base R and `stats` only, no external packages. - Inputs are numeric vectors or univariate `ts` objects. Outputs are plain numeric vectors or data frames. - Sample moments use the n-1 denominator, which is the default for `sd()` and `var()`. - Elements past `n_blocks * (length(x) %/% n_blocks)` are dropped from the block summary. ### What a Strong Answer Covers - Input guards that run before any arithmetic and match each `stop()` condition in the spec. - Correct trailing-window and block indexing, giving `length(x) - window + 1` rolling rows and `n_blocks` block rows. - Return types that match the spec: plain numeric vectors, integer `end_index` and `block`, sample moments. - Time and space complexity of the rolling computation. - How to read the rolling and block moments as a stationarity check on prices compared with log returns. ### Follow-up Questions - How would you statistically confirm stationarity beyond block moments? - Why prefer log returns over simple differences on prices? - How would you make `rolling_moments` efficient for very long series?

Overview: Write base R functions that compute log returns, lagged differences, trailing rolling moments and block moments for a stock index series such as a column of EuStockMarkets. Tests input validation, window indexing, sample moments, exact return types and how to judge stationarity from the results.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write four functions in /app/solution.R that transform a numeric price series toward stationarity and check whether its moments drift over time. Use base R and stats only. A typical input is one column of the built-in EuStockMarkets dataset, such as EuStockMarkets[, "DAX"].

  • log_returns(prices) returns diff(log(prices)) . It calls stop() if length(prices) < 2 or any price is <= 0 .
  • difference_series(x, lag = 1L, differences = 1L) returns as.numeric(diff(x, lag = lag, differences = differences)) .
  • rolling_moments(x, window) returns a data.frame with columns end_index (the values window:length(x) ), roll_mean and roll_sd (sample standard deviation), each computed over the trailing window that ends at end_index . It calls stop() if window < 2 or window > length(x) .
  • block_stationarity(x, n_blocks) splits x into contiguous blocks of size length(x) %/% n_blocks , drops the leftover elements at the end, and returns a data.frame with columns block , mean and variance (sample variance). It calls stop() if n_blocks < 2 or the block size is below 2.

The block and end_index columns are integer, and the moment columns are numeric.

log_returns(c(100, 110, 121))
# c(0.09531018, 0.09531018)

difference_series(c(1, 3, 6, 10), lag = 1L, differences = 1L)
# c(2, 3, 4)

rolling_moments(c(1, 2, 3, 4), window = 2L)
#   end_index roll_mean   roll_sd
# 1         2       1.5 0.7071068
# 2         3       2.5 0.7071068
# 3         4       3.5 0.7071068

block_stationarity(c(1, 2, 3, 4, 5), n_blocks = 2L)
#   block mean variance
# 1     1  1.5      0.5
# 2     2  3.5      0.5
# (the trailing element 5 is dropped)

Constraints & Assumptions

  • Base R and stats only, no external packages.
  • Inputs are numeric vectors or univariate ts objects. Outputs are plain numeric vectors or data frames.
  • Sample moments use the n-1 denominator, which is the default for sd() and var() .
  • Elements past n_blocks * (length(x) %/% n_blocks) are dropped from the block summary.

What a Strong Answer Covers Guidance

  • Input guards that run before any arithmetic and match each stop() condition in the spec.
  • Correct trailing-window and block indexing, giving length(x) - window + 1 rolling rows and n_blocks block rows.
  • Return types that match the spec: plain numeric vectors, integer end_index and block , sample moments.
  • Time and space complexity of the rolling computation.
  • How to read the rolling and block moments as a stationarity check on prices compared with log returns.

Follow-up Questions Guidance

  • How would you statistically confirm stationarity beyond block moments?
  • Why prefer log returns over simple differences on prices?
  • How would you make rolling_moments efficient for very long series?