Skip to content

Instantly share code, notes, and snippets.

@KulikDM
Created October 19, 2022 18:14
Show Gist options
  • Save KulikDM/18b8369a2d1930f972e452a5a9898bde to your computer and use it in GitHub Desktop.
Save KulikDM/18b8369a2d1930f972e452a5a9898bde to your computer and use it in GitHub Desktop.
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
def weights(vals):
norm = (vals-np.min(vals))/(np.max(vals)-np.min(vals))
return norm/np.sum(norm)
x1 = np.linspace(0, 1, 1000)
# Squared weights
y1 = (x1**2)
y1 = weights(np.max(y1)-y1)
# Cubic weights
y2 = (x1**4)
y2 = weights(np.max(y2)-y2)
# Gaussian weights
x2 = np.linspace(stats.halfnorm.ppf(0.01),
stats.halfnorm.ppf(0.95), 1000)
rv = stats.halfnorm()
y3 = weights(rv.pdf(x2))
# Discrete weights
y4 = x1.copy()
y4[0:801] = 0.8
y4[801:] = 0.2
y4 = weights(y4)
# Piecewise weights
xp1 = np.linspace(stats.halfnorm.ppf(0.01),
stats.halfnorm.ppf(0.95), 800)
yp1 = rv.pdf(xp1)
yp1 = (yp1-np.min(yp1))/(np.max(yp1)-np.min(yp1))*0.9
xp2 = np.linspace(stats.halfnorm.ppf(0.01),
stats.halfnorm.ppf(0.95), 200)
yp2 = rv.pdf(xp2)
yp2 = (yp2-np.min(yp2))/(np.max(yp2)-np.min(yp2))*0.1
y5 = y3.copy()
y5[0:800] = yp1+0.9
y5[800:] = yp2+0.1
y5 = y5/np.sum(y5)
x1 = (x1-np.min(x1))/(np.max(x1)-np.min(x1))
x2 = (x2-np.min(x2))/(np.max(x2)-np.min(x2))
fig, ax = plt.subplots(2, sharex=True,sharey=True, figsize=(8,8))
ax[0].plot(x1, y1, 'b', label='Squared')
ax[0].plot(x1, y2, 'r', label='Cubic')
ax[0].plot(x2, y3, 'y', label='Gaussian')
ax[0].legend(loc='upper right')
ax[1].plot(x1, y4, 'c', label='Discrete')
ax[1].plot(x1, y5, 'm', label='Piecewise Gaussian')
ax[1].legend(loc='upper right')
fig.supxlabel('Outlier Likelihood')
fig.supylabel('Weighting')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment