-
Notifications
You must be signed in to change notification settings - Fork 2
/
kendalls_tau.m
69 lines (58 loc) · 2.48 KB
/
kendalls_tau.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function [meanChange, tauDifference, tauChange] = kendalls_tau(spikesA, spikesB, alignmentdataA, alignmentdataB, binSize_in_seconds)
binSize = binSize_in_seconds;
both = find(alignmentdataA > 0 & alignmentdataB > 0);
want1 = alignmentdataA(both);
want2 = alignmentdataB(both);
spikesA = spikesA(want1, :);
spikesB = spikesB(want2, :);
% Convert spike times to binned spike counts
binSize = 1; % Define bin size in seconds
binnedA = spikeTimeToBins(spikesA, binSize);
binnedB = spikeTimeToBins(spikesB, binSize);
% Preallocate a matrix to store the Kendall's Tau values
numCells = size(binnedA, 1);
tauMatrixA = NaN(numCells, numCells); % Each element will store the Tau value between two cells for day A
tauMatrixB = NaN(numCells, numCells); % Similarly for day B
% Calculate Kendall's Tau for each pair of cells
for i = 1:numCells
for j = i+1:numCells % Only compute for upper triangular part since the matrix is symmetric
tauMatrixA(i, j) = corr(binnedA(i,:)', binnedA(j,:)', 'type', 'Kendall', 'Rows', 'pairwise');
tauMatrixA(j, i) = tauMatrixA(i, j); % Mirror the value to the lower triangular part
tauMatrixB(i, j) = corr(binnedB(i,:)', binnedB(j,:)', 'type', 'Kendall', 'Rows', 'pairwise');
tauMatrixB(j, i) = tauMatrixB(i, j);
end
end
% Calculate the difference and absolute differences between the two correlation matrices
tauDifference = tauMatrixA - tauMatrixB;
tauChange = abs(tauDifference);
meanChange = mean(tauChange, 'all', 'omitnan');
% Display mean change
disp(['Mean change in Kendall''s Tau across environments: ', num2str(meanChange)]);
% Visualize the results
figure;
subplot(1,3,1);
imagesc(tauMatrixA);
title('Environment A');
colorbar;
subplot(1,3,2);
imagesc(tauMatrixB);
title('Environment B');
colorbar;
subplot(1,3,3);
imagesc(tauChange);
title('Absolute Change in Tau');
colorbar;
end
function binnedData = spikeTimeToBins(spikeMatrix, binSize)
duration = max(spikeMatrix, [], 'all', 'omitnan'); % Find latest spike time
numBins = ceil(duration / binSize);
binnedData = zeros(size(spikeMatrix, 1), numBins);
for i = 1:size(spikeMatrix, 1)
for spike = spikeMatrix(i, :)
if ~isnan(spike)
bin = min(numBins, floor(spike / binSize) + 1);
binnedData(i, bin) = binnedData(i, bin) + 1;
end
end
end
end