Skip to content

Instantly share code, notes, and snippets.

@benrosenberg
Created March 10, 2024 03:40
Show Gist options
  • Save benrosenberg/709a505e9caab8d1f72e479a6548c216 to your computer and use it in GitHub Desktop.
Save benrosenberg/709a505e9caab8d1f72e479a6548c216 to your computer and use it in GitHub Desktop.
n-adic means for various distributions
# histograms of various distributions
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['mathtext.fontset'] = 'cm'
normal_50 = np.random.normal(50, scale=13, size=100000)
normal_30 = np.random.normal(30, scale=10, size=100000)
normal_70 = np.random.normal(70, scale=10, size=100000)
exponential = np.random.exponential(scale=12, size=100000)
gumbel = np.random.gumbel(loc=20, scale=8, size=100000)
uniform = np.random.uniform(0, 100, size=100000)
samples = [
(normal_30, 'normal $\mu=30$'),
(normal_50, 'normal $\mu=50$'),
(normal_70, 'normal $\mu=70$'),
(exponential, 'exponential'),
(gumbel, 'gumbel'),
(uniform, 'uniform')
]
for sample, _ in samples:
sample = [s for s in sample if 0 <= s <= 100]
plt.hist(sample, histtype='step', bins=1000)
plt.legend([sample[1] for sample in samples])
plt.title('Distributions')
plt.yticks(range(0, 101, 10))
plt.show()
# n-adic mean simulation
import numpy as np
import sys
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['mathtext.fontset'] = 'cm'
normal_50 = np.random.normal(50, scale=13, size=10000)
normal_30 = np.random.normal(30, scale=10, size=10000)
normal_70 = np.random.normal(70, scale=10, size=10000)
exponential = np.random.exponential(scale=12, size=10000)
gumbel = np.random.gumbel(loc=20, scale=8, size=10000)
uniform = np.random.uniform(0, 100, size=10000)
def mean(lst,p=1):
return (sum(x**p for x in lst) / len(lst))**(1/p)
def power_means(sample):
plt.plot([mean(sample, p) for p in range(1,200)])
plt.xlabel('$p$')
samples = [
(normal_30, 'normal $\mu=30$'),
(normal_50, 'normal $\mu=50$'),
(normal_70, 'normal $\mu=70$'),
(exponential, 'exponential'),
(gumbel, 'gumbel'),
(uniform, 'uniform')
]
for sample, _ in samples:
sample = [s for s in sample if 0 <= s <= 100]
power_means(sample)
plt.legend([sample[1] for sample in samples])
plt.title('Power means')
plt.yticks(range(0, 101, 10))
plt.show()
@benrosenberg
Copy link
Author

Simple uniform n-adic means, for p between 0 and 150
Simple uniform n-adic means, for p between 0 and 150

@benrosenberg
Copy link
Author

Distribution histograms
distributions

@benrosenberg
Copy link
Author

Power means for each of the above distributions
power_means

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment