-
Notifications
You must be signed in to change notification settings - Fork 2
/
sinkhorn.Rmd
178 lines (141 loc) · 4.05 KB
/
sinkhorn.Rmd
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
title: "sinkhorn"
author: "Matthew Stephens"
date: "2021-04-30"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
## Introduction
I wanted to do some examination of using sinkhorn normalization
for count/single cell data, inspired by the talk by B Landa and
the preprint https://arxiv.org/abs/2103.13840
First I wanted to compare the sinkhorn normalization to SVD and row/column sums.
```{r}
set.seed(1)
p = 200
n = 100
mu = matrix(rexp(n*p,1))
X = matrix(rpois(n*p,mu), nrow=n,ncol=p)
```
```{r}
sinkhorn = function(X,niter = 10){
c = colMeans(X)
#find row and column scalings r and c
for(i in 1:niter){
r = 1/rowMeans(t(t(X)*c))
c = 1/colMeans(X*r)
}
Xnorm = t(t(X*sqrt(r))*sqrt(c))
return(list(Xnorm=Xnorm,c = c, r=r))
}
X.s = sinkhorn(X)
rowMeans(t(t(X*X.s$r)*X.s$c))
colMeans(t(t(X*X.s$r)*X.s$c))
```
```{r}
X.svd = svd(X)
plot(X.svd$u[,1],X.s$r)
plot(X.svd$v[,1],X.s$c)
plot(X.s$r, 1/rowMeans(X))
plot(X.s$c, 1/colMeans(X))
```
# Try to recreate the figure from the preprint:
I reproduced this simulation:
"Figure 1 exemplifies the advantage of biwhitening on a simulated Poisson count matrix Y with m = 300, n = 1000, and r = 10. For this example, we first generated an m × r matrix B by sampling its entries independently from the log-normal distribution with mean 0 and variance 4 (i.e., from exp(2Z), where Z ∼ N (0, 1)). Then, we generated an r × n matrix C by sampling its entries independently from the uniform distribution over [0,1]. Lastly, we computed X = BC, normalized X by a scalar so that its average entry is 1, and sampled the entries of Y from the Poisson distribution as in (7). After generating Y , Algorithm 1 was applied to Y with scaling tolerance δ = 10−12."
```{r}
m=300
n=1000
r = 10
B = exp(2*matrix(rnorm(m*r),nrow=m))
C = matrix(runif(r*n),nrow=r)
X = B %*% C
X = X/mean(X)
Y = matrix(rpois(length(X),lambda=X), nrow=nrow(X))
```
```{r}
Ynorm = sinkhorn(Y)$Xnorm
Ynorm.eigen = eigen((1/n) * Ynorm %*% t(Ynorm))
d = Ynorm.eigen$values
hist(d[d<4],nclass=100)
beta_plus = (1+sqrt(m/n))^2
abline(v=beta_plus)
sum(d>beta_plus)
```
# Compute eigenvalues on some real data
This is a small subset of some single cell data: I extracted counts from a subset of 500 genes at 1000 cytotoxic and 10000 naive cytotoxic T cells from 10x genomics. Note that because
I have fewer genes than cells I make those the rows ($m<n$) by transposing the data
```{r}
df = read.csv("../data/cell_data.csv",sep=",")
dim(df)
Y = t(as.matrix(df[,-1]))
Y.svd = svd(Y)
hist(Y.svd$d[Y.svd$d<100], xlim = c(0,100), breaks = seq(0,100,by=1))
```
Now try the normalization (note that I did not try to estimate the scaling factor they use)
```{r}
Ynorm = sinkhorn(Y)$Xnorm
m= nrow(Ynorm)
n= ncol(Ynorm)
Ynorm.eigen = eigen((1/n) * Ynorm %*% t(Ynorm))
d = Ynorm.eigen$values
v= Ynorm.eigen$vectors
hist(d[d<4],nclass=100)
beta_plus = (1+sqrt(m/n))^2
abline(v=beta_plus)
sum(d>beta_plus)
sub = (d>beta_plus)
Ytrunc = v[,sub] %*% diag(d[sub]) %*% t(v[,sub])
```
Try running flashr on the covariance matrix
```{r}
library(flashr)
S = (1/n) * Ynorm %*% t(Ynorm)
diag(S) <- NA
S.flash = flash(S,K=50)
```
And on the truncated covariance
```{r}
Strunc = (1/n) * Ytrunc %*% t(Ytrunc)
diag(Strunc) <- NA
Strunc.flash = flash(Strunc,K=50)
```
Plot the factors from flash
```{r}
par(mfrow=c(3,4))
par(mai=rep(0.1,4))
for(i in 1:12){
plot(S.flash$ldf$l[,i],main=paste0(i))
}
```
Compare factors from flash and truncated:
```{r}
par(mfrow=c(3,4))
par(mai=rep(0.1,4))
for(i in 1:12){
plot(S.flash$ldf$l[,i],Strunc.flash$ldf$l[,i])
}
```
```{r}
image(cor(S.flash$ldf$l[,1:12],Strunc.flash$ldf$l[,1:12]))
plot(S.flash$ldf$l[,7],Strunc.flash$ldf$l[,6])
```
```{r}
gene_names =colnames(df)[-1]
gene_names[order(S.flash$ldf$l[,2])]
```
# Running on the cells
To cluster cells (rather than genes) we need to run flash on
the larger covariance (cell-cell).
```{r}
Scell = (1/p) * t(Ynorm) %*% Ynorm
diag(Scell) <- NA
Scell.flash = flash(Scell,K=50)
```
```{r}
par(mfrow=c(4,4))
par(mai=rep(0.1,4))
for(i in 1:16){
plot(Scell.flash$ldf$l[,i],main=paste0(i))
}
```