Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is pearson correlation and a fixed rotation used in the confidence_ellipse function for visualizing covariance #1

Open
RBJin opened this issue Aug 28, 2024 · 0 comments

Comments

@RBJin
Copy link

RBJin commented Aug 28, 2024

Hi @DominikMuhle
Thanks for your work on this project. I noticed that the confidence_ellipse function uses pearson correlation and a fixed 45-degree rotation instead of calculating the ellipse from eigenvectors and eigenvalues. Could you clarify why this approach was chosen?
Thanks!

def confidence_ellipse(
    mean: Tuple[float, float],
    cov: torch.Tensor,
    ax: Axes,
    scale: float = 3.0,
    linewidth: float = 1.0,
    facecolor: str = "none",
    **kwargs
):
    """
    Create a plot of the covariance confidence ellipse of *x* and *y*.

    Parameters
    ----------
    x, y : array-like, shape (n, )
        Input data.

    ax : matplotlib.axes.Axes
        The axes object to draw the ellipse into.

    scale : float
        Scale the covariance sizes of all for better visualization.

    **kwargs
        Forwarded to `~matplotlib.patches.Ellipse`

    Returns
    -------
    matplotlib.patches.Ellipse
    """

    n_std = 3.0
    pearson = cov[0, 1] / torch.sqrt(cov[0, 0] * cov[1, 1])
    # Using a special case to obtain the eigenvalues of this
    # two-dimensional dataset.
    ell_radius_x = torch.sqrt(1 + pearson).item()
    ell_radius_y = torch.sqrt(1 - pearson).item()
    ellipse = Ellipse(
        (0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2, facecolor=facecolor, linewidth=linewidth, **kwargs
    )

    # Calculating the standard deviation of x from
    # the squareroot of the variance and multiplying
    # with the given number of standard deviations.
    scale_x = torch.sqrt(cov[0, 0]).item() * n_std * scale
    mean_x = mean[0]

    # calculating the standard deviation of y ...
    scale_y = torch.sqrt(cov[1, 1]).item() * n_std * scale
    mean_y = mean[1]

    transf = transforms.Affine2D().rotate_deg(45).scale(scale_x, scale_y).translate(mean_x, mean_y)

    ellipse.set_transform(transf + ax.transData)
    return ax.add_patch(ellipse)
    ```
@RBJin RBJin changed the title Clarification on Using Pearson Correlation and Fixed Rotation in confidence_ellipse Why is pearson correlation and a fixed rotation used in the confidence_ellipse function for visualizing covariance Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant