Skip to content

Instantly share code, notes, and snippets.

Aaron Schlegel aschleg

Block or report user

Report or block aschleg

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@aschleg
aschleg / austin_animal_center_dataset_analysis_functions.py
Created Mar 29, 2018
Functions used in series of analyses on the Austin Animal Center's animal intakes and outcomes datasets.
View austin_animal_center_dataset_analysis_functions.py
import requests
import numpy as np
import pandas as pd
from six.moves.urllib.error import HTTPError
def get_soda_api_data(endpoint, count=1000, offset=0, return_df=True):
params = {'$limit': count, '$offset': offset}
results = []
@aschleg
aschleg / download_soda_api_data.py
Last active Mar 12, 2018
Simple script for extracting Socrata Open Data Access (SODA) datasets.
View download_soda_api_data.py
"""
Simple script for extracting Socrata Open Data Access (SODA) datasets. Compatible with 3+, though one can easily make it 2.7
compatible by changing the `from urllib.error import HTTPError` import to `from urllib2 import HTTPError`
Parameters
----------
endpoint : string
SODA API endpoint of the dataset.
count : int, default 1000
@aschleg
aschleg / lagrangian_interpolation.R
Created Jun 24, 2017
R function for performing Lagrangian polynomial interpolation
View lagrangian_interpolation.R
# Function for performing Lagrangian polynomial interpolation https://en.wikipedia.org/wiki/Lagrange_polynomial.
# Requires the package rSymPy https://cran.r-project.org/web/packages/rSymPy/index.html.
# Parameters:
# x: x values of interpolating points
# y: values corresponding to x values
# Returns:
# Simplified interpolated polynomial that passes through the given x and y points
lagrange.poly <- function(x, y) {
@aschleg
aschleg / nevilles_algorithm.R
Created Jun 24, 2017
R function for performing Neville's Algorithm for approximating an interpolated polynomial at a given point
View nevilles_algorithm.R
# Performs Neville's Algorithm (https://en.wikipedia.org/wiki/Neville%27s_algorithm) for
# interpolating a polynomial given a set of x and y values.
# Parameters:
# x: x values of interpolating points
# y: values corresponding to x values
# x0: desired point to approximate interpolated polynomial
# Returns:
# List containing approximated value of interpolated polynomial and matrix representing
# the intermediate values of Q
@aschleg
aschleg / matrix_trace.R
Created Oct 7, 2016
Simple function for computing the trace of a square matrix. Should replicate the output of sum(diag(matrix)).
View matrix_trace.R
# Simple function for computing the trace of a matrix. Should replicate the output of sum(diag(matrix)).
# Function requires a square n x n matrix as input.
# Used in post demonstrating the trace of a matrix here: http://wp.me/p4aZEo-5Nb
trace <- function(A) {
n <- dim(A)[1] # get dimension of matrix
# Check that the matrix is square
if (n != dim(A)[2]) {
@aschleg
aschleg / bisection_method.R
Created Sep 23, 2016
Sample function for evaluating a function root using the bisection method.
View bisection_method.R
# Sample function for evaluating a function root using the bisection method
# Inputs required are a function and the starting points to calculate the midpoint
# Function used in post demonstrating the bisection method of root-finding here: http://wp.me/p4aZEo-5MU
bisection <- function(f, a, b, n = 1000, tol = 1e-7) {
# If the signs of the function at the evaluated points, a and b, stop the function and return message.
if (!(f(a) < 0) && (f(b) > 0)) {
stop('signs of f(a) and f(b) differ')
} else if ((f(a) > 0) && (f(b) < 0)) {
stop('signs of f(a) and f(b) differ')
@aschleg
aschleg / secant_method.R
Created Sep 7, 2016
Simple function for performing the secant method of root finding
View secant_method.R
# Simple function for performing the secant method of root finding.
# Function takes three arguments:
# f: the function in question
# x0: first starting value
# x1: second starting value
# Function used in post on demonstrating the secant method here: http://wp.me/p4aZEo-5MH
secant.method <- function(f, x0, x1, tol = 1e-9, n = 500) {
if (is.function(f) == FALSE) {
stop('f is not a function')
View newton_raphson.R
# Sample function demonstrating how the Newton-Raphson method for root-finding is performed.
# Requires the numDeriv package for finding the derivative. One could replace this with the limit using a
# small enough h value.
# Requires three inputs:
# f: univariate function
# a: lower bound (also represents starting value for method)
# b: upper bound
# Newton-Raphson method for finding roots was examined in post here: http://wp.me/p4aZEo-5My
newton.raphson <- function(f, a, b, tol = 1e-5, n = 1000) {
@aschleg
aschleg / working_hotelling_bonferroni_intervals.R
Last active Aug 15, 2016
Example function for calculating Working-Hotelling and Bonferroni confidence intervals at a 95% confidence level
View working_hotelling_bonferroni_intervals.R
# Example function for calculating Working-Hotelling and Bonferroni confidence intervals at a 95% level.
# Function takes two arguments:
# x: predictor variable. Must be a vector.
# y: response variable. Must be a vector.
# Plots are built using ggplot2
# gridExtra package, https://cran.r-project.org/web/packages/gridExtra/index.html, is used to plot multiple ggplots
# Function used to demonstrate how to construct simultaneous confidence intervals in post here: http://wp.me/p4aZEo-5Mg
working.hotelling.bonferroni.intervals <- function(x, y) {
@aschleg
aschleg / matrix_inverse.R
Created Aug 10, 2016
Sample R function demonstrating how a 2x2 or 3x3 matrix inverse is computed.
View matrix_inverse.R
# Sample function demonstrating how a 2x2 or 3x3 matrix inverse is computed.
# Requires an object with no more than 3 columns and an equal number of rows that can be coerced into a matrix.
# Used in post on computing the inverse of a matrix if one exists: http://wp.me/p4aZEo-5Ln
matrix.inverse <- function(mat) {
A <- as.matrix(mat)
# If there are more than four columns in the supplied matrix, stop routine
if ((ncol(A) >= 4) | (nrow(A) >= 4)) {
You can’t perform that action at this time.