Skip to content

Commit

Permalink
add formatter, update structs
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdfish committed Apr 13, 2024
1 parent d25d416 commit 634f938
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 175 deletions.
29 changes: 29 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
always_use_return = false
annotate_untyped_fields_with_any = false
conditional_to_if = false
import_to_using = false
join_lines_based_on_source = true
normalize_line_endings = "unix"
pipe_to_function_call = false
remove_extra_newlines = true
short_to_long_function_def = false
long_to_short_function_def = false
whitespace_in_kwargs = true
whitespace_ops_in_indices = true
whitespace_typedefs = true
indent = 4
margin = 92
format_docstrings = false
align_struct_field = false
align_assignment = false
align_conditional = false
align_pair_arrow = false
align_matrix = false
trailing_comma = false
trailing_zero = true
indent_submodule = false
separate_kwargs_with_semicolon = false
surround_whereop_typeparameters = true
variable_call_indent = []
yas_style_nesting = false
disallow_single_arg_nesting = true
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
name = "UtilityModels"
uuid = "bddc258c-9485-48da-848b-3e4c1ee8966c"
authors = ["itsdfish"]
version = "0.3.2"
version = "0.3.3"

[deps]
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
ConcreteStructs = "0.2.3"
Distributions = "0.24.0, 0.25"
SafeTestsets = "0.0.1, 0.1"
StatsBase = "0.33, 0.34"
Expand Down
12 changes: 6 additions & 6 deletions src/ExpectedUtility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Constructor
ExpectedUtility(;α=.80)
````
"""
mutable struct ExpectedUtility{T} <:UtilityModel
mutable struct ExpectedUtility{T} <: UtilityModel
α::T
end

function ExpectedUtility(;α=.80)
function ExpectedUtility(; α = 0.80)
return ExpectedUtility(α)
end

Expand All @@ -32,8 +32,8 @@ compute_utility(model::ExpectedUtility, gamble::Gamble)
````
"""
function compute_utility(model::ExpectedUtility, gamble::Gamble)
(;α) = model
(;v) = gamble
utility = @. sign(v) * abs(v)^α
(; α) = model
(; v) = gamble
utility = @. sign(v) * abs(v)^α
return utility
end
end
6 changes: 3 additions & 3 deletions src/Gamble.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Constructs a gamble object with probability vector `p` and outcome vector `v`.
- `p`: probability vector
- `v`: outcome vector
"""
mutable struct Gamble{T1,T2}
mutable struct Gamble{T1, T2}
p::T1
v::T2
end

function Gamble(;p=[.5,.5], v=[10.0,0.0])
function Gamble(; p = [0.5, 0.5], v = [10.0, 0.0])
return Gamble(p, v)
end

function sample(gamble::Gamble)
return sample(gamble.v, Weights(gamble.p))
end

mean(g::Gamble) = g.v' * g.p
mean(g::Gamble) = g.v' * g.p
60 changes: 33 additions & 27 deletions src/ProspectTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ Fennema, H., & Wakker, P. (1997). Original and cumulative prospect theory: A dis
Tversky, A., & Kahneman, D. (1992). Advances in prospect theory: Cumulative representation of uncertainty. Journal of Risk and uncertainty, 5(4), 297-323.
"""
@concrete mutable struct ProspectTheory <:UtilityModel
α
β
γg
γl
λ
mutable struct ProspectTheory{T <: Real} <: UtilityModel
α::T
β::T
γg::T
γl::T
λ::T
end

function ProspectTheory(;α=.80, β=α, γg=.70, γl=γg, λ=2.25)
function ProspectTheory(; α = 0.80, β = α, γg = 0.70, γl = γg, λ = 2.25)
return ProspectTheory(α, β, γg, γl, λ)
end

function ProspectTheory(α, β, γg, γl, λ)
return ProspectTheory(promote(α, β, γg, γl, λ)...)
end

"""
compute_utility(model::ProspectTheory, gamble)
Expand All @@ -41,10 +45,10 @@ Computes utility of gamble outcomes according to prospect theory
- `gamble`: a gamble object
"""
function compute_utility(model::ProspectTheory, gamble)
(;α,β,λ) = model
vl,vg = split_values(gamble)
utilg = vg.^α
utill = @. -λ * abs(vl)^β
(; α, β, λ) = model
vl, vg = split_values(gamble)
utilg = vg .^ α
utill = @. -λ * abs(vl)^β
return [utill; utilg]
end

Expand All @@ -59,8 +63,8 @@ Computes decision weights based on cummulative outcomes
- `gamble`: a gamble object
"""
function compute_weights(model::ProspectTheory, gamble::Gamble)
pl,pg = split_probs(gamble)
(;γg,γl) = model
pl, pg = split_probs(gamble)
(; γg, γl) = model
ω = [_compute_weights(pl, γl); _compute_weights(pg, γg)]
return ω
end
Expand All @@ -77,40 +81,42 @@ Computes decision weights based on cummulative outcomes
"""
function _compute_weights(p, γ)
n = length(p)
f(i) = weight(sum(p[i:n]), γ) - weight(sum(p[(i+1):n]), γ)
ω = [f(i) for i in 1:n-1]
f(i) = weight(sum(p[i:n]), γ) - weight(sum(p[(i + 1):n]), γ)
ω = [f(i) for i = 1:(n - 1)]
isempty(p) ? nothing : push!(ω, weight(p[n], γ))
return ω
end

function weight(p, γ)
function weight(p, γ)
p = min(p, 1.0) # to deal with overflow
return (p^γ) / (p^γ + (1-p)^γ)^(1/γ)
return (p^γ) / (p^γ + (1 - p)^γ)^(1 / γ)
end

