-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.jl
168 lines (149 loc) · 4.64 KB
/
tests.jl
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
include("geometric.jl")
using .geometricModule
using Distributions
using HDF5
# Estimates the probability of answering correctly given that the partworth vector is mu
#
# Parameters:
#
# - mu = partworth vector
#
# Returns:
#
# - alpha = the probability estimate
function getAlpha(mu)
dimension = length(mu)
alpha = 0
for i in 1:100000
prof1 = zeros(dimension)
prof2 = zeros(dimension)
while norm(prof1-prof2) < 1e-6
prof1 = rand(0:1,dimension)
prof2 = rand(0:1,dimension)
end
alpha += max(exp(dot(mu,prof1)),exp(dot(mu,prof2)))/(exp(dot(mu,prof1))+exp(dot(mu,prof2)))
end
return alpha/100000
end
# Generates precomputed data
#
# Parameters:
#
# - mu = mean of prior
# - sigma = covariance of prior
# - confidence = level for confidence ellipsoid/polyhedron
#
# Returns
#
# - precomputed data
function precompute(mu,sigma,confidence)
precomp = Dict{AbstractString,Any}()
distro = Chisq(length(mu))
precomp["rhs"] = quantile(distro, confidence)
precomp["alpha"] = getAlpha(mu)
precomp
end
function savedata(filename,results,methods)
file = open(string(filename,"_time.csv"),"w")
for m in methods
for key in keys(results[m])
println(file,m,",",key,",",mean(results[m][key]),",",maximum(results[m][key]))
end
end
close(file)
end
function test(logfile,filename,iterations,mu,truemu,sigma,confidence,numquestions,methods)
dimension = length(mu)
results = Dict()
for m in methods
results[m]=Dict(
"updatetime" => Float64[],
"questiontime" => Float64[]
)
end
csigma = ctranspose(chol(sigma))
precomp = precompute(mu,sigma,confidence)
filename = string(filename,norm(mu-truemu)<0.0001 ? "":"wrongmu")
serializefile = open(string(filename,"serial.dat"),"a")
betafilename = string(filename,"beta.h5")
betas=[]
if !isfile(betafilename)
h5open(betafilename, "w") do file
write(file, "betas", hcat([truemu + csigma*randn(dimension) for i in 1:iterations]...))
end
end
betas=h5read(betafilename,"betas")
for i in 1:iterations
println("Iteration: ",i,"/",iterations)
beta = betas[:,i]
runseed = rand(1:100000000)
allstats = Dict{AbstractString,Any}()
allstats["beta"]=beta
allstats["alpha"]=precomp["alpha"]
allstats["mu"]=mu
allstats["truemu"]=truemu
allstats["sigma"]=sigma
allstats["results"]=Dict{AbstractString,Any}[]
for m in methods
print(m)
parameters = split(m,"_")
stats = []
srand(runseed)
for tries in 1:100
stats = runsimulation(beta,mu,sigma,confidence,numquestions,precomp,parameters)
if stats["teststatus"] != "Normal"
println(logfile,"Error: Repeating iteration for ",m)
else
break
end
end
if stats["teststatus"] != "Normal"
error("Too many iteration drops")
end
stats["method"]=m
push!(allstats["results"],stats)
end
serialize(serializefile,allstats)
for m in methods
push!(results[m]["updatetime"],allstats["results"][find(x -> x["method"] == m,allstats["results"])[1]]["updatetime"]...)
push!(results[m]["questiontime"],allstats["results"][find(x -> x["method"] == m,allstats["results"])[1]]["questiontime"]...)
end
end
close(serializefile)
savedata(filename,results,methods)
end
function runall(iterations)
dimension = 12
confidence = 0.9
methods = [ "estimateEllipsoid_updateBayesApproximation_initializeEllipsoid_questionDEffPWL_effDim",
"estimateEllipsoid_updateBayesApproximation_initializeEllipsoid_questionDEffPWLFisher_effDim",
"estimateEllipsoid_updateBayesApproximation_initializeEllipsoid_questionDEffPWLFisherQGK_effDim",
"estimateEllipsoid_updateBayesApproximation_initializeEllipsoid_questionMaxMin",
"estimateAnalyticCenter_updatePolyhedral_initializeEllipsoid_questionMaxMin",
"estimateProbPoly_updateNull_initializeEllipsoid_questionMaxMin",
"estimateAnalyticCenterRobust_updateNull_initializeEllipsoid_questionNormalizedCenterMIP"
]
numquestions=16
logfile = open("logfile.txt","w")
for c in [0.5,1.5]
println(logfile,"c = ",c)
for varscale in [0.5,2.0]
println(logfile,"varscale = ",varscale)
basename = string("results/",dimension,"all",c,"c",varscale,"s")
mu = c*ones(dimension)
sigma = c*varscale*eye(dimension)
println(basename)
println("\n true :\n")
println(logfile,"\n true :\n")
test(logfile,basename,iterations,mu,mu,sigma,confidence,numquestions,methods)
tempgaussian = randn(dimension)
scale1=c*varscale*sqrt(2)*(gamma((dimension+1)/2)/gamma((dimension)/2))
distro = Chisq(length(mu))
truemu=mu+scale1*(tempgaussian/norm(tempgaussian))
println("\n false :\n")
println(logfile,"\n false :\n")
test(logfile,basename,iterations,mu,truemu,sigma,confidence,numquestions,methods)
end
end
close(logfile)
end