Skip to content

Commit 8d6e9a4

Browse files
committed
Separate the function cvit in another file
1 parent e92f4c5 commit 8d6e9a4

4 files changed

Lines changed: 55 additions & 49 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
*.jls
5+
tmp/
6+

Example/Compare.pdf

-28 Bytes
Binary file not shown.

Example/cvit.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# CVIT - Create itr and itst indeces for k-fold-cv
2+
#
3+
# Description
4+
# [ITR,ITST]=CVITR(N,K) returns 1xK cell arrays ITR and ITST holding
5+
# cross-validation indeces for train and test sets respectively.
6+
# K-fold division is balanced with all sets having floor(N/K) or
7+
# ceil(N/K) elements.
8+
#
9+
# [ITR,ITST]=CVITR(N,K,RS) with integer RS=true also makes random
10+
# permutation, using substream RS. This way different permutations
11+
# can be produced with different RS values, but same permutation is
12+
# obtained when called again with same RS. Function restores the
13+
# previous random stream before exiting.
14+
#
15+
16+
17+
# Copyright (c) 2010 Aki Vehtari
18+
19+
# This software is distributed under the GNU General Public
20+
# License (version 2 or later); please refer to the file
21+
# License.txt, included with the software, for details.
22+
23+
function cvit(n, k=10, rsubstream=false)
24+
25+
a = k-rem(n,k)
26+
b = floor(Int, n/k);
27+
28+
itst = Any[]
29+
itr = Any[]
30+
31+
for cvi in 1:a
32+
push!(itst, collect(1:b) + (cvi-1) * b)
33+
push!(itr, setdiff(1:n,itst[cvi]))
34+
end
35+
for cvi in (a+1):k
36+
push!(itst, (a * b) + collect(1:(b + 1)) + (cvi - a - 1) * (b + 1))
37+
push!(itr, setdiff(1:n,itst[cvi]))
38+
end
39+
40+
if rsubstream
41+
rng = MersenneTwister()
42+
rii = randperm(rng, n)
43+
for cvi in 1:k
44+
itst[cvi] = rii[itst[cvi]]
45+
itr[cvi] = rii[itr[cvi]]
46+
end
47+
end
48+
itr, itst
49+
end
50+

Example/demo_wells.jl

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,7 @@ using Stan
88

99
using PSIS
1010

11-
# CVIT - Create itr and itst indeces for k-fold-cv
12-
#
13-
# Description
14-
# [ITR,ITST]=CVITR(N,K) returns 1xK cell arrays ITR and ITST holding
15-
# cross-validation indeces for train and test sets respectively.
16-
# K-fold division is balanced with all sets having floor(N/K) or
17-
# ceil(N/K) elements.
18-
#
19-
# [ITR,ITST]=CVITR(N,K,RS) with integer RS>0 also makes random
20-
# permutation, using substream RS. This way different permutations
21-
# can be produced with different RS values, but same permutation is
22-
# obtained when called again with same RS. Function restores the
23-
# previous random stream before exiting.
24-
#
25-
26-
# Copyright (c) 2010 Aki Vehtari
27-
28-
# This software is distributed under the GNU General Public
29-
# License (version 2 or later); please refer to the file
30-
# License.txt, included with the software, for details.
31-
32-
function cvit(n, k=10, rsubstream=false)
33-
34-
a = k-rem(n,k)
35-
b = floor(Int, n/k);
36-
37-
itst = Any[]
38-
itr = Any[]
39-
40-
for cvi in 1:a
41-
push!(itst, collect(1:b) + (cvi-1) * b)
42-
push!(itr, setdiff(1:n,itst[cvi]))
43-
end
44-
for cvi in (a+1):k
45-
push!(itst, (a * b) + collect(1:(b + 1)) + (cvi - a - 1) * (b + 1))
46-
push!(itr, setdiff(1:n,itst[cvi]))
47-
end
48-
49-
if rsubstream
50-
rng = MersenneTwister()
51-
rii = randperm(rng, n)
52-
for cvi in 1:k
53-
itst[cvi] = rii[itst[cvi]]
54-
itr[cvi] = rii[itr[cvi]]
55-
end
56-
end
57-
itr, itst
58-
end
11+
include("cvit.jl")
5912

6013

6114
# Data
@@ -156,7 +109,7 @@ for cvi in 1:10
156109
r,v,c = size(sim2)
157110
ns = filter(x->startswith(x,"log_likt"),simcv.names)
158111
log_likt = reshape(permutedims(simcv[:,ns,:].value,[3, 1 ,2]), (r*c ,length(ns)))
159-
kfcvs[cvitst[cvi]]= Psis.logsumexp(log_likt) - log(size(log_likt,1))
112+
kfcvs[cvitst[cvi]]= PSIS.logsumexp(log_likt) - log(size(log_likt,1))
160113
end
161114

162115
# compare PSIS-LOO and k-fold-CV

0 commit comments

Comments
 (0)