Skip to content

Instantly share code, notes, and snippets.

@ishidur
Last active January 8, 2019 11:24
Show Gist options
  • Save ishidur/d4882fc50ba6545a76957d0e5372574c to your computer and use it in GitHub Desktop.
Save ishidur/d4882fc50ba6545a76957d0e5372574c to your computer and use it in GitHub Desktop.
局所的な平均化を繰り返すことで全体の平均値に収束するかの実験 #code
from functools import reduce
import random
import numpy as np
import matplotlib.pyplot as plt
print(plt.style.available)
plt.style.use('fivethirtyeight')
threshold = 0.8
def local_ave(l, ind):
# print(ind)
ave = reduce(lambda a, b: a + l[b], ind, 0) / len(ind)
for i in ind:
if random.random() < threshold:
l[i] = ave
if __name__ == '__main__':
n = np.random.rand(20)
iter = 30
print(np.mean(n))
xs = [[x] for x in n]
indxs = range(len(n))
t = np.arange(iter)
for x in t:
local_ave(n, random.sample(indxs, random.randrange(1, len(n))))
for i in range(len(n)):
xs[i].append(n[i])
print(n)
fig = plt.subplots(num=None, figsize=(10, 8), dpi=80,
facecolor='w', edgecolor='k')
t = np.append(t, iter + 1)
for i in range(len(n)):
plt.plot(t, xs[i], label="n%d" % (i + 1))
plt.xlabel('time')
plt.ylabel('value')
plt.tick_params(labelsize=18)
plt.grid()
plt.legend(loc="best")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment