Skip to content

Instantly share code, notes, and snippets.

View choffstein's full-sized avatar

Corey M. Hoffstein choffstein

View GitHub Profile
@choffstein
choffstein / entropy_pooling.py
Last active February 3, 2024 21:09
Attilio Meucci's Entropy Pooling
import numpy
import scipy.optimize
import pandas
def probability_constraint(x):
j, n = x.shape
Aeq = numpy.ones([1, j])
beq = numpy.array([1.])
import multiprocessing
import numpy
import functools
import tqdm
def _escapable_child(f, args):
# help prevent any forking issues with seeding the RNG
numpy.random.seed()
try:
convert -density 150 input.pdf -colorspace gray -linear-stretch 3.5%x10% -blur 0x0.5 -attenuate 0.25 +noise Gaussian -rotate 0.5 temp.pdf
gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite -sColorConversionStrategy=LeaveColorUnchanged dAutoFilterColorImages=true -dAutoFilterGrayImages=true -dDownsampleMonoImages=true -dDownsampleGrayImages=true -dDownsampleColorImages=true -sOutputFile=output.pdf temp.pdf
import pandas
import numpy
import scipy.optimize
# based upon rules set out at https://allocatesmartly.com/adam-butler-gestaltu-adaptive-asset-allocation/
tickers = ['spy', 'ezu', 'ewj', 'eem', 'vnq', 'rwx', 'ief', 'tlt', 'dbc', 'gld']
# assumes tickers are .csv files in the same folder as the code
# with a column named 'adjusted_close' with daily adjusted closing prices
data = {}
@choffstein
choffstein / gist:3380026
Created August 17, 2012 15:46
Fetch historical VIX data
import urllib2
months = ["F","G","H","J","K","M","N","Q","U","V","X","Z"]
years = ["04","05","06","07","08","09","10","11","12"]
for year in years:
for month in months:
filename = "CFE_" + month + year + "_VX.csv"
url = "http://cfe.cboe.com/Publish/ScheduledTask/MktData/datahouse/" + filename
try:
@choffstein
choffstein / dbscan.py
Created July 4, 2012 00:50
A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise
# A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise
# Martin Ester, Hans-Peter Kriegel, Jörg Sander, Xiaowei Xu
# dbscan: density based spatial clustering of applications with noise
import numpy as np
import math
UNCLASSIFIED = False
NOISE = None
@choffstein
choffstein / gist:1943113
Created February 29, 2012 18:12
GZIP A string
;; zip a string
(def s "aslgajskghkajsghkjasgjhajsghjags")
(def ostream (java.io.ByteArrayOutputStream.))
(def gzip_ostream (java.util.zip.GZIPOutputStream. ostream))
(clojure.java.io/copy s gzip_ostream)
(.close gzip_ostream)
(def bytes (java.io.ByteArrayInputStream. (.toByteArray ostream)))
(def gzip_istream (java.util.zip.GZIPInputStream. bytes))