Finished exercise 4.

Made all matrices have delimiters `[]`.

Co-authored-by: Ricardo-Rodrigues <R1cardu7@users.noreply.github.com>
This commit is contained in:
2023-12-04 17:14:13 +00:00
parent 3642e545bd
commit 483fb34788
8 changed files with 144 additions and 21 deletions

View File

@@ -30,4 +30,6 @@ while (steps < 100) {
cat(sprintf("Took %d steps\n", steps))
cat(sprintf("Final matrix:\n"))
step_matrix <- apply(step_matrix, 1, function(cell) round(cell, 4))
print(step_matrix)
write.table(step_matrix, "output/4-matrix.csv", sep = "\t", col.names = FALSE, row.names = FALSE)

View File

@@ -25,8 +25,7 @@ for (round_idx in 1:rounds) {
cur_state <- next_state
}
for (state in 1:3) {
state_count <- length(states[states == state])
state_prob <- state_count / rounds
cat(sprintf("State %d prob: %f\n", state, state_prob))
}
probs <- sapply(1:3, function(state) length(states[states == state]) / length(states))
probs <- sapply(probs, function(x) round(x, 4))
print(probs)
write.table(probs, "output/4-sim.csv", sep = "\t", col.names = FALSE, row.names = FALSE)

View File

@@ -11,16 +11,7 @@ prob_matrix <- matrix(
)
# Build the coefficient matrix and constants
coef_matrix <- matrix(
c(
c(-prob_matrix[1, 2] - prob_matrix[1, 3], prob_matrix[2, 1], prob_matrix[3, 1]),
c(prob_matrix[1, 2], -prob_matrix[2, 1] - prob_matrix[2, 3], prob_matrix[3, 2]),
c(prob_matrix[1, 3], prob_matrix[2, 3], -prob_matrix[3, 1] - prob_matrix[3, 2])
),
nrow = 3,
ncol = 3,
byrow = TRUE
)
coef_matrix <- t(prob_matrix) - diag(rowSums(prob_matrix))
consts <- c(0, 0, 0)
# Substitute one of the rows by the final `pi0 + pi1 + pi2 = 1` equation.
@@ -29,4 +20,6 @@ coef_matrix[subst_idx, ] <- c(1, 1, 1)
consts[subst_idx] <- 1
solution <- solve(coef_matrix, consts)
solution <- sapply(solution, function(x) round(x, 4))
print(solution)
write.table(solution, "output/4-solve.csv", sep = "\t", col.names = FALSE, row.names = FALSE)