Blame view

code/julia/forward.jl 4.41 KB
3e0f9b8a   Francisco Coelho   back to work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Combinatorics
using Symbolics
using Latexify
Latexify.set_default(;env=:raw)
#=

\mu(e) :-
    \mu(e,t),
    \mu_TC(t).

\mu_TC(t).

\mu(e,t) :-
    \mu([e], t),
    #[e].

#[e].

\mu([e], t) :- \mu(s, t), s \in [[e]]

\mu(s, t) :- \theta_s,t.

\theta_s,t.
=#


neg(a) = islowercase(a[1]) ? uppercase(a) : lowercase(a)
literals(atoms) = join(sort(collect(atoms * neg.(atoms))), "")

function is_consistent(event)
    for (i,x) in enumerate(event)
        for y in event[i:end]
            if y == neg(x)
                return false
            end
        end
    end 
    return true
end


omega(atoms) = join.(powerset(literals(atoms)), "")
consistent(atoms) = join.(filter(is_consistent, omega(atoms)))

sbf_atoms = "abc"

@variables θ
sbf_theta = Dict(
    ("A", "A") => 1,
    ("ab", "a") => θ,
    ("ac", "a") => 1 - θ
)

sbf_totalchoices = unique(values(sbf_theta))
sbf_probabilities = Dict(
    "a" => 3 // 10,
    "A" => 7 // 10
)

mu_st(s::String, t::String; theta=sbf_theta) = get(theta, (s,t), 0)

sbf_core = Dict{String,String}(
    "B" => "i", # independent class
    "C" => "i", 
    "bc" => "i", 
    "Ba" => "i", 
    "Bc" => "i", 
    "BC" => "i", 
    "Ca" => "i", 
    "Cb" => "i", 
    "BCa" => "i",
    "A" => "A",
    "Ab" => "A",
    "Ac" => "A",
    "AB" => "A",
    "AC" => "A",
    "Abc" => "A",
    "ACb" => "A",
    "ABc" => "A",
    "ABC" => "A",
    "b" => "ab",
    "ab" => "ab",
    "Cab" => "ab",
    "c" => "ac",
    "ac" => "ac",
    "Bac" => "ac",
    "a" => "ab,ac",
    "abc" => "ab,ac",
    "" => "A,ab,ac",
)

stablecore(e::String; core::Dict{String,String}=sbf_core) = Set(
    string.(split(get(core, e, "⊥"), ","))
    )

classes(core=sbf_core) =  Dict( 
    e => Set(k for k in keys(core) if core[k] == core[e]) for e in keys(core)
    )

sbf_classes = classes(sbf_core)

mu_class(e::String, t::String; theta=sbf_theta, core=sbf_core) = 
    sum(mu_st(s,t, theta=theta) for s in stablecore(e, core=core))

mu_class(e::String; theta=sbf_theta, core=sbf_core, tc_probabilities=sbf_probabilities) = 
    sum(mu_class(e, t, theta=theta,core=core) * p for (t, p) in tc_probabilities)

function mu(e::String, t::String; 
        theta=sbf_theta,
        core=sbf_core,
        classes=sbf_classes)
    class_size = length(classes[e])
    if class_size > 0
        return mu_class(e, t, theta=sbf_theta, core=sbf_core) // class_size
    else
        return 0
    end
end

mu(e::String; 
        theta=sbf_theta, 
        core=sbf_core,
        classes=sbf_classes, 
        tc_probabilities=sbf_probabilities) = 
    sum(mu(e, t, theta=theta, core=core, classes=classes) * p for (t, p) in tc_probabilities)

sbf_quoset = unique(values(sbf_classes))


Z = 0 // 1
for qs in sbf_quoset
    class_size = length(qs)
    if  class_size > 0
        e = [x for x in qs][1]
        mce = mu_class(e)
        global Z = Z + mce
    end
end
Z = simplify(Z)
println("Z = $Z")

# for qs in sbf_quoset
#     class_size = length(qs)
#     if  class_size > 0
#         e = [x for x in qs][1]
#         e_fmt = rpad(e,3)
#         cs_fmt = rpad(class_size, 3)
#         sc_fmt = rpad(join(stablecore(e), ", "), 20)
#         mu_ceA = rpad(latexify(mu_class(e, "A")), 20)
#         mu_cea = rpad(latexify(mu_class(e, "a")), 20)
#         mce = mu_class(e)
#         mu_ce  = rpad(latexify(mce), 20)
#         mu_eA = rpad(latexify(mu(e, "A")), 20)
#         mu_ea = rpad(latexify(mu(e, "a")), 20)
#         mu_e  = rpad(latexify(mu(e)), 20)
#         h_line="#[$e_fmt] = $cs_fmt; SC: $sc_fmt"
#         c_line = "[$e_fmt]\tA: $mu_ceA\ta: $mu_cea\tμ~: $mu_ce"
#         e_line = "'$e_fmt'\tA: $mu_eA\ta: $mu_ea\tμ~: $mu_e"
#         println(join([h_line, c_line, e_line], "\n\t"))    
#     end
# end

for qs in sbf_quoset
    class_size = length(qs)
    if  class_size > 0
        e = [x for x in qs][1]
        e_fmt = rpad(e,3)
        sc_fmt = rpad(join(stablecore(e), ", "), 20)
        pr_e = simplify(mu(e) // Z)
        pr_c = simplify(pr_e * class_size)
        pe_fmt  = rpad(latexify(pr_e), 20)
        pce_fmt  = rpad(latexify(pr_c), 20)
        h_line="P(E = $e_fmt) = $pe_fmt; P(E ∈ [$e_fmt]) = $pce_fmt | SC: $sc_fmt."
        println(h_line)    
    end
end

#=
for e in consistent(sbf_atoms)
    e_fmt = rpad(e,3)
    sc_fmt = rpad(join(stablecore(e), ", "), 20)
    class_size = length(sbf_classes[e]
    cs_fmt = rpad(class_size), 3)
    println("[[$e_fmt]] #$cs_fmt: $sc_fmt\tA: $(rpad(latexify(mu(e, "A")),20))\ta:$(rpad(latexify(mu(e, "a")), 20))")
end
=#