Commit 224e02c2 authored by Peter Jentsch's avatar Peter Jentsch
Browse files

Seperated Zeroweighteddistributions into its own package, implemented WS/Rest...

Seperated Zeroweighteddistributions into its own package, implemented WS/Rest functions, although fitting doesn't work too well yet
parent 2a60eb9d
This diff is collapsed.
......@@ -23,6 +23,7 @@ RandomNumbers = "e6cf234a-135c-5ec9-84dd-332b85af5143"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
ThreadsX = "ac1d9e8a-700a-412c-b207-f0111f4b6c0d"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
ZeroWeightedDistributions = "24733ad3-391a-4e41-8839-c7177de7dea4"
[compat]
julia = "1"
......
module CovidAlertVaccinationModel
# export get_u_0, solve!,get_parameters,plot_model,main,parse_cases_data,AgentModel,vaccination_rate_test,vaccinate_uniformly!
using ZeroWeightedDistributions
using LightGraphs
using RandomNumbers.Xorshifts
using Random
......@@ -12,12 +12,12 @@ import StatsBase:mean
using Dates
using ThreadsX
using DelimitedFiles
using NamedTupleTools
# using CUDA
using NetworkLayout:Stress
using NetworkLayout:SFDP
import Base.rand
import Random.rand!
import LightGraphs.add_edge!
include("agents.jl")
......
struct ZWDist{BaseDistType,T} <: Sampleable{Univariate,Discrete}
α::T
base_dist::BaseDistType
end
function Base.rand(rng::AbstractRNG, s::ZWDist{DType,EType}, n::T) where {T<:Int, EType, DType}
l = Vector{EType}(undef,n)
Random.rand!(rng,l)
l[l .< s.α] .= 0
Random.rand!(rng,s.base_dist,@view l[l .>= s.α])
return l
function adjust_distributions_mean(distribution_matrix::AbstractArray{T},mean_shift_percentage) where T<:ZWDist
return map(distribution_matrix) do dist
new_mean = mean(dist)*(1 + mean_shift_percentage)
return from_mean(typeof(dist),dist.α, new_mean)
end
end
function Random.rand!(rng::AbstractRNG, s::ZWDist, l::T) where T<:AbstractVector
Random.rand!(rng,l)
l[l .< s.α] .= 0
Random.rand!(rng,s.base_dist,@view l[l .>= s.α])
return l
function adjust_distributions_mean(distribution_matrix::AbstractArray{T},mean_shift_percentage) where T<:Distribution
return map(distribution_matrix) do dist
new_mean = mean(dist)*(1 + mean_shift_percentage)
return from_mean(typeof(dist), new_mean)
end
end
function Base.rand(rng::AbstractRNG, s::ZWDist{DType,T}) where {DType,T}
return ifelse(Base.rand(rng) < s.α, zero(T), Base.rand(rng,s.base_dist))
function ZeroPoisson(α,λ)
return ZWDist(α,Poisson(λ))
end
function ZeroGeometric(α,p)
return ZWDist(α,Geometric(p))
end
function from_mean(::Type{Geometric{T}},μ) where T
......@@ -37,39 +34,16 @@ end
function from_mean(::Type{ZWDist{DistType,T}},α,μ) where {DistType <: Distribution{Univariate,W} where W, T}
return ZWDist(α,from_mean(DistType,μ/(1-α)))
end
function ZeroPoisson(α,λ)
return ZWDist(α,Poisson(λ))
end
function ZeroGeometric(α,p)
return ZWDist(α,Geometric(p))
end
function adjust_distributions_mean(distribution_matrix::AbstractArray{T},mean_shift_percentage) where T<:ZWDist
return map(distribution_matrix) do dist
new_mean = mean(dist)*(1 + mean_shift_percentage)
return from_mean(typeof(dist),dist.α, new_mean)
end
end
function adjust_distributions_mean(distribution_matrix::AbstractArray{T},mean_shift_percentage) where T<:Distribution
return map(distribution_matrix) do dist
new_mean = mean(dist)*(1 + mean_shift_percentage)
return from_mean(typeof(dist), new_mean)
end
end
StatsBase.mean(d::ZWDist{Dist,T}) where {Dist,T} = (1 - d.α)*StatsBase.mean(d.base_dist)
const initial_workschool_type = Union{ZWDist{Geometric{Float64},Float64},ZWDist{Poisson{Float64},Float64}}
const initial_workschool_type = Union{ZWDist{Geometric{Float64},Discrete},ZWDist{Poisson{Float64},Discrete}}
const initial_workschool_mixing_matrix = convert(Array{initial_workschool_type},map(t->from_mean(t...),[
(ZWDist{Geometric{Float64},Float64}, 0.433835,4.104848) (ZWDist{Geometric{Float64},Float64},0.406326,2.568782) (ZWDist{Poisson{Float64},Float64},0.888015,0.017729) (ZWDist{Geometric{Float64},Float64},0.406326,2.568782) (ZWDist{Poisson{Float64},Float64},0.888015,0.017729)
(ZWDist{Geometric{Float64},Float64}, 0.600966,0.975688) (ZWDist{Geometric{Float64},Float64},0.4306,5.057572) (ZWDist{Poisson{Float64},Float64}, 0.84513,0.021307) (ZWDist{Geometric{Float64},Float64},0.4306,5.057572) (ZWDist{Poisson{Float64},Float64}, 0.84513,0.021307)
(ZWDist{Poisson{Float64},Float64},0.887392,0.001937) (ZWDist{Poisson{Float64},Float64},0.793004,0.00722) (ZWDist{Poisson{Float64},Float64},0.940473, 0.022134) (ZWDist{Poisson{Float64},Float64},0.793004,0.00722) (ZWDist{Poisson{Float64},Float64},0.940473, 0.022134)
(ZWDist{Geometric{Float64},Float64}, 0.600966,0.975688) (ZWDist{Geometric{Float64},Float64},0.4306,5.057572) (ZWDist{Poisson{Float64},Float64}, 0.84513,0.021307) (ZWDist{Geometric{Float64},Float64},0.4306,5.057572) (ZWDist{Poisson{Float64},Float64}, 0.84513,0.021307)
(ZWDist{Poisson{Float64},Float64},0.887392,0.001937) (ZWDist{Poisson{Float64},Float64},0.793004,0.00722) (ZWDist{Poisson{Float64},Float64},0.940473, 0.022134) (ZWDist{Poisson{Float64},Float64},0.793004,0.00722) (ZWDist{Poisson{Float64},Float64},0.940473, 0.022134)
(ZWDist{Geometric{Float64},Discrete}, 0.433835,4.104848) (ZWDist{Geometric{Float64},Discrete},0.406326,2.568782) (ZWDist{Poisson{Float64},Discrete},0.888015,0.017729) (ZWDist{Geometric{Float64},Discrete},0.406326,2.568782) (ZWDist{Poisson{Float64},Discrete},0.888015,0.017729)
(ZWDist{Geometric{Float64},Discrete}, 0.600966,0.975688) (ZWDist{Geometric{Float64},Discrete},0.4306,5.057572) (ZWDist{Poisson{Float64},Discrete}, 0.84513,0.021307) (ZWDist{Geometric{Float64},Discrete},0.4306,5.057572) (ZWDist{Poisson{Float64},Discrete}, 0.84513,0.021307)
(ZWDist{Poisson{Float64},Discrete},0.887392,0.001937) (ZWDist{Poisson{Float64},Discrete},0.793004,0.00722) (ZWDist{Poisson{Float64},Discrete},0.940473, 0.022134) (ZWDist{Poisson{Float64},Discrete},0.793004,0.00722) (ZWDist{Poisson{Float64},Discrete},0.940473, 0.022134)
(ZWDist{Geometric{Float64},Discrete}, 0.600966,0.975688) (ZWDist{Geometric{Float64},Discrete},0.4306,5.057572) (ZWDist{Poisson{Float64},Discrete}, 0.84513,0.021307) (ZWDist{Geometric{Float64},Discrete},0.4306,5.057572) (ZWDist{Poisson{Float64},Discrete}, 0.84513,0.021307)
(ZWDist{Poisson{Float64},Discrete},0.887392,0.001937) (ZWDist{Poisson{Float64},Discrete},0.793004,0.00722) (ZWDist{Poisson{Float64},Discrete},0.940473, 0.022134) (ZWDist{Poisson{Float64},Discrete},0.793004,0.00722) (ZWDist{Poisson{Float64},Discrete},0.940473, 0.022134)
]))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment