# Thread: Requesting help understanding an algorithm involving normalised vector

1. ## Requesting help understanding an algorithm involving normalised vector

Hi everyone,

I am new here. I stumbled upon this forum while trying to figure out an algorithm that involves vector and normalised difference between the vector. The equation is given as

Sr = 1 - ( || r1 - r2|| / || r1 + r2|| )

A given example gives r1 and r2 as shown below.

r1 = {1 2 3 4 5 6 0 3 3 0 0 0 1 1}
r2 = {4 0 3 0 0 0 1 2 3 5 6 7 8 9}

Sr = 0.2023

It would be really great if anyone can explain or show me how to get the answer Sr = 0.2023. Thank you very much!

2. ## Re: Requesting help understanding an algorithm involving normalised vector

You get this result if you add and subtract the vectors component-wise (i.e., $\displaystyle (x_1,\dots,x_n)+(y_1,\dots,y_n)=(x_1+y_1,\dots,x_n +y_n)$) and $\displaystyle \|(x_1,\dots,x_n)\|=\sqrt{x_1^2+\dots+x_n^2}$.

3. ## Re: Requesting help understanding an algorithm involving normalised vector

Thanks for the quick reply. I understand the first part that I should add and subtract the vector as give below.

r1 - r2 = (x1 - y1, . . . , xn - yn)
r1 + r2 = (x1 + y1, . . . , xn + yn)

But, I am not sure understand the second part though. Is the equation below correct?

||r1 - r2|| = || sqrt{(x12 - y12)+ . . . + (xn2 - yn2)} ||
||r1 + r2|| = || sqrt{(x12 + y12)+ . . . + (xn2 + yn)} ||

If that is correct, then I still cannot get the answer as 0.2023.

4. ## Re: Requesting help understanding an algorithm involving normalised vector

Originally Posted by Eiko
But, I am not sure understand the second part though. Is the equation below correct?

||r1 - r2|| = || sqrt{(x12 - y12)+ . . . + (xn2 - yn2)} ||
||r1 + r2|| = || sqrt{(x12 + y12)+ . . . + (xn2 + yn)} ||
No. To evaluate $\displaystyle \|r_1-r_2\|$, first find $\displaystyle r=r_1-r_2$ and then, if $\displaystyle r=(x_1,\dots,x_n)$, find $\displaystyle \|r\|$ according to the formula $\displaystyle \|r\|=\sqrt{x_1^2+\dots+x_n^2}$.

Edit: Replaced $\displaystyle \|r_1=r_2\|$ with $\displaystyle \|r_1-r_2\|$.

5. ## Re: Requesting help understanding an algorithm involving normalised vector

Hi emakarov,

I've tried that as well and the answers are still different from the given answer of 0.2023. It's difficult to explain the equation without being able to insert the proper symbols or insert images. But, basically I am trying to find

1 - ( ||r1 - r2|| divide by ||r1 + r2|| ) for r1 and r2.

It's actually an algorithm proposed to check for semantic similarity for sentences. If it would help explain my case, I posted a link to the paper. It's equation 8 on page 6.

Any help would really be great at this point.

6. ## Re: Requesting help understanding an algorithm involving normalised vector

First, I made a small correction to post #4.

I'll post the output of the program that computes Sr.

r₁
Code:
[1.; 2.; 3.; 4.; 5.; 6.; 0.; 3.; 3.; 0.; 0.; 0.; 1.; 1.]
r₂
Code:
[4.; 0.; 3.; 0.; 0.; 0.; 1.; 2.; 3.; 5.; 6.; 7.; 8.; 9.]
r₁ + r₂
Code:
[5.; 2.; 6.; 4.; 5.; 6.; 1.; 5.; 6.; 5.; 6.; 7.; 9.; 10.]
r₁ - r₂
Code:
[-3.; 2.; 0.; 4.; 5.; 6.; -1.; 1.; 0.; -5.; -6.; -7.; -7.; -8.]
Squares of elements of r₁ + r₂
Code:
[25.; 4.; 36.; 16.; 25.; 36.; 1.; 25.; 36.; 25.; 36.; 49.; 81.; 100.]
Squares of elements of r₁ - r₂
Code:
[9.; 4.; 0.; 16.; 25.; 36.; 1.; 1.; 0.; 25.; 36.; 49.; 49.; 64.]
Sum of squares of elements of r₁ + r₂ is 495

Sum of squares of elements of r₁ - r₂ is 315

||r₁ + r₂|| = sqrt(495) = 22.248595461286989

||r₁ - r₂|| = sqrt(315) = 17.7482393492988493

||r₁ - r₂|| / ||r₁ + r₂|| = 0.79772403521746571

1 - ||r₁ - r₂|| / ||r₁ + r₂|| = 0.20227596478253429

7. ## Re: Requesting help understanding an algorithm involving normalised vector

first we need to find r1 - r2 and r1 + r2.

r1 - r2 = (-3,2,0,4,5,6,-1,1,0,-5,-6,-7,-7,-8)

r1 + r2 = (5,2,6,4,5,6,1,5,6,5,6,7,9,10)

to find the norm, we dot these vectors with themselves (sum of squares) and then take the square root:

(r1 - r2).(r1 - r2) = 9+4+0+16+25+36+1+1+0+25+36+49+49 = 315

(r1 + r2).(r1 + r2) = 25+4+36+16+25+36+1+25+36+25+36+49+81+100 = 495

√(315/495) ~ 0.79772403521746564185512620976512

1 - √(315/495) ~ 0.20227596478253435814487379023488 to 4 decimal places this is 0.2023

where you went wrong on ||r1 - r2|| is its not √(Σi(xi2 - yi2) but rather √(Σi(xi-yi)2)

that is a sum of the squares of the differences, not a sum of the differences of the squares, and similarly for ||r1 + r2||

(and darn it, yvgeny, no fair using a program when i have to do this stuff by hand!)

8. ## Re: Requesting help understanding an algorithm involving normalised vector

Originally Posted by Deveno
(and darn it, yvgeny, no fair using a program when i have to do this stuff by hand!)
Thanks for providing 15 additional decimal digits

9. ## Re: Requesting help understanding an algorithm involving normalised vector

Wow thanks a lot to the both of you. I was never good with Math but I never thought that there's still someone out there that would help me out here.

Oh by the way, do you mind telling me what program did you use for this? Can the program be used to calculate other algorithms as well?

10. ## Re: Requesting help understanding an algorithm involving normalised vector

Originally Posted by Eiko
Oh by the way, do you mind telling me what program did you use for this? Can the program be used to calculate other algorithms as well?
I used OCaml programming language. The expression that computes Sr is

Code:
1. -. sqrt (fold_right (+.) (map sqr (map2 (-.) r1 r2)) 0.) /. sqrt (fold_right (+.) (map sqr (map2 (+.) r1 r2)) 0.);;
where r1 and r2 are lists of floats.

But this can be easily written in any decent programming language. It is also possible to use computational software like MATLAB and Octave, but I personally don't know them.