Skip to content

Instantly share code, notes, and snippets.

View BrianWeinstein's full-sized avatar

Brian Weinstein BrianWeinstein

View GitHub Profile
circ = 1; rad = circ/(2 \[Pi]); nRunners = 5;
rList[t_] := {1 t, 2 t, 4 t, 8 t, 9.6 t, 21 t, 31 t, 33 t}[[1 ;; nRunners]]
dist[d\[Theta]_, circ_] :=
N[circ/2 (TriangleWave[(d\[Theta] - \[Pi]/2)/(2 \[Pi])] + 1)/2]
minDist[runnerList_, circ_] :=
Table[
runner = runnerList[[i]];
other = DeleteCases[runnerList, runner];
Min[dist[Abs[runner - other], circ]],
ic[x_, y_] := 1 E^(-350 ((x - 1/5)^2 + ( y - 1/3)^2))
solnDir =
NDSolve[
{D[u[x, y, t], {t, 2}] == D[u[x, y, t], {x, 2}] + D[u[x, y, t], {y, 2}],
u[x, y, 0] == ic[x, y],
(D[u[x, y, t], t] /. t -> 0) == 0,
u[0, y, t] == ic[0, y],
u[1, y, t] == ic[1, y],
u[x, 0, t] == ic[x, 0],
@BrianWeinstein
BrianWeinstein / RemoveSparseTermsLarge.R
Last active October 19, 2016 16:43
remove sparse terms on a large document term matrix
# tm::removeSparseTerms attempts to remove sparse terms via slicing a sparse matrix.
# The slicing operation tries to convert the sparse matrix to a dense matrix, but this
# fails if the dense matrix has more than ((2^31) - 1) entries [i.e., if (nrow * ncol) > ((2^31) - 1)]
#
# The error message is
# In nr * nc : NAs produced by integer overflow
#
# Instead of using tm::removeSparseTerms, the following function subsets the sparse matrix directly
# and avoids converting the sparse matrix to a dense one.
bootstrap_ci <- function(data, sample_size_pct = 0.50, samples = 100, conf_level = 0.95){
# Computes a bootstrapped confidence interval
# INPUT:
# data: a numeric vector
# sample_size_pct: the percentage of the input data to be used in each bootsrapped sample
# samples: the number of samples
# conf_level: the desired confidence level
# OUTPUT:
# a bootstrapped conf_level confidence interval
is_outlier <- function(x) {
# Computes a bootstrapped confidence interval
# INPUT:
# x: a numeric vector
# OUTPUT:
# a boolean vector, indicating if each value in x is an outlier
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}