The pseudocode for the numerically stable sample correlation coefficient rewritten in common math:
m x , i = { x 1 i = 2 m x , i − 1 + ( x i − m x , i − 1 ) / ( i − 1 ) i > 2 {\displaystyle m_{x,i}={\begin{cases}x_{1}&i=2\\m_{x,i-1}+(x_{i}-m_{x,i-1})/(i-1)&i>2\end{cases}}}
m y , i = { y 1 i = 2 m y , i − 1 + ( y i − m y , i − 1 ) / ( i − 1 ) i > 2 {\displaystyle m_{y,i}={\begin{cases}y_{1}&i=2\\m_{y,i-1}+(y_{i}-m_{y,i-1})/(i-1)&i>2\end{cases}}}
ρ = ∑ i = 2 N ( x i − m x , i ) ( y i − m y , i ) i − 1 i ∑ i = 2 N ( x i − m x , i ) ( x i − m x , i ) i − 1 i ∑ i = 2 N ( y i − m y , i ) ( y i − m y , i ) i − 1 i {\displaystyle \rho ={\frac {\sum _{i=2}^{N}{(x_{i}-m_{x,i})(y_{i}-m_{y,i}){\frac {i-1}{i}}}}{{\sqrt {\sum _{i=2}^{N}{(x_{i}-m_{x,i})(x_{i}-m_{x,i}){\frac {i-1}{i}}}}}{\sqrt {\sum _{i=2}^{N}{(y_{i}-m_{y,i})(y_{i}-m_{y,i}){\frac {i-1}{i}}}}}}}}