function sort!(model::ProspectTheory, gamble)
(;p,v) = gamble
(; p, v) = gamble
i = sortperm(v)
p .= p[i]; v .= v[i]
p .= p[i]
v .= v[i]
gains = v .>= 0
pl = @view p[.!gains]
vl = @view v[.!gains]
reverse!(vl); reverse!(pl)
reverse!(vl)
reverse!(pl)
return nothing
end

function split_values(gamble)
(;v) = gamble
(; v) = gamble
gains = v .>= 0
vg = @view v[gains]
vg = @view v[gains]
vl = @view v[.!gains]
return vl,vg
return vl, vg
end

function split_probs(gamble)
(;v,p) = gamble
(; v, p) = gamble
gains = v .>= 0
pg = @view p[gains]
pg = @view p[gains]
pl = @view p[.!gains]
return pl,pg
end
return pl, pg
end
41 changes: 23 additions & 18 deletions src/TAX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ Constructs a model object for transfer of attention exchange.
Birnbaum, M. H., & Chavez, A. (1997). Tests of theories of decision making: Violations of branch independence and distribution independence. Organizational Behavior and Human Decision Processes, 71(2), 161-194.
Birnbaum, M. H. (2008). New paradoxes of risky decision making. Psychological Review, 115(2), 463.
"""
@concrete mutable struct TAX <:UtilityModel
δ
γ
β
mutable struct TAX{T <: Real} <: UtilityModel
δ::T
γ::T
β::T
end

function TAX(;δ=-1.0, β=1.0, γ=.70)
function TAX(; δ = -1.0, β = 1.0, γ = 0.70)
return TAX(δ, γ, β)
end

function TAX(δ, γ, β)
return TAX(promote(δ, γ, β)...)
end

"""
compute_utility(model::TAX, gamble)
Expand All @@ -35,25 +39,26 @@ Computes utility of gamble outcomes according to TAX
- `gamble`: a gamble object
"""
function compute_utility(model::TAX, gamble)
(;β) = model
(;v) = gamble
utility = @. sign(v) * abs(v)^β
(; β) = model
(; v) = gamble
utility = @. sign(v) * abs(v)^β
return utility
end

tax_weight(p, γ) = (p^γ)

function ω(p, pk, n, δ, γ)
if δ > 0
return δ * tax_weight(pk, γ) / (n+1)
return δ * tax_weight(pk, γ) / (n + 1)
end
return δ * tax_weight(p, γ) / (n+1)
return δ * tax_weight(p, γ) / (n + 1)
end

function sort!(model::TAX, gamble)
(;p,v) = gamble
(; p, v) = gamble
i = sortperm(v)
p .= p[i]; v .= v[i]
p .= p[i]
v .= v[i]
return nothing
end

Expand All @@ -68,24 +73,24 @@ Computes mean utility for the TAX model
- `gamble`: a gamble object
"""
function mean(model::TAX, gamble::Gamble)
(;p,v) = gamble
(;γ,δ) = model
(; p, v) = gamble
(; γ, δ) = model
n = length(p)
sort!(model, gamble)
utility = compute_utility(model, gamble)
eu = 0.0
sum_weight = 0.0
for i in 1:n
for i = 1:n
eu += tax_weight(p[i], γ) * utility[i]
sum_weight += tax_weight(p[i], γ)
for k in 1:(i-1)
for k = 1:(i - 1)
eu += (utility[i] - utility[k]) * ω(p[i], p[k], n, δ, γ)
end
end
return eu/sum_weight
return eu / sum_weight
end

function var(model::TAX, gamble::Gamble)
error("var not implimented for TAX")
return -100.0
end
end
4 changes: 2 additions & 2 deletions src/UtilityModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ end
"""
function var(model::UtilityModel, gamble::Gamble)
(;p) = gamble
(; p) = gamble
utility = compute_utility(model, gamble)
eu = mean(model, gamble)
return sum(p .* (utility .- eu).^2)
return sum(p .* (utility .- eu) .^ 2)
end

"""
Expand Down
61 changes: 30 additions & 31 deletions src/UtilityModels.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
module UtilityModels

using StatsBase
using Distributions
using ConcreteStructs
using StatsBase
using Distributions

import Distributions: ContinuousUnivariateDistribution
import Distributions: mean
import Distributions: var
import Distributions: std
import Distributions: rand
import Distributions: logpdf
import StatsBase: sample
export UtilityModel
export TAX
export ProspectTheory
export ExpectedUtility
export ValenceExpectancy
export Gamble
export mean
export var
export std
export pdf
export logpdf
export sample
#export rand
include("Gamble.jl")
include("UtilityModel.jl")
include("ProspectTheory.jl")
include("ExpectedUtility.jl")
include("ValenceExpectancy.jl")
include("TAX.jl")
import Distributions: ContinuousUnivariateDistribution
import Distributions: mean
import Distributions: var
import Distributions: std
import Distributions: rand
import Distributions: logpdf
import StatsBase: sample

export UtilityModel
export TAX
export ProspectTheory
export ExpectedUtility
export ValenceExpectancy
export Gamble
export mean
export var
export std
export pdf
export logpdf
export sample
#export rand

include("Gamble.jl")
include("UtilityModel.jl")
include("ProspectTheory.jl")
include("ExpectedUtility.jl")
include("ValenceExpectancy.jl")
include("TAX.jl")
end
Loading

0 comments on commit 634f938

Please sign in to comment.