The Question
CodingVectorized Correlation Matrix Calculation
Implement a function to compute the Pearson correlation matrix for given datasets using NumPy. The function should accept a 2D array $X$ (size $N \times P$) and an optional 2D array $Y$ (size $N \times M$). If $Y$ is provided, return the cross-correlation matrix between the features of $X$ and $Y$ (size $P \times M$). If $Y$ is omitted, return the self-correlation matrix of features in $X$ (size $P \times P$). Ensure the implementation is vectorized to handle large datasets efficiently and handles cases where variance might be zero (avoiding division by zero).
Python
NumPy
Pearson Correlation
Vectorization