> For the complete documentation index, see [llms.txt](https://docs.fiinquant.vn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fiinquant.vn/ham-va-cong-thuc/5.-dinh-luong-and-phan-tich-nang-cao/5.3.-tuong-quan-and-so-sanh/5.3.1.-correlation.md).

# 5.3.1. Correlation

Mục đích: Dùng để **tính hệ số tương quan trượt (rolling correlation)** giữa hai chuỗi thời gian giá hoặc lợi suất trong tài chính.

Cấu trúc hàm: correlation(x, y, w=, type\_=SeriesType.PRICES)

Tham số:

<table><thead><tr><th width="82">Tham số</th><th width="174">Kiểu dữ liệu</th><th>Ý nghĩa</th></tr></thead><tbody><tr><td>x</td><td>Series</td><td>Chuỗi thời gian thứ nhất (giá hoặc lợi suất)</td></tr><tr><td>y</td><td>Series</td><td>Chuỗi thời gian thứ hai (giá hoặc lợi suất)</td></tr><tr><td>w</td><td>Window, int hoặc str</td><td>Kích thước cửa sổ trượt (rolling window)<br>Ví dụ: <code>Window(22, 10)</code> là dùng cửa sổ 22 phiên và bỏ qua 10 phiên đầu để "ramp-up".<br>Nếu dùng <code>str</code>, có thể là <code>'1m'</code> (1 tháng), <code>'1d'</code> (1 ngày)...</td></tr><tr><td>type_</td><td>SeriesType.PRICES (mặc định) hoặc SeriesType.RETURNS</td><td>Xác định đầu vào là giá hay lợi suất</td></tr></tbody></table>

Ý nghĩa toán học: Hàm tính hệ số tương quan Pearson giữa hai chuỗi lợi suất (hoặc giá được chuyển thành lợi suất) trong cửa sổ trượt `w`.

Công thức:

$$
\rho\_t = \frac{\sum\_{i=t-w+1}^t (R\_i - \overline{R})(S\_i - \overline{S})}{(N-1).\sigma\_{R}.\sigma\_{S}}
$$

Trong đó:

* $$R\_i = \frac{X\_i}{X\_i - 1}$$: lợi suất từ chuỗi `x` nếu input là giá.
* $$S\_i = \frac{Y\_i}{Y\_i - 1}$$: lợi suất từ chuỗi `y` nếu input là giá.
* Nếu input đã là lợi suất thì dùng trực tiếp $$R\_i = X\_i$$, $$S\_i = Y\_i$$.
* $$\overline{R}$$, $$\overline{S}$$: trung bình mẫu trong mỗi cửa sổ.
* $$\sigma\_{R}$$, $$\sigma\_{S}$$: độ lệch chuẩn mẫu trong mỗi cửa sổ.

Giá trị trả về:

* Một `Series` chứa hệ số tương quan qua từng thời điểm ttt, tương ứng với mỗi cửa sổ trượt.
* Có thể dùng để đánh giá mối quan hệ đồng biến/nghịch biến theo thời gian.

Ví dụ mẫu tính rolling correlation trong vòng 22 ngày gần nhất từ 03/06/2025 - 25/06/2025 của HPG và HSG trên tập dữ liệu 1 năm từ 25/05/2024 - 25/06/2025:

<pre class="language-python"><code class="lang-python">import pandas as pd
from FiinQuantX import FiinSession
from FiinQuantX.timeseries.econometrics import correlation
from FiinQuantX.timeseries.helper import Window
<strong>from datetime import datetime
</strong>from dateutil.relativedelta import relativedelta

username = "REPLACE_WITH_YOUR_USERNAME"
password = "REPLACE_WITH_YOUR_PASSWORD"

client = FiinSession(
    username=username,
    password=password
).login()

# Lấy dữ liệu giá đóng cửa 1 năm EOD của HPG và HSG
data = client.Fetch_Trading_Data(
    realtime=False,
    tickers=["HPG","HSG"],
    fields=["close"],
    adjusted=True,
    by="1d",
    from_date=(datetime.now() - relativedelta(years=1)).strftime("%Y-%m-%d")
).get_data()

# Pivot theo ticker để tách riêng từng mã
data["timestamp"] = pd.to_datetime(data["timestamp"])
pivot_data = data.pivot(index="timestamp", columns="ticker", values="close")

# Lấy Series
series_HPG = pivot_data["HPG"]
series_HSG = pivot_data["HSG"]

# Tính rolling correlation 22 ngày
corr = correlation(series_HPG, series_HSG, w=22)

# Ghép 3 series vào thành 1 dataframe
df_hpg = series_HPG.rename("HPG").reset_index().rename(columns={"index": "timestamp"})
df_hsg = series_HSG.rename("HSG").reset_index().rename(columns={"index": "timestamp"})
df_corr = corr.rename("correlation").reset_index().rename(columns={"index": "timestamp"})

df_merged = pd.merge(df_hpg, df_hsg, on="timestamp", how="outer")
df_merged = pd.merge(df_merged, df_corr, on="timestamp", how="outer")

df_merged = df_merged.sort_values("timestamp").reset_index(drop=True)
print(df_merged)
</code></pre>

Kết quả trả ra (code chạy tại ngày 25/06/2025)

```
   timestamp   HPG      HSG       correlation
0  2025-01-24  26550.0  16954.42          NaN
1  2025-02-03  26400.0  17100.16          NaN
2  2025-02-04  26850.0  17391.64          NaN
3  2025-02-05  26800.0  17391.64          NaN
4  2025-02-06  26800.0  17245.90          NaN
..        ...      ...       ...          ...
95 2025-06-19  26900.0  16750.00     0.674208
96 2025-06-20  27000.0  16600.00     0.661541
97 2025-06-23  26850.0  16350.00     0.667212
98 2025-06-24  27000.0  16750.00     0.655733
99 2025-06-25  27200.0  16750.00     0.668507
```

Biểu đồ minh họa (tuỳ chọn)

```python
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# Chuẩn hóa giá HPG và HSG (min-max normalization)
df_merged['timestamp'] = pd.to_datetime(df_merged['timestamp'])
df_merged['HPG_norm'] = (df_merged['HPG'] - df_merged['HPG'].min()) / (df_merged['HPG'].max() - df_merged['HPG'].min())
df_merged['HSG_norm'] = (df_merged['HSG'] - df_merged['HSG'].min()) / (df_merged['HSG'].max() - df_merged['HSG'].min())

fig, ax1 = plt.subplots(figsize=(14, 6))

# Plot HPG và HSG đã chuẩn hóa
ax1.plot(df_merged['timestamp'], df_merged['HPG_norm'], label='HPG (normalized)', color='blue')
ax1.plot(df_merged['timestamp'], df_merged['HSG_norm'], label='HSG (normalized)', color='green')
ax1.set_ylabel('Normalized Price', color='black')
ax1.legend(loc='upper left')
ax1.grid(True)

# Format thời gian
ax1.xaxis.set_major_locator(mdates.AutoDateLocator())
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
fig.autofmt_xdate(rotation=45)

# Trục y phải: correlation
ax2 = ax1.twinx()
ax2.plot(df_merged['timestamp'], df_merged['correlation'], label='Rolling Correlation (HPG-HSG)', color='red', linestyle='--')
ax2.set_ylabel('Correlation', color='red')
ax2.legend(loc='upper right')

plt.title('Giá HPG & HSG (chuẩn hóa) và hệ số tương quan theo thời gian')
plt.tight_layout()
plt.show()
```

<figure><img src="/files/jyIjO68ZZbh6ZYjshAtA" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.fiinquant.vn/ham-va-cong-thuc/5.-dinh-luong-and-phan-tich-nang-cao/5.3.-tuong-quan-and-so-sanh/5.3.1.-correlation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
