function share(δ, Σ, dFν, x)
function shareν(ν)
s = exp.(δ .+ x*Σ*ν)
return(s./sum(s))
end
return(∫(shareν, dFν))
end
share (generic function with 1 method)
2024-09-23
\[ \def\Er{{\mathrm{E}}} \def\En{{\mathbb{E}_n}} \def\cov{{\mathrm{Cov}}} \def\var{{\mathrm{Var}}} \def\R{{\mathbb{R}}} \def\arg{{\mathrm{arg}}} \newcommand\norm[1]{\left\lVert#1\right\rVert} \def\rank{{\mathrm{rank}}} \newcommand{\inpr}{ \overset{p^*_{\scriptscriptstyle n}}{\longrightarrow}} \def\inprob{{\,{\buildrel p \over \rightarrow}\,}} \def\indist{\,{\buildrel d \over \rightarrow}\,} \DeclareMathOperator*{\plim}{plim} \DeclareMathOperator*{\argmax}{argmax} \DeclareMathOperator*{\argmin}{argmin} \]
function share(δ, Σ, dFν, x)
function shareν(ν)
s = exp.(δ .+ x*Σ*ν)
return(s./sum(s))
end
return(∫(shareν, dFν))
end
share (generic function with 1 method)
∫(shareν, dFν)
is not a function that exists, will create it next@doc raw"""
share(δ, Σ, dFν, x)
Computes shares in random coefficient logit with mean tastes `δ`, observed characteristics `x`, unobserved taste distribution `dFν`, and taste covariances `Σ`.
# Arguments
- `δ` vector of length `J`
- `Σ` `K` by `K` matrix
- `dFν` distribution of length `K` vector
- `x` `J` by `K` array
# Returns
- vector of length `J` consisting of $s_1$, ..., $s_J$
"""
function share(δ, Σ, dFν, x)
J,K = size(x)
(length(δ) == J) || error("length(δ)=$(length(δ)) != size(x,1)=$J")
K,K == size(Σ) || error("size(x,1)=$J != size(Σ)=$(size(Σ))")
function shareν(ν)
s = δ .+ x*Σ*ν
s .-= maximum(s)
s .= exp.(s)
s ./= sum(s)
return(s)
end
return(∫(shareν, dFν))
end
Main.Notebook.share
exp
∫mc (generic function with 1 method)
module EC
mutable struct EvaluationCounter
f::Function
n::Int
end
EvaluationCounter(f) = EvaluationCounter(f, 0)
function (ec::EvaluationCounter)(x)
ec.n += 1
return ec.f(x)
end
import Base: show
function show(io::IO, ec::EvaluationCounter)
print(io, "evaluated $(ec.f) $(ec.n) times")
end
function reset!(ec::EvaluationCounter)
ec.n = 0
end
end
using Distributions
dx = Normal(0, 1)
f = EC.EvaluationCounter(x->x^2)
trueint = 1.0
EC.reset!(f)
intmc = ∫mc(f, dx, ndraw=100)
@show intmc, trueint, intmc-trueint
@show f
EC.reset!(f)
(intmc, trueint, intmc - trueint) = (0.9369385559524867, 1.0, -0.06306144404751335)
f = evaluated #6 100 times
0
using Sobol
import Base.Iterators: take
function ∫s(f,dx::AbstractMvNormal;ndraw=100)
marginals = [Normal(dx.μ[i], sqrt(dx.Σ[i,i])) for i in 1:length(dx)]
invcdf(x) = quantile.(marginals,x)
ss = skip(SobolSeq(length(dx)),ndraw)
mean(f(invcdf(x)) for x in take(ss,ndraw))
end
function ∫s(f,dx::Normal;ndraw=100)
invcdf(x) = quantile(dx,x)
ss = skip(SobolSeq(length(dx)),ndraw)
mean(f(invcdf(x[1])) for x in take(ss,ndraw))
end
∫s (generic function with 2 methods)
f = x->x^2
S = 1_000
@show mean(abs(∫s(f,dx,ndraw=100) - trueint) for s in 1:S)
@show mean(abs(∫mc(f,dx,ndraw=100) - trueint) for s in 1:S)
mean((abs(∫s(f, dx, ndraw = 100) - trueint) for s = 1:S)) = 0.02875751885407857
mean((abs(∫mc(f, dx, ndraw = 100) - trueint) for s = 1:S)) = 0.11025243264624399
0.11025243264624399
using FastGaussQuadrature, LinearAlgebra
import Base.Iterators: product, repeated
function ∫q(f, dx::MvNormal; ndraw=100)
n = Int(ceil(ndraw^(1/length(dx))))
x, w = gausshermite(n)
L = cholesky(dx.Σ).L
sum(f(√2*L*vcat(xs...) + dx.μ)*prod(ws)
for (xs,ws) ∈ zip(product(repeated(x, length(dx))...),
product(repeated(w, length(dx))...))
)/(π^(length(dx)/2))
end
∫q (generic function with 1 method)
using HCubature
function ∫cuba(f, dx; rtol=1e-4)
D = length(dx)
x(t) = t./(1 .- t.^2)
Dx(t) = prod((1 .+ t.^2)./(1 .- t.^2).^2)
hcubature(t->f(x(t))*pdf(dx,x(t))*Dx(t), -ones(D),ones(D), rtol=rtol)[1]
end
∫cuba (generic function with 1 method)
““” share(δ, Σ, dFν, x)
Computes
δ
vector of length J
Σ
K
by K
matrixdFν
distributino of length K
vectorx
J
by K
arrayJ
consisting of \(s_1\), …, \(s_J\) ““” function share(δ, Σ, dFν, x) J,K = size(x) (length(δ) == J) || error(“length(δ)=\((length(δ)) != size(x,1)=\)J”) K,K == size(Σ) || error(“size(x,1)=\(J != size(Σ)=\)(size(Σ))”) function shareν(ν) s = δ .+ xΣν s .-= maximum(s) s .= exp.(s) s ./= sum(s) return(s) end return(∫(shareν, dFν)) endend ```