Correlation between absolute deviation and standard deviation

What is the correlation between a uniformly distributed random variable $$x$$ and $$y \sim |N(0, x)|$$?

Or what Pearson do I get for the following algorithm when $$n \to \infty$$?

import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import pearsonr
n= 10000
std = np.random.uniform(low=0.0001, high=10.0, size=(n, 1))
abs_deviation = np.abs(np.random.normal(loc=0.0, scale=std, size=(n, 1)))
plt.plot(std, abs_deviation, ".")
print(pearsonr(std.flatten(), abs_deviation.flatten()))
plt.xlabel("std")
plt.ylabel("abs_deviation")
plt.xlabel("std")
plt.ylabel("abs_deviation")


PearsonRResult(statistic=0.5503903841937862, pvalue=0.0)

• Can you tell us what the algorithm does? E.g. selects a uniform value in $[0.0001,10]$ and uses that as the standard deviation of of a normal random variable with mean $0$, and then the correlation between the absolute value of the normal random variable and the standard deviation? Commented Jun 28, 2023 at 12:43
• You may find it useful to know that a half-normal distribution has mean $\sigma \sqrt{\frac2\pi}$ and variance $\sigma^2\left(1-\frac2\pi\right)$ Commented Jun 28, 2023 at 12:48
• Your description of the algorithm is correct! Commented Jun 28, 2023 at 12:50

We have $$X \sim \mathrm{Unif}(a,b)$$, with $$a,b>0$$, and $$Y|X=x \sim \mathcal{N}(0, x^2)$$. Notice that we can write $$Y=Z X$$, where $$Z$$ is standard normal (and $$Z \perp \!\!\! \perp X$$).
The covariance we are looking for is $$\mathrm{Cov}(X,|Y|)=\mathrm{Cov}(X, |Z|X) = \mathbb{E}(X^2|Z|) - \mathbb{E}(X)\mathbb{E}(|Z| X) = \mathrm{Var}(X)\mathbb{E}(|Z|)$$ and the correlation is, thus, $$\mathrm{Corr}(X,|Y|)=\frac{ \mathrm{Var}(X)\mathbb{E}(|Z|)}{ \sqrt{\mathrm{Var}(X)\mathrm{Var}(|Y|)}}=\sqrt{\frac{\mathrm{Var}(X)}{\mathrm{Var}(|Y|)}}\mathbb{E}(|Z|)$$ and we can use standard distributional results to find the answer ($$|Z|$$ is chi-distributed).
Edit: for the final bit we also need: $$\mathrm{Var}(|Y|)=\mathrm{Var}(X|Z|)=\mathbb{E}(X^2 Z^2)-\mathbb{E}(X|Z|)^2= \mathbb{E}(X^2) \mathbb{E}(Z^2)-\mathbb{E}(X)^2\mathbb{E}(|Z|)^2$$