Skip to content

Instantly share code, notes, and snippets.

View jseabold's full-sized avatar

Skipper Seabold jseabold

View GitHub Profile
@jseabold
jseabold / R2array.R
Created August 21, 2011 18:29
R matrix and vector to numpy array
# This function respects the digits option
mkarray <- function(X, name) {
cat(name); cat(" = np.array(["); cat(X, sep=","); cat("])")
if (is.matrix(X)) {
i <- as.character(nrow(X))
j <- as.character(ncol(X))
cat(".reshape("); cat(i); cat(","); cat(j); cat(", order='F')")
}
cat("\n\n")
@jseabold
jseabold / mat2nparray.ado
Created August 21, 2011 18:35
Convert Stata matrices to numpy arrays
capture program drop mat2nparray
program define mat2nparray
version 11.0
syntax namelist(min=1), SAVing(str) [ Format(str) APPend REPlace ]
if "`format'"=="" local format "%16.0g"
local saving: subinstr local saving "." ".", count(local ext)
if !`ext' local saving "`saving'.py"
tempname myfile
file open `myfile' using "`saving'", write text `append' `replace'
file write `myfile' "import numpy as np" _n _n
@jseabold
jseabold / panelindex.py
Created September 8, 2011 21:21
pandas PanelIndex from MultiIndex
from pandas import MultiIndex, Factor
import numpy as np
import pandas
def _ensure_like_indices(time, panels):
n_time = len(time)
n_panel = len(panels)
u_panels = np.unique(panels) # this sorts!
u_time = np.unique(time)
if len(u_time) == n_time:
@jseabold
jseabold / send_text.py
Created October 16, 2011 15:07
context manager to time code and send text message when done
"""
Context manager or function to send text messages to your phone when a
process is done.
Edit the global variables. You might be able to find your phone e-mail
address here: http://tinywords.com/about-old/mobile/
Usage:
with SendText("long running process"):
do_something()
@jseabold
jseabold / webuse.py
Created November 28, 2011 04:25
Stata's webuse in python
import pandas
import numpy as np
def webuse(data, baseurl='http://www.stata-press.com/data/r11/'):
"""
Parameters
----------
data : str
Name of dataset to fetch.
@jseabold
jseabold / translate.py
Created December 13, 2011 18:58
Use Google Translate API from Python
# -*- coding: utf-8 -*-
"""
You need to fill in your API key from google below. Note that querying
supported languages is not implemented.
Language Code
-------- ----
Afrikaans af
Albanian sq
Arabic ar
@jseabold
jseabold / transform.py
Created December 16, 2011 20:24
Use a decorator to transform the params input to a likelihood function.
"""
Use a decorator to transform the params input to a likelihood function.
"""
from functools import wraps
from contextlib import contextmanager
import numpy as np
import inspect
def make_doc(func, wrapper):
argspec = inspect.getargspec(func)
@jseabold
jseabold / bigmat.ado
Created January 12, 2012 20:35
Put a varlist into a matrix in Stata, even if matrix is bigger than matsize
/*
Put a variable or variable list into a Stata matrix even if it's bigger than
matsize.
Usage:
If you have some variables xb1 and xb2
bigmat xb1 xb2, mat(new_mat)
@jseabold
jseabold / empirical_likelihood.py
Created March 19, 2012 12:11
Empirical Likelihood with Moment Constraints
from statsmodels.model import LikelihoodModel
import numpy as np
from numpy import asarray
from scipy import optimize
def log_star(z, eps):
"""
Owens' log* function. Provides curvature at eps.
"""
log_idx = z > eps
@jseabold
jseabold / bootstrap_numpy.sh
Created March 23, 2012 20:16
bootstrap installations that depend on numpy
#! /bin/bash
# edit this to install whatever you want that depends on numpy after you
# reinstall numpy
# edit these variables or add to them
npsrc="$HOME/src/numpy/"
spsrc="$HOME/src/scipy/"
pdsrc="$HOME/src/pandas/"
smsrc="$HOME/statsmodels/statsmodels/"