-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcovariance_mm.py
More file actions
56 lines (42 loc) · 1.6 KB
/
covariance_mm.py
File metadata and controls
56 lines (42 loc) · 1.6 KB
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
# -*- coding: utf-8 -*-
"""
This script computes the covariance of fluctuations between two matrix.
Example: density_covariance = <mu*nu> - <mu><nu> = A - B
Author: DiegoDZ
Date: 24 june 2016
Modified: 16 december 2016
run: >> python convariance_mm matrix1 matrix2 > output_file
"""
import numpy as np
import sys
def covariance(arg1, arg2):
# load files
datafile1 = np.loadtxt(str(arg1))
datafile2 = np.loadtxt(str(arg2))
# define number of nodes
number_nodes = len(datafile1[0])
# define number of snapshots
number_snapshots = len(datafile1)
# create a 3D array in which we will save information per snapshot
node_density_snapshot = np.zeros((number_snapshots, number_nodes, number_nodes))
for i in range(0,number_snapshots):
# compute the outer product (row x row) in each snapshot and save it.
node_density_snapshot[i,:,:] = np.outer(datafile1[i], datafile2[i])
# Compute the first term of the covariance (A)
A = np.sum(node_density_snapshot, axis = 0) / number_snapshots
# Sum column elements of the datafile and average the result.
node_density1 = datafile1.sum(axis = 0) / number_snapshots
node_density2 = datafile2.sum(axis = 0) / number_snapshots
# Compute the second term of the covariance (B)
B = np.outer(node_density1, node_density2)
covariance = A - B
return covariance
(covariance) = covariance(sys.argv[1], sys.argv[2])
#For convenience the output will save in matrix format.
aux = ''
for line in covariance:
for element in line:
aux = aux + str(element) + ' '
aux = aux + '\n'
print aux
#EOF