compute_cosine: Trajectory similarity: Cosine Similarity

View source: R/similarity.R

compute_cosineR Documentation

Trajectory similarity: Cosine Similarity

Description

Cosine Similarity is used to characterize how state direction of two trajectory \vec{X}(t), \vec{Y}(t) agree with each other. It computes cosine of the angle between the two states at each time.

Usage

compute_cosine(res_mat1, res_mat2)

Arguments

res_mat1

Trajectory matrix 1, first column must be time while others are states.

res_mat2

Trajectory matrix 2, first column must be time while others are states.

Details

Formula: \mbox{CS}(t^j)=\frac{\Sigma_i X_i(t^j)Y_i(t^j)}{\left\Vert X_i(t^j) \right\Vert \left\Vert Y_i(t^j) \right\Vert}

Value

Cosine similarity of the two trajectory, vector with length = number of time steps.

Examples

# Perfect alignment (cos=1)
mat1 <- cbind(time = 1:3, state1 = 1:3, state2 = 4:6)
mat2 <- cbind(time = 1:3, state1 = 2:4, state2 = 5:7)
compute_cosine(mat1, mat2)  # c(1, 1, 1)

# Orthogonal vectors (cos=0)
mat3 <- cbind(time = 1:2, state1 = c(1, 0), state2 = c(0, 1))
mat4 <- cbind(time = 1:2, state1 = c(0, 1), state2 = c(1, 0))
compute_cosine(mat3, mat4)  # c(0, 0)

# Opposite direction (cos=-1)
mat5 <- cbind(time = 1:3, state1 = 1:3)
mat6 <- cbind(time = 1:3, state1 = -1:-3)
compute_cosine(mat5, mat6)  # c(-1, -1, -1)

clockSim documentation built on April 11, 2025, 5:40 p.m.