
| Current Path : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/bootstrapFunction.jl |
using Distributions
using Optim
const N=1000
const K=10
genX = MvNormal(eye(K))
X = rand(genX,N)
X = X'
constant = ones(N)
X = [constant X]
genEpsilon = Normal(0, 1)
epsilon = rand(genEpsilon,N)
trueParams = [-K/2:K/2]*.02
Y = X*trueParams + epsilon
params0 = [trueParams,1]
function loglike(rho,x,y)
beta = rho[1:K+1]
sigma2 = exp(rho[K+2])
residual = y-x*beta
dist = Normal(0, sigma2)
contributions = logpdf(dist,residual)
loglikelihood = sum(contributions)
return -loglikelihood
end
function bootstrapSamples(B)
println("hi")
M=convert(Int,floor(N/2))
samples = zeros(B,K+2)
for b=1:B
theIndex = randperm(N)[1:M]
x = X[theIndex,:]
y = Y[theIndex,:]
function wrapLoglike(rho)
return loglike(rho,x,y)
end
samples[b,:] = optimize(wrapLoglike,params0,method=:cg).minimum
end
samples[:,K+2] = exp(samples[:,K+2])
println("bye")
return samples
end