Created
July 9, 2016 14:49
-
-
Save swairshah/227f056e6acee07db6778c3ae746685b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy.random import uniform as unif | |
def euclidean_dist(x,y): | |
return np.sqrt(sum((x-y)**2)) | |
def method1(n, N = 10000): | |
dist = 0 | |
for i in range(N): | |
x = np.array([unif(0,1) for i in range(n)]) | |
y = np.array([unif(0,1) for i in range(n)]) | |
dist = (dist*i + euclidean_dist(x,y))/(i+1.0) | |
return dist | |
def method2(n, N = 10000): | |
dist = 0 | |
for i in range(N): | |
x = unif(0,1,n) | |
y = unif(0,1,n) | |
dist = (dist*i + euclidean_dist(x,y))/(i+1.0) | |
return dist | |
d1 = [] | |
d2 = [] | |
for i in range(100): | |
d1.append(method1(2)) | |
d2.append(method2(2)) | |
print ('d1', np.average(d1), np.std(d1)) | |
print ('d2', np.average(d2), np.std(d2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment