Skip to content

Instantly share code, notes, and snippets.

Matthew Pearce pearcemc

View GitHub Profile
View example_server.jl
# context: http://stackoverflow.com/questions/39448808/julia-tcp-server-and-connection
# Use fn to process messages from sock.
# Loop till sock is open and fn returns true.
function processor(fn, sock)
proc = true
try
while proc && ((nb_available(sock) > 0) || isopen(sock))
proc = fn(sock)
end
@pearcemc
pearcemc / simgamma
Created May 21, 2015
Correct shape rate parameterisation of gamma and inverse gamma with scipy.stats
View simgamma
from pylab import *
import scipy.stats as stats
########################################################################################
#
# QUICK ILLUSTRATION OF USING SCIPY.STATS FOR GAMMA AND INV-GAMMA
# HOW TO DO SHAPE-RATE (alpha, beta) PARAMETERISATION CORRECTLY
#
########################################################################################
@pearcemc
pearcemc / blockimage
Created Apr 28, 2015
Plotting functions for multidimensional image arrays.
View blockimage
from pylab import *
import numpy
def grid_sq(fr,ncol,bs,npx=2,bval=0):
Z = fr.shape[0]
z=0
vbar = bval*ones((bs[0],npx))
hbar = bval*ones((npx, (bs[1] + npx)*ncol + npx))
grid = [hbar]
row = [vbar]
@pearcemc
pearcemc / APTS-modelling1-qc
Created Mar 24, 2015
APTS Southampton - Statistical Modelling - script for 1c
View APTS-modelling1-qc
plot.aic <- function(fit, new=T, sd=0.1)
{ # code to plot AIC against order of AR model fitted
if (new) plot((1:length(fit$aic))-1,fit$aic,type="l",xlab="Order",ylab="AIC") else
lines((1:length(fit$aic))-1,fit$aic,type="l")
points(rnorm(1,fit$order,sd=sd),rnorm(1,sd=sd),pch=16,col="red")
}
plot.stat_ic <- function(stat, new=T, sd=0.1)
{ # code to plot AIC against order of AR model fitted
if (new) plot((1:length(stat))-1,stat,type="l",xlab="Order",ylab="AIC") else
@pearcemc
pearcemc / ibp_vis
Last active Aug 29, 2015
Visualise the two parameter Indian Buffet Process
View ibp_vis
import sys
import time
from pylab import *
import scipy.stats as stats
"""
Script to visualise draws from a two parameter Indian Buffet Process.
usage example:
@pearcemc
pearcemc / binomial-mixture-EM.R
Last active Sep 5, 2017
EM algorithm for a binomial mixture model (arbitrary number of mixture components, counts etc).
View binomial-mixture-EM.R
##################################################################
#
# APPLYING EM TO A BINOMIAL MIXTURE MODEL
# matthew@refute.me.uk
# Licence: MIT Licence (http://opensource.org/licenses/MIT)
#
##################################################################
###### EM ALGORITHM FUNCTIONS
@pearcemc
pearcemc / contour.py
Created Nov 9, 2010
Contour map of own function with matplotlib
View contour.py
import numpy as np
import matplotlib.pyplot as plt
from math import exp, sqrt, pi
#the function we're going to map
def gausspdf( x, mu, sd2 ):
return exp( -1*( (x - mu)**2/(2*sd2)) )/sqrt( 2*pi*sd2 )
#canvas size
x = y = np.arange( 0, 10, 0.1 )
View Knearest
class KNearest:
"""k-nearest neighbour inferer"""
def __init__(self, ds):
#set the dataset
self.ds = ds
def predict(self, p1, k=1):
"""Given a test point p1, return the modal class of its knearest neighbours"""
distances = []
#calculate the distance between the test point and known data points.
View TestPredictor
class TestPredictor:
"""Iterate a KNearest predictor and return its success rate."""
def __init__(self, predictor, times=1000):
#set the KNearest predictor
self.predictor = predictor
#set number of iterations
self.times = times
def test(self, k=1):
View PDimClass
import scipy as sp
class PDimClass:
"""
A p-dimensional class, which takes a list of Normal distribution parameters,
each of which defines a dimension
"""
#a list of normal distribution parameter tuples, for N(mean, sd)
params = [(0,1), (0,1)]
You can’t perform that action at this time.