Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjamin Ogorek baogorek

View GitHub Profile
View sleepstudy.py
import pandas as pd
import numpy as np
import patsy
import tensorflow as tf
class SleepReg(tf.Module):
def __init__(self, sleepdata_path):
"""Initializing tensorflow variables and other necessary matrices"""
# These two TensorFlow variables show up in the trainable_variables
@baogorek
baogorek / non-invertible-ma-process.R
Created Jun 25, 2019
non-invertible-ma-process.R
View 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])
@baogorek
baogorek / kalman-derivations.ipynb
Last active Sep 11, 2019
Gist version of Miscellaneous/methods/kalman-filter/kalman-derivations.ipynb
View kalman-derivations.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View Part-III-code-block-4.R
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)
View part-III-code-block-3.R
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))
View Part-III-code-block-2.R
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) {
View get-performance-parameters.R
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)
View cumulative-impact-part-ii-graphics.R
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) +
View cumulative-impact-part-ii-close-the-loop.R
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))
}
@baogorek
baogorek / spline-convolution-estimation.R
Last active Mar 5, 2019
Code Block 3: Introducing a spline-based method for modeling cumulative impact.
View spline-convolution-estimation.R
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)
}
You can’t perform that action at this time.