0

3日間の相関を計算する必要があります。サンプル マトリックスを以下に示します。私の問題は、ID が毎日宇宙にあるとは限らないことです。たとえば、AAPL は常にユニバースに存在する可能性がありますが、会社 - CCL は私のユニバースに 2 日間しか存在しない可能性があります。ベクトル化されたソリューションをいただければ幸いです。相関行列のサイズが異なる場合があるため、ここでは structs/accumarrayなどを使用する必要があるかもしれません。

% col1 = tradingDates, col2 = companyID_asInts, col3 = VALUE_forCorrelation

rawdata = [ ...

    734614 1 0.5; 
    734614 2 0.4; 
    734614 3 0.1; 

    734615 1 0.6; 
    734615 2 0.4; 
    734615 3 0.2; 
    734615 4 0.5; 
    734615 5 0.12;

    734618 1 0.11; 
    734618 2 0.9; 
    734618 3 0.2; 
    734618 4 0.1; 
    734618 5 0.33;
    734618 6 0.55; 

    734619 2 0.11; 
    734619 3 0.45; 
    734619 4 0.1; 
    734619 5 0.6; 
    734619 6 0.5;

    734620 5 0.1; 
    734620 6 0.3] ; 

「3日間の相関」:

% 734614 & 734615 corr is ignored as this is a 3-day corr

% 734618_corr = corrcoef(IDs 1,2,3 values are used. ID 4,5,6 is ignored) -> 3X3 matrix

% 734619_corr = corrcoef(IDs 2,3,4,5 values are used. ID 1,6 is ignored) -> 3X4 matrix

% 734620_corr = corrcoef(IDs 5,6 values are used. ID 1,2,3,4 is ignored) -> 3X2 matrix

実際のデータは、1995 年から 2011 年までの Russel1000 ユニバースをカバーし、410 万行以上あります。望ましい相関関係は 20 日間です。

4

1 に答える 1

1

ここでベクトル化されたソリューションを取得しようとはしません。MATLAB JIT コンパイラは、最近のバージョンの MATLAB でループが同じくらい高速になることが多いことを意味します。

あなたの行列は疎行列によく似ています: 配列インデックスを使用できるように、その形式に変換するのに役立ちますか? これはおそらく、3 番目の列のデータが 0 にならない場合にのみ機能します。それ以外の場合は、現在の明示的なリストを保持して、次のようなものを使用する必要があります。

dates = unique(rawdata(:, 1));
num_comps = max(rawdata(:, 2));

for d = 1:length(dates) - 2;
    days = dates(d:d + 2);

    companies = true(1, num_comps);
    for curr_day = days'
        c = false(1, num_comps);
        c(rawdata(rawdata(:, 1) == curr_day, 2)) = true;
        companies = companies & c;
    end
    companies = find(companies);

    data = zeros(3, length(companies));
    for curr_day = 1:3
        for company = 1:length(companies)
            data(curr_day, company) = ...
                rawdata(rawdata(:, 1) == days(curr_day) & ...
                        rawdata(:, 2) == companies(company), 3);
        end
    end

    corrcoef(data)
end
于 2011-06-13T07:59:15.900 に答える