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 | Fit Gaussian, Laplace and Student-t to Data with OutliersFit Gaussian, Laplace and Student-t to Data with Outliers
Fit Gaussian, Laplace and Student-t to Data with Outliers
HardStatistics
Example:
Input:
```python
x = [-1.0, -0.5, 0.0, 0.1, 0.6, 20.0]
r = robust_fits(x)
for k in ('gaussian', 'laplace', 'student'):
print(k, [round(v, 4) for v in r[k]])
```
Five points near 0 and one at 20: the Gaussian mean jumps to 3.3 with a scale of 7.4, while the median-based Laplace fit stays at 0.05 and the Student EM down-weights the outlier to keep its location near 0.
Fit three location-scale models to a 1-D sample by maximum likelihood and compare how far the outliers drag each one.
Implement robust_fits(x, nu=4.0, iters=200) returning a dict with keys "gaussian", "laplace", "student", each mapped to a tuple (loc, scale, loglik) of floats, where loglik is the total log-likelihood of x under the fitted model:
GaussianN(μ,σ2): the MLE, with σ the standard deviation computed with divisor N.
Laplace2b1exp(−∣y−μ∣/b): the MLE, μ = the sample median (np.median, which averages the two middle values for even N) and b = the mean absolute deviation from that median.
Student t with fixed degrees of freedom ν, density
T(y∣μ,σ2,ν)=Γ(2ν)νπσΓ(2ν+1)[1+ν1(σy−μ)2]−2ν+1
fit by the EM algorithm for its Gaussian scale-mixture form. Start from μ = the sample median and σ = the Gaussian MLE standard deviation, then run exactly iters iterations of:
E-step: weights wn=ν+rn2ν+1 with rn=(xn−μ)/σ (current μ,σ).
M-step: μ←∑nwnxn/∑nwn, then σ2←N1∑nwn(xn−μ)2 using the new μ.