Skip to main content

Are you on the right long-term path? Get a full financial assessment

Get a full financial assessment
← Back to C Definitions

Cosine similarity

What Is Cosine Similarity?

Cosine similarity is a quantitative analysis technique that measures the similarity between two non-zero vectors in an inner product space by calculating the cosine of the angle between them. It is widely used in data analytics and the broader field of quantitative analysis to determine how similar two data points are in terms of their orientation, regardless of their magnitude. This makes cosine similarity particularly useful when the size or scale of the vectors is not as important as their direction. When applied in finance, it can help in tasks such as identifying similar assets or analyzing market trends.

History and Origin

The concept of cosine similarity is rooted in fundamental linear algebra and vector space principles. While not tied to a single inventor, its application gained prominence with the rise of information retrieval and text mining. It became a cornerstone of techniques used to measure document similarity, where documents are represented as vectors in a high-dimensional space. The cosine of the angle between these document vectors indicates how similar their subject matter is, independent of document length. This method is extensively detailed in discussions of data analysis and information retrieval.

Key Takeaways

  • Cosine similarity quantifies the directional similarity between two vectors, ranging from -1 (completely opposite) to +1 (perfectly aligned).
  • It is invariant to vector magnitude, making it suitable for comparing items where size differences are irrelevant.
  • Widely applied in areas like machine learning, natural language processing, and recommendation systems.
  • A score of 1 indicates identical direction, 0 indicates orthogonality (no relationship), and -1 indicates opposite directions.

Formula and Calculation

Cosine similarity is calculated using the dot product of two vectors and their magnitudes. For two vectors, A and B, in an N-dimensional vector space, the formula is:

Cosine Similarity(A,B)=ABAB=i=1NAiBii=1NAi2i=1NBi2\text{Cosine Similarity}(A, B) = \frac{A \cdot B}{||A|| \cdot ||B||} = \frac{\sum_{i=1}^{N} A_i B_i}{\sqrt{\sum_{i=1}^{N} A_i^2} \sqrt{\sum_{i=1}^{N} B_i^2}}

Where:

  • (A \cdot B) represents the dot product of vectors A and B.
  • (||A||) and (||B||) represent the Euclidean norms (magnitudes) of vectors A and B, respectively.
  • (A_i) and (B_i) are the components of vectors A and B.

This formula essentially normalizes the vectors, allowing the measure to focus solely on the angle between them. The dot product is a key component, representing the sum of the products of corresponding elements from the two vectors.

Interpreting the Cosine Similarity

The value of cosine similarity ranges from -1 to +1. A value of +1 signifies that the two vectors are perfectly aligned and point in the same direction, indicating maximum similarity. A score of 0 suggests that the vectors are orthogonal (perpendicular), implying no directional relationship or correlation between them. A value of -1 means the vectors are exactly opposite in direction, indicating maximum dissimilarity.

For instance, in the context of financial modeling, if two stock portfolios are represented as vectors of sector allocations, a high cosine similarity would indicate that they have very similar exposures to different industries, regardless of the total value of the portfolios. Conversely, a low or negative score would suggest differing or opposing allocations. Understanding these similarity measures is crucial for effective analysis.

Hypothetical Example

Consider a simplified scenario in which an investor wants to compare two hypothetical investment portfolios, Portfolio X and Portfolio Y, based on their exposure to three asset classes: Equities (E), Bonds (B), and Real Estate (RE).

Portfolio X can be represented as a vector (A = [0.6, 0.3, 0.1]), meaning 60% in Equities, 30% in Bonds, and 10% in Real Estate.
Portfolio Y can be represented as a vector (B = [0.5, 0.4, 0.1]), meaning 50% in Equities, 40% in Bonds, and 10% in Real Estate.

Step 1: Calculate the dot product ((A \cdot B))
AB=(0.6×0.5)+(0.3×0.4)+(0.1×0.1)=0.30+0.12+0.01=0.43A \cdot B = (0.6 \times 0.5) + (0.3 \times 0.4) + (0.1 \times 0.1) = 0.30 + 0.12 + 0.01 = 0.43

Step 2: Calculate the magnitude of Vector A ((||A||))
A=0.62+0.32+0.12=0.36+0.09+0.01=0.460.678||A|| = \sqrt{0.6^2 + 0.3^2 + 0.1^2} = \sqrt{0.36 + 0.09 + 0.01} = \sqrt{0.46} \approx 0.678

Step 3: Calculate the magnitude of Vector B ((||B||))
B=0.52+0.42+0.12=0.25+0.16+0.01=0.420.648||B|| = \sqrt{0.5^2 + 0.4^2 + 0.1^2} = \sqrt{0.25 + 0.16 + 0.01} = \sqrt{0.42} \approx 0.648

Step 4: Calculate the Cosine Similarity
Cosine Similarity(A,B)=0.430.678×0.648=0.430.4394640.978\text{Cosine Similarity}(A, B) = \frac{0.43}{0.678 \times 0.648} = \frac{0.43}{0.439464} \approx 0.978

