Skip to content

Commit

Permalink
Update README.md and apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzgillner committed Oct 29, 2024
1 parent 5e47119 commit 7e62c6c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UncertainEvidence"
uuid = "1a86d166-648f-46fd-af52-fbd54890c8f1"
authors = ["Lorenz Gillner <[email protected]"]
version = "0.1.0"
version = "0.1.1"

[compat]
julia = "1.6"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The basic probability assessment (`BPA`) is the fundamental data structure for c
X12 = combine_dempster(X1, X2)
```

Based on this combined structure, the lower (*belief*) and upper bound (*plausibility*) of the likelihood of a cause can be queried:
Based on this combined structure, the lower (*belief*) and upper (*plausibility*) bound of the likelihood of a cause can be queried:

```julia
# lower bound
Expand All @@ -46,7 +46,7 @@ bel('A', X12)
pls('A', X12)
```

The masses of a `BPA` should always sum up to 1. To ensure this, use the method `bpa` instead:
The mass of a `BPA` should always sum up to 1. To ensure this, use the wrapper `bpa` instead:

```julia
X = bpa(
Expand All @@ -62,4 +62,6 @@ X = bpa(
# Set(['C']) => 0.3
```

Note the use of `Set`s in this example; if the sum of masses is lower than 1, the remaining mass is automatically assigned to $\Omega$, the set of all focal elements. This would fail, if all other focal elements would be of type `Char`.
Note the use of `Set`s in this example; if the sum of masses is lower than 1, the remaining mass is automatically assigned to $\Omega$, the set of all focal elements. This would fail, if all other focal elements would be of type `Char`.

Take a look at the [unit tests](test/runtests.jl) for more usage examples.
244 changes: 122 additions & 122 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,125 +5,125 @@ using LazySets
using LinearAlgebra

@testset verbose = true "UncertainEvidence" begin
@testset "basics" begin
# test correct redistribution
A = bpa(
Set("a") => 0.1,
Set("b") => 0.2
)
@test A[Set("ab")] == 0.7
end

@testset verbose = true "focal element types" begin
@testset "characters" begin
# test combination rules, based on Zadeh's paradox, see:
# https://doi.org/10.1609/aimag.v5i3.452

X1 = BPA(
'A' => 0.99,
'B' => 0.01,
'C' => 0.00
)

X2 = BPA(
'A' => 0.00,
'B' => 0.01,
'C' => 0.99
)

X12 = combine_dempster(X1, X2)

@test X12['A'] == 0.0
@test X12['B'] 1.0
@test X12['C'] == 0.0
end

@testset "sets of characters" begin
# three colors example from Wikipedia, see:
# https://en.wikipedia.org/wiki/Dempster%E2%80%93Shafer_theory#Bayesian_approximation

# first sensor
m1 = BPA(
Set("r") => 0.35,
Set("y") => 0.25,
Set("g") => 0.15,
Set("ry") => 0.06,
Set("rg") => 0.05,
Set("yg") => 0.04,
Set("ryg") => 0.1,
)

# second sensor; notice how this one is missing the mass assignment for Ω:
m2 = BPA(
Set("r") => 0.11,
Set("y") => 0.21,
Set("g") => 0.33,
Set("ry") => 0.21,
Set("rg") => 0.01,
Set("yg") => 0.03,
)

# combined data, rounded to two decimal places
m12 = BPA(
Set("r") => 0.32,
Set("y") => 0.33,
Set("g") => 0.24,
Set("yr") => 0.07,
Set("gr") => 0.01,
Set("gy") => 0.01,
Set("gyr") => 0.02,
)

@test sum(values(redistribute!(m2))) == one(Real)

mc = combine_dempster(m1, m2)

@test keys(mc) == keys(m12)

@test sort(round.(values(mc), digits=2)) == sort(collect(values(m12)))
end

@testset "sets of strings" begin
# Zadeh's paradox again, but this time with more descriptive focal elements

X1 = BPA(
["concussion"] => 0.99,
["tumor"] => 0.01,
["migraine"] => 0.00
)

X2 = BPA(
["concussion"] => 0.00,
["tumor"] => 0.01,
["migraine"] => 0.99
)

X12 = combine_dempster(X1, X2)

@test X12[["concussion"]] == 0.0
@test X12[["tumor"]] 1.0
@test X12[["migraine"]] == 0.0
end

@testset "balls (ℝ²)" begin
# earthquake example, inspired by:
# Z. Wang, G. J. Klir (2013): "Fuzzy measure theory"

# epicenter of the earthquake
B = Ball2([2.0, 1.0], 1.0)

# estimates for the earthquake's epicenter
E1 = Ball2([2.5, 0.75], 0.25)
E2 = Ball2([1.8, 1.8], 0.5)
E3 = Ball2([2.5, 2.5], 0.25)
E4 = Ball2([2.7, 2.5], 0.2)

estimates = [E1, E2, E3, E4]
masses = fill(1.0 / 4, 4)
me = bpa(zip(estimates, masses))

@test bel(B, me) == 0.25
@test pls(B, me) == 0.5
end
end
end
@testset "basics" begin
# test correct redistribution of masees when using `bpa`
A = bpa(
Set("a") => 0.1,
Set("b") => 0.2,
)
@test A[Set("ab")] == 0.7
end

@testset verbose = true "focal element types" begin
@testset "characters" begin
# test combination rules, based on Zadeh's paradox, see:
# https://doi.org/10.1609/aimag.v5i3.452

X1 = BPA(
'A' => 0.99,
'B' => 0.01,
'C' => 0.00,
)

X2 = BPA(
'A' => 0.00,
'B' => 0.01,
'C' => 0.99,
)

X12 = combine_dempster(X1, X2)

@test X12['A'] == 0.0
@test X12['B'] 1.0
@test X12['C'] == 0.0
end

@testset "sets of characters" begin
# three colors example from Wikipedia, see:
# https://en.wikipedia.org/wiki/Dempster%E2%80%93Shafer_theory#Bayesian_approximation

# first sensor
m1 = BPA(
Set("r") => 0.35,
Set("y") => 0.25,
Set("g") => 0.15,
Set("ry") => 0.06,
Set("rg") => 0.05,
Set("yg") => 0.04,
Set("ryg") => 0.1,
)

# second sensor; notice how this one is missing the mass assignment for Ω:
m2 = BPA(
Set("r") => 0.11,
Set("y") => 0.21,
Set("g") => 0.33,
Set("ry") => 0.21,
Set("rg") => 0.01,
Set("yg") => 0.03,
)

# combined data, rounded to two decimal places
m12 = BPA(
Set("r") => 0.32,
Set("y") => 0.33,
Set("g") => 0.24,
Set("yr") => 0.07,
Set("gr") => 0.01,
Set("gy") => 0.01,
Set("gyr") => 0.02,
)

@test sum(values(redistribute!(m2))) == one(Real)

mc = combine_dempster(m1, m2)

@test keys(mc) == keys(m12)

@test sort(round.(values(mc), digits = 2)) == sort(collect(values(m12)))
end

@testset "sets of strings" begin
# Zadeh's paradox again, but this time with more descriptive focal elements

X1 = BPA(
["concussion"] => 0.99,
["tumor"] => 0.01,
["migraine"] => 0.00,
)

X2 = BPA(
["concussion"] => 0.00,
["tumor"] => 0.01,
["migraine"] => 0.99,
)

X12 = combine_dempster(X1, X2)

@test X12[["concussion"]] == 0.0
@test X12[["tumor"]] 1.0
@test X12[["migraine"]] == 0.0
end

@testset "balls (ℝ²)" begin
# earthquake example, inspired by:
# Z. Wang, G. J. Klir (2013): "Fuzzy measure theory"

# epicenter of the earthquake
B = Ball2([2.0, 1.0], 1.0)

# estimates for the earthquake's epicenter
E1 = Ball2([2.5, 0.75], 0.25)
E2 = Ball2([1.8, 1.8], 0.5)
E3 = Ball2([2.5, 2.5], 0.25)
E4 = Ball2([2.7, 2.5], 0.2)

estimates = [E1, E2, E3, E4]
masses = fill(1.0 / 4, 4)
me = bpa(zip(estimates, masses))

@test bel(B, me) == 0.25
@test pls(B, me) == 0.5
end
end
end

0 comments on commit 7e62c6c

Please sign in to comment.