-
Notifications
You must be signed in to change notification settings - Fork 3
/
covfun.r
executable file
·156 lines (143 loc) · 4.21 KB
/
covfun.r
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
gknotLG = gauss.quad(9000,kind="hermite")
poiscompLG <- function(mu,sig,dat){
t=mu+sqrt(2*sig)*gknotLG$nodes
dp=dpois(dat,exp(t))*gknotLG$weights/sqrt(pi)
evs=dp%*%cbind(1,t,t^2)
un=evs[2]/evs[1]
vn=evs[3]/evs[1]-evs[2]^2/evs[1]^2
c(un,vn,evs[1])
}
getLapLoc<-function(mu,sig,d,cut=20){
l=rep(0,length(d))
l[d<cut]=d[d<cut]*sig[d<cut]+mu[d<cut]-lambert_W0(exp((d*sig+mu)[d<cut])*sig[d<cut])
l[d>cut]=(log(d[d>cut])*d[d>cut]+mu[d>cut]/sig[d>cut])/(d[d>cut]+1/sig[d>cut])
sh=1/(exp(l)+1/sig)
pr=sqrt(2*pi*sh)*dpois(d,exp(l))*dnorm(l,mu,sqrt(sig))
list(l,sh,pr)
}
makeTable <- function(data,muoffs,stabval,maxcut=100){
maxval=max(data)
llmu=sapply(0:maxval,function(i){
if(i<maxcut){
v=poiscompLG(muoffs,stabval,i)
(v[1]/v[2]-muoffs/stabval)/(1/v[2]-1/stabval)
}else{
(log(i)*i+muoffs/stabval)/(i+1/stabval)
}
})
llprec=sapply(0:maxval,function(i){
if(i<maxcut){
1/poiscompLG(muoffs,stabval,i)[2]-1/stabval
}else{
me=(log(i)*i+muoffs/stabval)/(i+1/stabval)
exp(me)-1/stabval
}
})
list(llmu,llprec)
}
fastFit <- function(data,mupri,muadjusts,sigadjusts,scid){
datind=which(data>0)
if(length(datind)>0){
sigadjust=sigadjusts[data[datind]+1]-sigadjusts[1]
#
dss=scid[datind,datind,drop=F]*outer(sqrt(sigadjust),sqrt(sigadjust),'*')
sccm=t(scid[datind,,drop=F])
cminternal=(solve(dss+diag(length(datind))))*outer(sqrt(sigadjust),sqrt(sigadjust),'*')
a1=sccm%*%cminternal%*%t(sccm)
allinv=(scid-a1)
vh=as.vector(muadjusts[data+1]*sigadjusts[data+1]+mupri)
uinv=scid%*%vh-sccm%*%(cminternal%*%(t(sccm)%*%vh))
list(uinv,allinv)
}else{
list(scid%*%as.vector(mupri+muadjusts[1]*sigadjusts[1]),scid)
}
}
fastMu <- function(data,mupri,muadjusts,sigadjusts,scid){
datind=which(data>0)
if(length(datind)>0){
sigadjust=sigadjusts[data[datind]+1]-sigadjusts[1]
#
dss=scid[datind,datind,drop=F]*outer(sqrt(sigadjust),sqrt(sigadjust),'*')
sccm=t(scid[datind,,drop=F])
cminternal=(solve(dss+diag(length(datind))))*outer(sqrt(sigadjust),sqrt(sigadjust),'*')
#a1=sccm%*%cminternal%*%t(sccm)
#allinv=(scid-a1)
sapply(mupri,function(mu){
vh=as.vector(muadjusts[data+1]*sigadjusts[data+1]+mu)
uinv=scid%*%vh-sccm%*%(cminternal%*%(t(sccm)%*%vh))
})
}else{
sapply(mupri,function(mu){
scid%*%as.vector(mu+muadjusts[1]*sigadjusts[1])
})
}
}
#cin = solve(covpost+zerovar)
calcPR <- function(data,mupris,muadjusts,sigadjusts,cin){
datind=which(data>0)
dv= sapply(mupris,function(mu){
as.vector((muadjusts[data+1]-mu)%*%cin%*%(muadjusts[data+1]-mu))
})
if(length(datind)>0){
sigadjust=1/(sigadjusts[data[datind]+1]-sigadjusts[1])
#
dss=cin[datind,datind,drop=F]*outer(sqrt(sigadjust),sqrt(sigadjust),'*')
sccm=t(cin[datind,,drop=F])
cminternal=(solve(dss+diag(length(datind))))*outer(sqrt(sigadjust),sqrt(sigadjust),'*')
da=sapply(mupris,function(mu){
sum((((muadjusts[data+1]-mu)%*%sccm)^2)%*%cminternal)
#%*%(t(sccm)%*%(muadjusts[data+1]-mu))
#as.vector((muadjusts[data+1]-mu)%*%cin%*%(muadjusts[data+1]-mu))
})
#a1=sccm%*%cminternal%*%t(sccm)
#allinv=(cin-a1)
dv-da
}else{
dv
}
}
makeblocks <- function(xs,bln){
bind=do.call(c,lapply(1:bln,function(i){
do.call(c,lapply(1:bln,function(j){
ofx=length(xs)*(i-1)
ofy=length(xs)*(j-1)
c(list(cbind(xs+ofx,xs+ofy)),
lapply(1:(length(xs)-1),function(i){
cbind(ofx+xs[-(length(xs):(length(xs)-i+1))],ofy+xs[-(1:i)])
}))
}))
}))
}
toepvals <- function(x,bind){
sapply(bind,function(i){mean(x[i])})
}
toeptomat <- function(x,bind,sz){
cmt=matrix(0,sz,sz)
for(i in 1:length(bind)){
cmt[bind[[i]]]=x[i]
}
cmt[cmt==0]=t(cmt)[cmt==0]
ind1=1:(sz/2)
ind2=ind1+(sz/2)
same.strand.avg=(cmt[ind1,ind1]+cmt[ind2,ind2])/2
cmt[ind1,ind1]=same.strand.avg
cmt[ind2,ind2]=same.strand.avg
cmt
}
topproj <- function(x,bind){
spp=toepvals(x,bind)
toeptomat(spp,bind,ncol(x))
}
topiter <- function(matinp,bind){
matin=topproj(matinp,bind)
ecp=eigen(matin)
ecv=ecp$values
while(min(ecv)< -1e-2){
print(min(ecv))
matin=ecp$vectors%*%diag(ecv*(ecv>0))%*%t(ecp$vectors)
matin=topproj(matin,bind)
ecp=eigen(matin)
ecv=ecp$values
}
matin+diag(ncol(matin))*0.011
}