← All articles

Blog · English

A Universal Measure of Similarity

The cosine function is one of those mathematical concepts that are timeless. Originally intended to describe the relationship between the sides and angles of a triangle, it has long since found its way into modern computer science. In its simplest form, it shows how two directions relate to each other, whether they poi

The cosine function is one of those mathematical concepts that are timeless. Originally intended to describe the relationship between the sides and angles of a triangle, it has long since found its way into modern computer science. In its simplest form, it shows how two directions relate to each other, whether they point the same way, in opposite directions, or somewhere in between.

You can picture it visually. If two cars are traveling in the same direction, the angle between their movements is zero degrees, so the cosine is 1. If they are heading toward each other, the angle is 180 degrees, and the cosine is -1. And if one crosses an intersection while the other continues straight ahead, the cosine is exactly 0.

Computer science uses precisely this principle. Whether comparing two vectors in a neural network, two word meanings in a language model, or two images in the same semantic space, the cosine reveals how similar they are to each other.

Today, this geometric idea is indispensable in many fields. Whether we are comparing images, measuring similarity between words, determining temporal proximity between dates, or calculating geographical distances, the cosine provides a stable, scale-independent measure of proximity.

From Triangles to Vectors

If two vectors point in the same direction, the cosine of the angle between them is 1. If they are perpendicular to each other, it is 0. If they point in opposite directions, it is -1. This simple range from -1 to 1 makes the cosine a natural measure of similarity.

In CardAI, I use precisely this property to compare embedding vectors, the 512-dimensional numerical fingerprints that describe each postcard. Two cards with similar subjects have vectors that point in almost the same direction, and their cosine similarity is close to 1. Completely different images, on the other hand, point in mutually orthogonal directions, and their cosine similarity is close to 0.

Why Cosine Rather Than Distance

You might wonder why we do not simply measure the distance between the vectors. The reason lies in the meaning of the embeddings. Their length is often arbitrary, while their direction carries the meaning. The cosine normalizes both vectors before comparing them and focuses only on their orientation. This makes it insensitive to differences in brightness, contrast, or calibration, exactly what is needed when analyzing images or semantic structures.

Beyond Images: Dates and Places

The same principle works far beyond image data.

If we treat time as a cyclical variable, for example by mapping calendar days onto a unit circle, the cosine elegantly describes how close two dates are to each other. Each day of the year corresponds to a point on the circle whose angle is determined by $\theta = 2\pi \cdot t / 365$.

The cosine of the angular difference, $\cos(\theta_1 - \theta_2)$, then provides a measure of the temporal proximity of two dates. The difference between March 1 and March 5 produces a value close to 1; between March 1 and September 1, the cosine is close to 0; and between March 1 and December 1, it becomes negative.

The advantage of this representation becomes particularly clear at the end of the year. On a linear scale, December 31 is far away from January 1, even though the two days immediately follow one another. On the unit circle, however, they lie at almost the same point, and the cosine of the angular difference is nearly 1. This keeps the annual cycle mathematically smooth and continuous, without the artificial discontinuities that arise from simply subtracting date values.

Geographical calculations also use the cosine. The Haversine formula, which is used to calculate distances on the Earth's surface, is based on cosine values of latitudes and longitudes. Once again, it is the direction in space that matters, not the raw numerical values of the coordinates.

The Quiet Constant of Similarity

From classical geometry to modern AI, the cosine function is everywhere. It turns measurements into relationships, and relationships are the foundation of meaning.

In CardAI, it connects postcards by subject; in language models, it connects words by meaning; in geographic information systems, it connects places by proximity. The cosine function is a quiet mediator between mathematics, physics, and machine learning, a universal measure of similarity.

A Small Example in Python

Here is how to try the idea out directly:

import math

def day_angle(day_of_year):
    return 2 * math.pi * day_of_year / 365

def date_similarity(day1, day2):
    theta1, theta2 = day_angle(day1), day_angle(day2)
    return math.cos(theta1 - theta2)

print(date_similarity(365, 1))   # Nähe zwischen 31. Dez und 1. Jan
print(date_similarity(60, 180))  # März vs. Juni
print(date_similarity(90, 270))  # Frühling vs. Herbst

The output shows that December 31 and January 1 are rated almost identically, while spring and autumn are nearly orthogonal to each other, exactly what the cosine is meant to express.

When viewed as a circle, December 31 and January 1 lie close together, while March and September occupy opposite points on the unit circle. This creates a continuous, cyclical representation of time in which the beginning and end of the year flow seamlessly into each other.