CorrelationTTV (Easy version)

Exclusive Easy+ 2 Math MillenniumHRT

This is the easy version of RollingCorr. The only difference between this and the hard version are the constraints. Solving one problem does not guarantee solving the other.

This is an interactive problem.

Implement the class RollingCorr, which persistently calculates the Pearson correlation coefficient of a two-dimensional incoming data stream.

You will receive a sequence of pairs (xi,yi)(x_i, y_i). After each pair arrives, you must update your internal state and return the correlation of all points seen so far.

The interactor is non-adaptive, meaning the pair you receive is independent of your previous correlation calculation. However, the judge will immediately terminate if your correlation is incorrect.


Constructor

RollingCorr()

  • Your class must provide a constructor. It should initialize any internal state required to support persistent rolling updates.

    • (Details of what you initialize are intentionally left unspecified; choose what you need.)

Methods

update(double x, double y)

  • The judge will call update(x, y) repeatedly.

  • Each call appends the pair (x,y)(x,y) to the stream.

  • Return the Pearson correlation coefficient of all appended pairs so far (including the new one).

Pearson Correlation Coefficient

The correlation after nn points is:

r=i=1n(xixˉ)(yiyˉ)i=1n(xixˉ)2i=1n(yiyˉ)2r=\frac{\sum_{i=1}^{n}(x_i-\bar x)(y_i - \bar y)}{\sqrt{\sum_{i=1}^{n}(x_i-\bar x)^2}\cdot\sqrt{\sum_{i=1}^{n}(y_i-\bar y)^2}}


Notes

  • If fewer than two points have been seen, return 0.0.

  • If the correlation is undefined (for example, all xix_i are identical or all yiy_i are identical), return 0.0.

  • Your answer will be accepted if it is within 1e-5 absolute or relative error of the expected value.

  • You do not need to write any input/output parsing.

Constraints

  • x and y are finite double values.
  • |x|, |y| 100\leq 100
  • The number of updates may be at most 1e3.
Accepted 2/9
Acceptance 22%
Loading editor...
Sample Input:
Inserting data stream...
Expected Output: