-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
105 lines (73 loc) · 2.5 KB
/
test.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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 14 14:31:24 2010
@author: Barton Baker
"""
import numpy as np
from numpy import *
numvars=9
L_n=np.zeros([(1.0/2)*(numvars)*(numvars+1),numvars**2])#this is the elimination matrix
#this method is taken from Magnus and Neudecker (1980)
ident=np.eye(numvars)
j=0
while j < numvars:
i=j
while j <= i < numvars:
u=np.zeros([(1.0/2)*numvars*(numvars+1),1])#initialize u_ij matrix
u[(j)*numvars+(i+1)-(1.0/2)*(j+1)*j-1]=1 #because of 0 indexing, had to mess with formula from magnus (1980) paper
L_n=L_n+kron(kron(u,ident[:,j].T),ident[:,i].T)
i=i+1
j=j+1
#output['L_n']=L_n #this is the Elimination matrix
#since the variance/covariance matrix is symmetric, the Moore-Penrose inverse of L is D
D_nT=np.zeros([L_n.shape[0],L_n.shape[1]])
j=0
while j < numvars:
i=j
while j <= i < numvars:
u=np.zeros([(1.0/2)*numvars*(numvars+1),1])#initialize u_ij matrix
u[(j)*numvars+(i+1)-(1.0/2)*(j+1)*j-1]=1 #because of 0 indexing, had to mess with formula from magnus (1980) paper
Tij=np.zeros([numvars,numvars])
Tij[i,j]=1
Tij[j,i]=1
D_nT=D_nT+dot(u,(Tij.ravel('F')[:,None]).T)
i=i+1
j=j+1
D_n=D_nT.T
x=numvars #x keeps track of sum function to take out correct columns
y=1 #y keeps track of lost columns
collector=D_n
collector=np.delete(collector,0,1)
###################here's the problem
while x >= 2:
collector=np.delete(collector,sum(b for b in range(x,numvars+1))-y,1)
#print sum(b for b in range(x,numvars+1))
x=x-1
y=y+1
#print collector[:,0]
#print raw_input("Continue?")
#output['collector']=collector
S_Bpre=collector
#output['S_Bpre']=S_Bpre
S_B=np.zeros([S_Bpre.shape[0],S_Bpre.shape[1]])
i=0
j=0
while j < S_Bpre.shape[1]:
while i < S_Bpre.shape[0]:
if S_Bpre[i,j]==1:
S_B[i,j]=1
S_B[i+1:,j][:,None]=np.zeros([S_Bpre.shape[0]-i-1,1])
break
i=i+1
j=j+1
S_BT=np.zeros([S_Bpre.shape[0],S_Bpre.shape[1]])
j=0
while j < S_Bpre.shape[1]:
i=S_Bpre.shape[0]-1
while i > 0:
if S_Bpre[i,j]==1:
S_BT[i,j]=1
S_BT[:i,j][:,None]=np.zeros([i,1])
break
i=i-1
j=j+1