forked from sergeyplis/ica
/
demo.py
42 lines (34 loc) · 1.16 KB
/
demo.py
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
# Import ica function
from ica import ica1
import numpy as np
import matplotlib.pyplot as plt
import time
def main():
# Define matrix dimensions
Nobs = 1000 # Number of observation
Nvars = 50000 # Number of variables
Ncomp = 100 # Number of components
# Simulated true sources
S_true = np.random.logistic(0,1,(Ncomp,Nvars))
# Simulated true mixing
A_true = np.random.normal(0,1,(Nobs,Ncomp))
# X = AS
X = np.dot(A_true,S_true)
# add some noise
X = X + np.random.normal(0,1,X.shape)
# apply ICA on X and ask for 2 components
model = ica1(Ncomp)
start = time.time()
A,S = model.fit(X)
total = time.time() - start
print('total time: {}'.format(total))
# compare if our estimates are accurate
# correlate A with Atrue and take
aCorr = np.abs(np.corrcoef(A.T,A_true.T)[:Ncomp,Ncomp:]).max(axis = 0).mean()
sCorr = np.abs(np.corrcoef(S,S_true)[:Ncomp,Ncomp:]).max(axis = 0).mean()
print("Accuracy of estimated sources: %.2f"%sCorr)
print("Accuracy of estimated mixing: %.2f"%aCorr)
if __name__=="__main__":
import theano.sandbox.cuda
theano.sandbox.cuda.use('gpu')
main()