Skip to content

Instantly share code, notes, and snippets.

@samclifford
Last active January 13, 2018 00:09
Show Gist options
  • Save samclifford/5ba3eff6d93ae147e9a9f98cc28d9164 to your computer and use it in GitHub Desktop.
Save samclifford/5ba3eff6d93ae147e9a9f98cc28d9164 to your computer and use it in GitHub Desktop.
Discussing how often a string with two cuts will form a triangle
n_sims <- 1e5
breaking_convention <- "all_at_once" # or stick_breaking
if (breaking_convention == "stick_breaking"){
break_locations <- matrix(runif(n = n_sims), ncol=1)
sorted_break_locations <- cbind(break_locations, matrix(runif(n = n_sims, min = break_locations, max=1), ncol=1))
} else {
break_locations <- matrix(runif(n = 2*n_sims), ncol=2)
sorted_break_locations <- t(apply(break_locations, 1, sort))
}
segment_lengths <- cbind(sorted_break_locations[,1],
sorted_break_locations[,2] - sorted_break_locations[,1],
1 - sorted_break_locations[,2])
sorted_segment_lengths <- t(apply(segment_lengths, 1, sort))
head(sorted_segment_lengths)
is_triangle <- function(x){
as.logical(x[3] < (x[2] + x[1]))
}
triangles <- apply(sorted_segment_lengths, 1, is_triangle)
binom.test(x = rev(table(triangles)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment