[Python] How to Compute Pairwise Correlations for Thousands of Stocks in Batch

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to compute the pairwise correlation of stock return data using DolphinDB. There are thousands of stocks. How can I achieve this? I have already implemented a version in Python:

    import pandas as pd
    import numpy as np

    # suppose you have a table like this:
    # AAPL MSFT GOOGL ...
    # 2021-01-01 0.01 0.02 -0.01
    # 2021-01-02 0.00 0.01 0.02
    # ...

    tickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'META']
    dates = pd.date_range(start='2021-01-01', periods=100)
    np.random.seed(0)
    data = np.random.randn(100, len(tickers)) / 100 # daily ratios

    df = pd.DataFrame(data, index=dates, columns=tickers)

    correlation_matrix = df.corr()


    print(correlation_matrix.head())

    Continue reading...

Compartilhe esta Página