Completed exercise 1.

Added `zbuild` manifest to build the diagrams and images.
This commit is contained in:
Filipe Rodrigues 2023-12-03 12:21:33 +00:00
parent 80dbb730d1
commit efd3fc1ed2
7 changed files with 186 additions and 1 deletions

6
.gitignore vendored
View File

@ -1,3 +1,7 @@
# Ignore most `pdf`s
# Auto-generated
*.pdf
images/*.svg
diagrams/*.svg
# Exceptions
!guide.pdf

10
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
"cSpell.words": [
"byrow",
"DTMC",
"ggplot",
"ggsave",
"Tsvg",
"typst"
]
}

59
code/1.R Normal file
View File

@ -0,0 +1,59 @@
library(ggplot2)
library(gridExtra)
simulate <- function(alpha, beta, output_file) {
# 2-DTMC transition matrix
p <- matrix(
c(
c(alpha, 1 - alpha),
c(beta, 1 - beta)
),
nrow = 2,
ncol = 2,
byrow = TRUE
)
# DTMC mean
p_avg <- p[1, 2] / (1 + p[2, 1] - p[1, 1])
points_len <- 200
# 2-DTMC simulation
points1 <- c()
current_state <- 0
for (i in 1:points_len) {
# Extracts probability of transition to same state
prob <- p[current_state + 1, current_state + 1]
# Changes state if no success
if (rbinom(1, 1, prob) == 0) {
current_state <- (current_state + 1) %% 2
}
points1[length(points1) + 1] <- current_state
}
# Bernoulli process simulation
points2 <- lapply(1:points_len, function(...) rbinom(1, 1, p_avg))
points2 <- unlist(points2)
# Organize into data frames
data1 <- data.frame(x = seq_along(points1), y = points1)
data2 <- data.frame(x = seq_along(points2), y = points2)
p1 <- ggplot(data1) +
geom_point(aes(.data$x, .data$y)) +
scale_y_continuous("2-DTMC")
p2 <- ggplot(data2) +
geom_point(aes(.data$x, .data$y)) +
scale_y_continuous("Bernoulli")
p3 <- grid.arrange(p1, p2, ncol = 1)
ggsave(p3, file = output_file, device = "svg")
}
set.seed(0)
pdf(NULL)
simulate(0.1, 0.1, "images/1a (α=0.1, β=0.1).svg")
simulate(0.5, 0.5, "images/1a (α=0.5, β=0.5).svg")
simulate(0.9, 0.9, "images/1a (α=0.9, β=0.9).svg")
simulate(0.9, 0.1, "images/1b (α=0.9, β=0.1).svg")

16
diagrams/1.dot Normal file
View File

@ -0,0 +1,16 @@
digraph {
rankdir = "LR";
State0 [label = "0";];
State1 [label = "1";];
State0 -> State0 [label = "α";];
State0 -> State1 [label = "1-α";];
# TODO: Better solution for this?
State0 -> State1 [style = "invis";];
State0 -> State1 [style = "invis";];
State1 -> State0 [label = "β";];
State1 -> State1 [label = "1-β";];
}

47
exercises/1.typ Normal file
View File

@ -0,0 +1,47 @@
#import "/util.typ" as util: indent_par
#indent_par[The 2-DTMC process is capable of performing both what the Bernoulli process can, as well as another interesting behavior]
#figure(
image("/diagrams/1.svg", width: 50%),
caption: [2-DTMC process]
)
==== a. Interesting behavior
#indent_par[When the α and β parameters are on opposite sides of the spectrum, the 2-DTMC process exhibits an interesting behavior:]
#figure(
image("/images/1b (α=0.9, β=0.1).svg", width: 50%),
caption: [2-DTMC and Bernoulli processes (α=0.9, β=0.1)]
)
#indent_par[Unlike the Bernoulli process, the 2-DTMC "remembers" it's previous state, ensuring that both states are very stable, not wanting to transition to the other side.]
#pagebreak()
==== b. Bernoulli-like behavior
#indent_par[When the α and β parameters are equal, the 2-DTMC process performs almost exactly as the Bernoulli process:]
#grid(
columns: (1fr, 1fr, 1fr),
figure(
image("/images/1a (α=0.1, β=0.1).svg", width: 80%),
caption: [2-DTMC and Bernoulli processes (α=0.1, β=0.1)]
),
figure(
image("/images/1a (α=0.5, β=0.5).svg", width: 80%),
caption: [2-DTMC and Bernoulli processes (α=0.5, β=0.5)]
),
figure(
image("/images/1a (α=0.9, β=0.9).svg", width: 80%),
caption: [2-DTMC and Bernoulli processes (α=0.9, β=0.9)]
)
)
#indent_par[When α and β are close to 0.0 or 1.0, one of the states will become very stable while the other state will become very unstable, quickly wanting to transition to the other state.]
#indent_par[When α and β are close to 0.5, both states are very unstable.]
#indent_par[The existence of one or more unstable states imply that the system can no longer as easily "remember" it's previous state and thus the probability of finding the system in a given state can now be approximated by a bernoulli process]

View File

@ -36,3 +36,8 @@
#pagebreak()
= Exercises
== A. Discrete-time Markov chains
=== 1. Exercise 1
#include "exercises/1.typ"

44
zbuild.yaml Normal file
View File

@ -0,0 +1,44 @@
---
default: [diagrams/1.svg, rule: ex1]
rules:
# Typst
typst:
alias:
input: ^(file).typ
output: ^(file).pdf
out: [$(output)]
deps: [$(input)]
exec:
- - typst
- compile
- --root=.
- $(input)
- $(output)
# Diagram
diagram:
alias:
input: diagrams/^(file).dot
output: diagrams/^(file).svg
out: [$(output)]
deps: [$(input)]
exec:
- - dot
- -Tsvg
- $(input)
- -o
- $(output)
# Exercise 1
ex1:
out:
- images/1a (α=0.1, β=0.1).svg
- images/1b (α=0.9, β=0.1).svg
- images/1a (α=0.5, β=0.5).svg
- images/1a (α=0.9, β=0.9).svg
deps:
- code/1.R
exec:
- - Rscript
- code/1.R