Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Nicola Botta
HaskellLibs
Commits
e9d83efa
Commit
e9d83efa
authored
Feb 26, 2021
by
Nicola Botta
Browse files
Added CP examples.
parent
13886a0d
Changes
1
Hide whitespace changes
Inline
Side-by-side
IntegerLineMPD/prob1.lhs
View file @
e9d83efa
...
...
@@ -35,6 +35,19 @@ What is the probability of ~a~? (If ~a~ is not in the support of pd, we "round"
>
lookup
a
[(
_
,
p
)]
=
p
>
lookup
a
((
a'
,
p
)
:
rest
)
=
if
a
<=
a'
then
p
else
lookup
a
rest
Probabilities:
>
prob
::
(
a
->
Bool
)
->
PD
a
->
Double
>
prob
e
(
PD
aps
)
=
sum
[
p
|
(
a
,
p
)
<-
aps
,
e
a
==
True
]
>
lift
::
(
Bool
->
Bool
->
Bool
)
->
(
a
->
Bool
)
->
(
a
->
Bool
)
->
(
a
->
Bool
)
>
lift
op
f
g
=
\
a
->
op
(
f
a
)
(
g
a
)
>
condProb
::
(
a
->
Bool
)
->
(
a
->
Bool
)
->
PD
a
->
Maybe
Double
>
condProb
e1
e2
pd
=
let
pe2
=
prob
e2
pd
in
>
if
pe2
==
0.0
>
then
Nothing
>
else
Just
(
prob
(
lift
(
&&
)
e1
e2
)
pd
/
pe2
)
Cummulative probabilities:
...
...
@@ -51,6 +64,7 @@ Expected value:
>
expectation
::
PD
Double
->
Double
>
expectation
(
PD
as
)
=
sum
(
map
(
uncurry
(
*
))
as
)
Some distributions
>
bernoulli
::
Double
->
PD
Double
...
...
@@ -195,19 +209,9 @@ What is the probability of the first flip being a head given that there are at l
>
then
return
a
>
else
twoOutOfThree
>
toss3
::
PD
(
Double
,
Double
,
Double
)
>
toss3
=
PD
[((
x
,
y
,
z
),
p
)
|
x
<-
s
,
y
<-
s
,
z
<-
s
]
>
where
p
=
0.5
*
0.5
*
0.5
>
s
=
[
0.0
,
1.0
]
>
prob
::
(
a
->
Bool
)
->
PD
a
->
Double
>
prob
e
(
PD
aps
)
=
sum
[
p
|
(
a
,
p
)
<-
aps
,
e
a
==
True
]
>
cprob
::
(
a
->
Bool
)
->
(
a
->
Bool
)
->
PD
a
->
Maybe
Double
>
cprob
e1
e2
pd
=
let
pe2
=
prob
e2
pd
in
>
if
pe2
==
0.0
>
then
Nothing
>
else
Just
(
prob
(
\
a
->
e1
a
&&
e2
a
)
pd
/
pe2
)
>
fstHtwoHs1
::
Nat
->
Double
>
fstHtwoHs1
n
=
let
pd
=
PD
(
get_pd
n
twoOutOfThree
)
in
>
get_prob
1.0
pd
>
fstH
::
(
Double
,
Double
,
Double
)
->
Bool
>
fstH
(
x
,
_
,
_
)
=
x
==
1.0
...
...
@@ -215,7 +219,28 @@ What is the probability of the first flip being a head given that there are at l
>
twoHs
::
(
Double
,
Double
,
Double
)
->
Bool
>
twoHs
(
x
,
y
,
z
)
=
x
+
y
+
z
>=
2.0
>
lala
=
cprob
fstH
twoHs
toss3
>
toss3
::
RV
(
Double
,
Double
,
Double
)
>
toss3
=
do
a
<-
toss
0.5
>
b
<-
toss
0.5
>
c
<-
toss
0.5
>
return
(
a
,
b
,
c
)
>
fstHtwoHs2
::
Nat
->
Maybe
Double
>
fstHtwoHs2
n
=
let
pd
=
PD
(
get_pd
n
toss3
)
in
>
condProb
fstH
twoHs
pd
>
fstHtwoHs3
::
Nat
->
Maybe
Double
>
fstHtwoHs3
n
=
let
t1
=
get_pd
n
(
toss
0.5
)
in
>
let
pd
=
PD
[((
x
,
y
,
z
),
px
*
py
*
pz
)
|
(
x
,
px
)
<-
t1
,
(
y
,
py
)
<-
t1
,
(
z
,
pz
)
<-
t1
]
in
>
condProb
fstH
twoHs
pd
>
fstHtwoHs4
::
Maybe
Double
>
fstHtwoHs4
=
let
t1
=
pd2List
(
bernoulli
0.5
)
in
>
let
pd
=
PD
[((
x
,
y
,
z
),
px
*
py
*
pz
)
|
(
x
,
px
)
<-
t1
,
(
y
,
py
)
<-
t1
,
(
z
,
pz
)
<-
t1
]
in
>
condProb
fstH
twoHs
pd
3.2 Factor
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment