Skip to content

Instantly share code, notes, and snippets.

View baogorek's full-sized avatar

Ben Ogorek baogorek

  • spencer Health Solutions, Inc.
  • Raleigh, NC
  • X @benogorek
View GitHub Profile
@baogorek
baogorek / combined-impulse-response-ff.R
Last active March 5, 2019 14:15
Code Block 2. Comparing the true theoretical impulse response with B-spline approximation.
library(splines)
exp_decay <- function(t, tau) {
exp(-t / tau)
}
get_true_phi <- function(t) {
0.07 * exp_decay(t, 60) - 0.27 * exp_decay(t, 13)
}
days_grid <- 1:259
@baogorek
baogorek / spline-convolution-estimation.R
Last active March 5, 2019 14:15
Code Block 3: Introducing a spline-based method for modeling cumulative impact.
library(splines)
my_spline <- ns(1:259, Boundary.knots = c(1, 259), knots = c(14, 40, 100))
z_vars <- list()
for (n in 1:nrow(train_df)) {
spline_pred <- predict(my_spline, newx = (n - 1):1)
spline_vars <- colSums(spline_pred * train_df$w[1:(n - 1)]) # convolution
spline_const <- sum(train_df$w[1:(n - 1)])
z_vars[[n]] <- c(spline_const, spline_vars)
}
@baogorek
baogorek / cumulative-impact-part-ii-close-the-loop.R
Last active March 5, 2019 14:17
Code Block 4. Closing the loop.
get_eta_hat <- function(t_seq) {
spline_vars_grid <- predict(my_spline, newx = t_seq)
spline_vars_grid <- cbind(1, spline_vars_grid)
eta_hat <- spline_vars_grid %*% coef(spline_reg)[-1]
as.numeric(eta_hat)
}
convolve_with_fn <- function(training, n, impulse_fn) {
sum(training[1:(n - 1)] * impulse_fn((n - 1):1))
}
library(ggplot2)
# Figure 1
plot_df <- rbind(
data.frame(day = days_grid, level = phi, type = "true \u03D5(t)"),
data.frame(day = days_grid, level = eta_star, type = "best fit \u03B7*(t)")
)
ggplot(plot_df, aes(x = day, y = level, color = type, linetype = type)) +
geom_line(size = 2) +
get_perf_params <- function(world_record_perf, able_bodied_perf, limit,
type = "running") {
# sum of squares around fixed point
rss <- function(a, b, world_record_perf, able_bodied_perf, limit) {
(1000 - b * log(a / (world_record_perf - limit))) ** 2 +
(0 - b * log(a / (able_bodied_perf - limit))) ** 2
}
stopifnot(type %in% c("running", "jumping"))
a_starting <- ifelse(type == "running", 5, -5)
library(ggplot2)
# Files are at ...
folder_path <- "c:/devl/swimming" # Update accordingly
perf_params <- get_perf_params(world_record_perf = 1.8,
able_bodied_perf = 0.5, limit = 2.0,
type = "jumping")
get_perf_in_cp <- function(perf, a, b, limit) {
library(dplyr)
library(gridExtra)
library(ggplot2)
exp_decay <- function(t, tau) {
exp(-t / tau)
}
convolve_training<- function(training, n, tau) {
sum(training[1:(n - 1)] * exp_decay((n - 1):1, tau))
library(dplyr)
library(ggplot2)
library(splines)
days_grid <- 0:180
interior_knots <- c(2, 6, 25)
#interior_knots <- c(6, 25, 80)
my_spline <- ns(days_grid, knots = interior_knots)
@baogorek
baogorek / kalman-derivations.ipynb
Last active February 3, 2023 04:29
Gist version of Miscellaneous/methods/kalman-filter/kalman-derivations.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@baogorek
baogorek / non-invertible-ma-process.R
Created June 25, 2019 20:51
non-invertible-ma-process.R
N <- 10000
theta <- 1.4
e <- rnorm(N)
e_lag <- c(NA, e[1:(N-1)])
y <- e - theta * e_lag
acf(y[2:N])