# define square shape: def square_fn(centre,width,height,x): if (centre - width/2) <= x <= (centre + width/2): return height return 0 # define bell curve: def bell_fn(centre,width,height,x): return height*math.exp(- (x - centre)**2/2*width) # define Euclidean Distance function: def ED(f,g): if len(f) != len(g): print("different length vectors!") return 0 return math.sqrt(sum((f[k] - g[k])**2 for k in range(len(f)))) # define Guassian simm: # guassian-simm(s,f,g) = exp(-||f - g||^2/2s) def gauss_simm(s,f,g): return math.exp(-ED(f,g)**2/2*s) # print out the results: f = [square_fn(15,20,10,k) for k in range(100)] g = [bell_fn(15,0.01,10,k) for k in range(100)] print("Gauss simm:",round(gauss_simm(0.001,f,g),4)) print("simm:",round(list_simm([1],f,g),4)) print("scaled simm:",round(rescaled_list_simm([1],f,g),4))Now, some examples:

f = [square_fn(15,20,10,k) for k in range(100)]

g = [bell_fn(15,0.01,10,k) for k in range(100)]

Gauss simm:0.8583

simm: 0.752

scaled simm: 0.752

f = [square_fn(15,20,10,k) for k in range(100)]

g = [bell_fn(35,0.01,10,k) for k in range(100)]

Gauss simm:0.2208

simm: 0.1698

scaled simm: 0.1698

f = [square_fn(15,20,10,k) for k in range(100)]

g = [bell_fn(75,0.01,10,k) for k in range(100)]

Gauss simm:0.1443

simm: 0.0

scaled simm: 0.0

Now some notes:

1) I used s = 0.001 in the Gaussian simm. So you may want to choose other values for that.

2) I rounded the results to 4 digits

3) In this case simm and scaled simm give essentially the same answer. This is because w*f is pretty close to w*g even without rescaling:

wf: 210

wg: 248.9

OK. Let's redo the examples with a smaller bell curve for curve g (we just drop the height from 10 to 1):

f = [square_fn(15,20,10,k) for k in range(100)]

g = [bell_fn(15,0.01,1,k) for k in range(100)]

Gauss simm: 0.4141

simm: 0.0843

scaled simm: 0.752

f = [square_fn(15,20,10,k) for k in range(100)]

g = [bell_fn(35,0.01,1,k) for k in range(100)]

Gauss simm: 0.3619

simm: 0.0203

scaled simm: 0.1698

f = [square_fn(15,20,10,k) for k in range(100)]

g = [bell_fn(75,0.01,1,k) for k in range(100)]

Gauss simm: 0.3469

simm: 0.0

scaled simm: 0.0

And now the difference between simm and scaled simm is stark. Simm is now much smaller, but scaled simm gives the exact same answer as in the previous examples (as we expect!).

And that's it for this post.

Home

previous: a list implementation of the simm

next: a superposition implementation of simm

updated: 19/12/2016

by Garry Morrison

email: garry -at- semantic-db.org