Skip to content

Instantly share code, notes, and snippets.

View mike-lawrence's full-sized avatar

Mike Lawrence mike-lawrence

  • Halifax, Nova Scotia, Canada
View GitHub Profile
@mike-lawrence
mike-lawrence / holo_fail.r
Created December 22, 2019 16:13
Failed attempt at using holographic database for determining entry presence/absense
#define a function to generate a random vector
makeNewVector = function(vectorLength){
#random normal
x = rnorm(vectorLength)
#normalize to length 1
x/(sqrt(sum(x^2)))
}
#define a function to compute cosine similarity between two vectors
cosine <- function( x, y) {
@mike-lawrence
mike-lawrence / pulse.Rproj
Last active December 28, 2017 17:17
Inference on amplitude & noise in simple periodic pulse model Raw
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: No
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
@mike-lawrence
mike-lawrence / gp_regression.stan
Last active April 7, 2020 18:15
GP Regression example
functions{
// GP: computes noiseless Gaussian Process
vector GP(real volatility, real amplitude, vector normal01, int n_x, real[] x ) {
matrix[n_x,n_x] cov_mat ;
real amplitude_sq_plus_jitter ;
amplitude_sq_plus_jitter = amplitude^2 + 1e-6 ;
cov_mat = cov_exp_quad(x, amplitude, 1/volatility) ;
for(i in 1:n_x){
cov_mat[i,i] = amplitude_sq_plus_jitter ;
}
@mike-lawrence
mike-lawrence / read_incrementally.R
Last active May 12, 2017 17:30
Read a file incrementally in R, when that file is being occasionally written to by another process
fname = 'temp.txt'
#wait for it to exist
while(!file.exists(fname)){}
old = 0
new = old
while(T){
while(new==old){
new = file.size(fname)
}
f = file(
@mike-lawrence
mike-lawrence / find_last_line.R
Created May 11, 2017 17:35
Find the last line of a big text file in R
f = file(description='bigFile.txt',open='rb')
n = 0
temp = 1
while(length(temp)<2){
n = n + 1
seek(con=f,origin='end',where=-(2^n))
temp <- scan(f,what='character',quiet=T)
}
last_line = temp[2]
@mike-lawrence
mike-lawrence / gp_regression.stan
Last active May 4, 2017 13:24
gp_regression_example
functions{
# GP: computes noiseless Gaussian Process given pre-computed unique distances
vector GP(real volatility, real amplitude, vector normal01, int n_x, int n_dx, vector dx_unique, int [,] dx_index) {
# covars: unique entries in covariance matrix
vector[n_dx] covars ;
# covMat: covariance matrix
matrix[n_x,n_x] covMat ;
@mike-lawrence
mike-lawrence / crf_demo.R
Created April 18, 2017 16:12
Canonical response function analysis in Stan
library(tidyverse)
library(rstan)
rstan_options(auto_write = TRUE)
curve(dweibull(x,shape=1.75,scale=1e3),from=0,to=3e3)
one_response = dweibull(x=1:3e3,shape=1.75,scale=1e3)
one_response = one_response/max(one_response)
# create a signal with pulses at t=2e3, t=4e3 & t=5e3
obs = rep(0,8e3)
@mike-lawrence
mike-lawrence / bigStan.R
Last active April 19, 2017 19:08
Functions facilitating running Stan models on many-cpu systems
# Code below may not be up to date, see ezStan (https://github.com/mike-lawrence/ezStan/blob/master/R/bigStan.R) for latest version
# todo:
# during-sampling: effective sample size and rhat for each parameter
# during-sampling: diagnostics?
#usage:
# #compile the model using rstan::stan_model:
@mike-lawrence
mike-lawrence / cfa.stan
Created March 21, 2017 15:10
Demo using Stan for Confirmatory Factor Analysis
data{
# n_subj: number of subjects
int n_subj ;
# n_y: number of outcomes
int n_y ;
# y: matrix of outcomes
matrix[n_subj,n_y] y ;
# n_fac: number of latent factors
int n_fac ;
# y_fac: list of which factor is associated with each outcome
@mike-lawrence
mike-lawrence / compare_ceq_dx_udx.R
Last active March 12, 2017 16:09
Comparing cov_exp_quad to alternative gp optimizations
#load packages
library(tidyverse)
library(rstan)
rstan_options(auto_write = TRUE)
# Make some fake data ----
#set random seed for reproducibility
set.seed(1)