def calculate_correlations(tetra_z):
    orgs = sorted(tetra_z.keys())
    correlations = pd.DataFrame(index=orgs, columns=orgs,
                                dtype=float).fillna(1.0)
    for idx, org1 in enumerate(orgs[:-1]):
        for org2 in orgs[idx+1:]:
            assert sorted(tetra_z[org1].keys()) == sorted(tetra_z[org2].keys())
            tets = sorted(tetra_z[org1].keys())
            zscores = [[tetra_z[org1][t] for t in tets],
                       [tetra_z[org2][t] for t in tets]]
            zmeans = [sum(zscore)/len(zscore) for zscore in zscores]
            zdiffs = [[z - zmeans[0] for z in zscores[0]],
                      [z - zmeans[1] for z in zscores[1]]]
            diffprods = sum([zdiffs[0][i] * zdiffs[1][i] for i in
                             range(len(zdiffs[0]))])
            zdiffs2 = [sum([z * z for z in zdiffs[0]]),
                       sum([z * z for z in zdiffs[1]])]
            correlations[org1][org2] = diffprods / \
                                       math.sqrt(zdiffs2[0] * zdiffs2[1])
            correlations[org2][org1] = correlations[org1][org2]
    return correlations