ITPEC FE Subject B April 2024 Question 14

From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array indexes start at 1.

The function calcSim takes two vectors (arrays) as input, calculates their similarity, and returns a value that characterizes the similarity. A large similarity indicates that the vectors are similar, and a small similarity indicates that the vectors are dissimilar. For example, to quantify the similarity between two documents, you can create a vector of occurrences of some words for each of the two documents and input them into calcSim. When the function calcSim({2, 2, 1, 0, 4}, {3, 1, 1, 1, 2}) is called, the return value rounded to the first decimal place is blank.

[Program]
// Assume that arrays v1 and v2 have the same number of one or more elements
// and that the arrays are not all-zero.
○ real: calcSim(integer []: v1, integer []: v2)
integer: i, x, y
integer: sxx ← 0
integer: syy ← 0
integer: sxy ← 0
for (increase i from 1 to the number of elements in v1 by 1)
x ← v1[i]
y ← v2[i]
sxx ← sxx + x × x
syy ⟵ syy + y × y
sxy ⟵ sxy + x × y
endfor
return sxy ÷ (square root of (sxx × syy))

Answer group

Explanation

0.9 — the function computes the cosine similarity of the two vectors, 0.85 before rounding.

The loop builds three sums: sxx of x × x, syy of y × y and sxy of x × y. The result is sxy divided by the square root of sxx × syy.

ixyx × xy × yx × y
123496
221412
311111
401010
5421648
Sum––251617

Result: 17 ÷ √25 × 16 = 17 ÷ √400 = 17 ÷ 20 = 0.85, which rounds to 0.9 at the first decimal place.

Answer (i)

Why not others:

  • •(h) 0.8 — truncates 0.85 instead of rounding it
  • •(j) 1.0 — the maximum, reached only when one vector is a positive multiple of the other; {2, 2, 1, 0, 4} and {3, 1, 1, 1, 2} are not proportional
  • •(g) 0.7 — what 17 ÷ 25 = 0.68 gives, dividing by sxx alone instead of the root of the product
  • •(d) 0.4 — what 17 ÷ (25 + 16) = 0.41 gives, adding the two sums of squares instead of taking the root of their product
  • •(a) 0.1, (b) 0.2, (c) 0.3, (e) 0.5 and (f) 0.6 — no step of the trace produces these; with every product non-negative, sxy is 17 against a denominator of 20

Key rule: Cosine similarity is the dot product divided by the product of the vector lengths; tabulate the three sums in one pass and round only at the end.

Math & Data Science · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.