A cosine similarity of approximately 0.978 indicates a very high degree of directional similarity between Portfolio X and Portfolio Y. This suggests that, in terms of their proportional allocation across these asset classes, the portfolios are quite similar, which could inform decisions about asset allocation or investment strategies.

Practical Applications

Cosine similarity has a wide range of practical applications, particularly in fields that involve analyzing high-dimensional data. In finance, it is a valuable tool for various analyses:

  • Portfolio Diversification: It can be used to assess the similarity of different assets or portfolios based on their historical price movements or fundamental characteristics. A low cosine similarity between assets might suggest good candidates for portfolio diversification as their movements are less correlated.
  • Algorithmic Trading: In algorithmic trading strategies, cosine similarity can help identify patterns or relationships between different financial instruments, such as stocks, commodities, or currencies, by comparing their price or volume vectors.
  • Credit Scoring and Fraud Detection: By representing customer behavior or transaction histories as vectors, cosine similarity can help identify patterns that are highly similar to known fraudulent activities or indicate a higher credit risk.
  • News and Sentiment Analysis: Financial news articles or social media sentiment can be vectorized, and cosine similarity can then identify similar news events or sentiment trends across different companies or sectors, aiding in market analysis. This method of comparing text documents for semantic similarity is a common application.4,3

Limitations and Criticisms

While cosine similarity is a powerful tool, it has limitations that should be considered. A primary critique is that it focuses exclusively on the angle between vectors, disregarding their magnitude. This means that two vectors pointing in the same direction, but with vastly different lengths (magnitudes), will have a perfect cosine similarity of +1. In some financial contexts, the magnitude (e.g., the actual dollar value of an investment or the intensity of a signal) might be just as important as the direction.

Recent research, including a study by Netflix, has highlighted that cosine similarity of learned embeddings can sometimes yield arbitrary or misleading results depending on how the underlying models are trained. This suggests that while it works well in many practical applications, its effectiveness can be influenced by the structure and regularization of the data representations it operates on.2,1 Therefore, for nuanced analyses in risk management or complex financial scenarios, relying solely on cosine similarity without considering other metrics or the context of vector magnitude could be problematic.

Cosine Similarity vs. Pearson Correlation

Cosine similarity and Pearson correlation are both measures of similarity, but they differ fundamentally in their approach and what they quantify.

FeatureCosine SimilarityPearson Correlation
What it measuresThe cosine of the angle between two vectors (directional similarity).Linear relationship between two variables (how they move together).
Range[-1, +1][-1, +1]
Magnitude BiasIgnores magnitude; only focuses on direction.Sensitive to magnitude and mean of the data; essentially normalizes for the mean.
Interpretation of 0Orthogonal vectors (no directional relationship).No linear correlation.
Typical UseDocument similarity, recommender systems, clustering in high-dimensional spaces.Statistical analysis, finance (e.g., asset correlation, beta calculation).

The key distinction lies in how they treat the data. Cosine similarity treats vectors as orientations from the origin, making it robust to variations in magnitude. Pearson correlation, on the other hand, measures how closely two variables move together relative to their means, effectively standardizing the data before calculating the linear relationship. This makes Pearson correlation more suitable when the relationship between changes in values (rather than just directions from an arbitrary origin) is important. For example, if analyzing two stocks' returns, Pearson correlation would indicate if they tend to go up and down together, whereas cosine similarity would indicate if their profiles of returns (e.g., proportions across different market conditions) are similar, regardless of their absolute return values. The choice between them depends on the specific analytical objective and the nature of the Euclidean distance or directional relationship being sought.

FAQs

What does a cosine similarity of 0 mean?

A cosine similarity of 0 indicates that the two vectors are orthogonal, meaning they are perpendicular to each other. In practical terms, this suggests there is no directional relationship or similarity between the two items or data points being compared. For example, if analyzing two different sets of data points, a score of 0 would imply they are unrelated in terms of their vector orientation.

When is cosine similarity preferred over other similarity measures?

Cosine similarity is often preferred when the magnitude of the vectors is not a significant factor, and the primary interest is in the orientation or directional alignment between them. This is common in high-dimensional spaces, such as in text analysis (where document length shouldn't bias similarity) or in certain machine learning applications where vectors represent abstract features.

Can cosine similarity be used for negative values in vectors?

Yes, cosine similarity can handle negative values in vectors. The formula accounts for negative components in the dot product calculation. The resulting cosine similarity will still fall within the range of -1 to +1, allowing for interpretation of opposite directions when negative values are present. This allows for a more comprehensive understanding of relationships in quantitative models.

AI Financial Advisor

Get personalized investment advice

  • AI-powered portfolio analysis
  • Smart rebalancing recommendations
  • Risk assessment & management
  • Tax-efficient strategies

Used by 30,000+ investors