Skip to content

Beta, Correlation and Covariance accept the period their message forbids #9725

Description

@mkzung

Expected Behavior

All three raise on a period of two, which is what their comment and their message both say.

Actual Behavior

Indicators/Correlation.cs:63:

// Assert the period is greater than two, otherwise the correlation can not be computed
if (period < 2)
{
    throw new ArgumentException($"Period parameter for Correlation indicator must be greater than 2 but was {period}");
}

The comment says three, the message says three, the condition admits two. Beta.cs:62 and Covariance.cs:55 are the same three lines. ValueAtRisk.cs:61 states the identical bound and tests period < 3.

A two point window has nothing to measure. Fed ten bars of two series that are not proportional, Correlation at period 2 returns 1 or -1 on every one of its nine readings; the same series at period 3 gives values between -0.40 and 0.33.

period  2: 1 -1 -1 -1 -1 -1 -1 -1 -1
period  3: 0.3273 -0.2402 -0.2402 -0.3974 -0.2895 -0.2895 -0.2895 -0.2895
period  4: 0.3586 0 -0.049 -0.0804 -0.0476 -0.0476 -0.0476
period 10: 0.749

So the value is the sign of the last move, whatever the data is.

Potential Solution

period < 3 in all three, matching the message and ValueAtRisk. Nothing in Tests/, Algorithm.CSharp/ or Algorithm.Python/ constructs any of the three with 2; the smallest literal in use is 3.

Reproducing the Problem

var correlation = new Correlation(Symbols.SPY, Symbols.AAPL, 2);
// feed any two non-proportional series; every reading is 1 or -1

System Information

master, net10.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions