Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
Deep-ML | Truncated Gaussian: pdf, cdf and Mean via the Error FunctionTruncated Gaussian: pdf, cdf and Mean via the Error Function
Truncated Gaussian: pdf, cdf and Mean via the Error Function
Medium
Probability
Example:
Input:
```python
import math
s = truncnorm_stats(1.0, 0.0, 1.0, 0.0, math.inf)
print({k: round(v, 4) for k, v in s.items()})
```
With a = mu = 0 and b = infinity this is the half-normal: Z = 1/2, the pdf at 1 is 2 phi(1) = 0.4839, the cdf is 2 (Phi(1) - 1/2) = 0.6827, the mean is sqrt(2/pi) = 0.7979 and the variance is 1 - 2/pi = 0.3634.
Implement truncnorm_stats(x, mu, sigma, a, b) returning a dict with keys "pdf", "cdf", "mean", "var" (floats) for a scalar x, using only math.erf, math.exp, math.sqrt and math.pi (no scipy).
a may be -math.inf and b may be math.inf; treat ϕ(±∞)=0 and zϕ(z)=0 at z=±∞.
Outside the support the pdf is 0; the cdf is 0 for x≤a and 1 for x≥b.