Skip to content

Instantly share code, notes, and snippets.

@notbanker
notbanker / compositional
Created July 3, 2013 02:42
Composition of probability models as per Jirousek.
object Compositional {
type FunctionOfList[T1] = List[T1] => Option[Double]
def convertFunctionOfListToFunctionInf[T1](f: FunctionOfList[T1], support: Set[Integer]): (Stream[T1] => Option[Double]) = {
(xs: Stream[T1]) =>
{
val variablesThatMatter = support.toList.map(i => xs(i))
val fx = f(variablesThatMatter)
fx
@notbanker
notbanker / gist:afb4301ac3007977c290
Created August 13, 2014 17:59
ISDA Legal Entity Identifier (LEI) hash. Takes 20 digit LEI to a 10 digit Unique Trade Identifier (UTI) prefix
ISDA_PERMITTED_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ISDA_PRIME = 59575553
def leiHash( lei, permittedChars = ISDA_PERMITTED_CHARS ):
""" Shorten 20 character alpha-numeric Legal Entity Identifier into 10 character alpha-numeric "short LEI" that
can be used as a Unique Trade Identifier (UTI) prefix. The algorithm is simply the modulo operation lifted from
integers to the space of alpha-numeric case-insensitive strings.
Argument
--------
@notbanker
notbanker / shortlei.py
Last active August 29, 2015 14:05
ISDA Legal Entity Identifier (LEI) hash. Takes 20 digit LEI to a 10 digit Unique Trade Identifier (UTI) prefix
import math
import func
# These coincide with the choices hardwired into the spreadsheet provided to ISDA, so don't change them
ISDA_PERMITTED_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' # Don't change!
ISDA_PRIME = 59575553
def _avoidReserved( s ):
""" Avoid clash with reserved namespace used by CFTC """
@notbanker
notbanker / gist:1375bca2e5283c5a4d1f
Created August 29, 2014 20:39
MCMC for twin diagnosis
from pymc import Model, MCMC, deterministic, stochastic, Normal, Bernoulli, Beta, distributions
import numpy as np
def twinModelVars( p_identical = 0.5, p_down = 0.01, measurement = 2.5, measurementPrecision = 2.0 ):
""" Stylized model for twin Down Syndrome likelihood after a blood sample is taken from mother
containing a mixture of indicators from each foetus
:param p_identical: (Prior) probability of identical twins versus fraternal (0.1 for population)
:param p_down: (Prior) probability of Down Syndrome
:param measurement: Measured marker value
@notbanker
notbanker / twinsmodel.py
Created August 29, 2014 20:39
MCMC for twin diagnosis
from pymc import Model, MCMC, deterministic, stochastic, Normal, Bernoulli, Beta, distributions
import numpy as np
def twinModelVars( p_identical = 0.5, p_down = 0.01, measurement = 2.5, measurementPrecision = 2.0 ):
""" Stylized model for twin Down Syndrome likelihood after a blood sample is taken from mother
containing a mixture of indicators from each foetus
:param p_identical: (Prior) probability of identical twins versus fraternal (0.1 for population)
:param p_down: (Prior) probability of Down Syndrome
:param measurement: Measured marker value
@notbanker
notbanker / app.py
Created August 30, 2014 02:38
Bayesian calculation for twin Down Syndrome probabilities (Flask app)
from flask import Flask, request, url_for
from scipy.optimize import bisect
import math
import numpy as np
app = Flask(__name__)
app.secret_key = 'Whatever'
@app.route('/')
def index():
@notbanker
notbanker / lambert.py
Last active July 6, 2019 19:54
Amazing facts about Lambert
lambert_facts = ['Lambert was born the son of a tailor, and was expected by his father to continue in that profession.',
'Lamberts early fight for his education is a remarkable story---he had to sell drawings and writings to his classmates to buy candles for night study,',
'Lambert produced fundamental discoveries in cartography, despite being self-educated.', 'Lambert influenced hygrometry, pyrometry, pure mathematics and statistics.',
'Lambert was more famous as a philosopher than as a mathematician.', 'The Lambert projection is still in use in cartography.',
'Lambert was the first person to prove that pi is irrational.', 'Proofs that it is impossible to square the circle leaned on insights from Lambert.',
'Lambert found a series solution to the trinomial equation, by hand calculation.', 'Lamberts technique for solving general series reversion techniques was a forerunner to the Langrange Inversion Theorem.',
'Lambert discovered an
@notbanker
notbanker / stockfish.sh
Last active November 24, 2019 16:03
Use stockfish engine to output the position evaluation only
#!/usr/bin/env bash
# Call stockfish engine on mac and return only the evaluation score
# Usage stockfish.sh 'r1b2rk1/4qppp/2pp4/pp6/3Bn3/PB3Q1P/1PP2PP1/3R1RK1' 5 mac 12 1024
# Usage stockfish.sh 'r1b2rk1/4qppp/2pp4/pp6/3Bn3/PB3Q1P/1PP2PP1/3R1RK1' 5 mac 12 1024
# Assumes the stockfish binary is called 'stockfish_'+binary
fen=$1
seconds=${2:-3}
binary=${3:-mac}
threads=${4:-12}
@notbanker
notbanker / lichess.py
Created March 21, 2016 21:31
Get some random middlegame positions from actual games on www.lichess.org
import json
import urllib2
import time
import pandas as pd
def fen_examples( max_users = 3, max_games_per_user=10, sleep_time = 15 ):
""" Grab some real game positions from the middle of actual games, and the result """
users = users_in_team( team='coders' )
records = list()
for user in users[ :max_users ]:
@notbanker
notbanker / stockfish.py
Created March 23, 2016 15:49
Python call to stockfish chess engine
import subprocess
from lib.common.dominoUtil import running_on_local
from lib.config import PROJECT_PATH
import time
import psutil
import os
import numpy as np
def _recommended_threads():
return psutil.cpu_count()-2