Skip to content

Instantly share code, notes, and snippets.

View drbenvincent's full-sized avatar

Benjamin T. Vincent drbenvincent

View GitHub Profile
@drbenvincent
drbenvincent / rejectionSamplingDemo.m
Last active May 26, 2018 11:41
rejection sampling demo - Matlab
%% true probability distribution
true_func = @(x) betapdf(x,1+1,1+10);
%% Do rejection sampling
% create many samples on interval 0-1
x_samples = rand(10^6,1);
% evaluate for each sample
sample_value = true_func(x_samples);
% accept in proportion to highest
max_value = max(sample_value);
@drbenvincent
drbenvincent / rejectionSamplingDemo2.m
Last active December 10, 2015 15:24
rejection sampling demo - Matlab - parameter estimation
true_mean = 0;
true_sigma = 1;
% likelihood_func = @(x, mean, sigma) normpdf(x, mean, sigma);
% the above function to calcalate in matrix form, for speed
likelihood_func = @(x, mean, sigma)...
prod(normpdf(repmat(x,[1 numel(mean)]),...
repmat(mean, [1 numel(x)])',...
repmat(sigma,[1 numel(x)])' ), 1);
%% generate data
@drbenvincent
drbenvincent / importanceSamplingDemo1.m
Created December 10, 2015 20:52
quick example of importance sampling
%% true probability distribution
true_func = @(x) betapdf(x,1+1,1+10);
%% Do importance sampling
N = 10^6;
% uniform proposal distribution
x_samples = rand(N,1);
proposal = 1/N;
% evaluate for each sample
target = true_func(x_samples);
@drbenvincent
drbenvincent / importanceSamplingDemo2.m
Created December 10, 2015 21:27
importance sampling demo - Matlab - parameter estimation
true_mean = 0;
true_sigma = 1;
% likelihood_func = @(x, mean, sigma) normpdf(x, mean, sigma);
% the above function to calcalate in matrix form, for speed
likelihood_func = @(x, mean, sigma)...
prod(normpdf(repmat(x,[1 numel(mean)]),...
repmat(mean, [1 numel(x)])',...
repmat(sigma,[1 numel(x)])' ), 1);
%% generate data
true_mean = 0;
true_sigma = 1;
% likelihood_func = @(x, mean, sigma) normpdf(x, mean, sigma);
% the above function to calcalate in matrix form, for speed
likelihood_func = @(params)...
prod(normpdf(repmat(x,[1 numel(params(1))]),...
repmat(params(1), [1 numel(x)])',...
repmat(params(2),[1 numel(x)])' ), 1);
%% generate data
@drbenvincent
drbenvincent / multiple_groups_row_means.R
Created June 13, 2016 11:13
multiple groups of row means in R
# We have a dataframe with many columns.
# 15 of these columns correspond to responses to questions about hunger.
# They group into 5 subscales (each with 3 questions).
# I want to calculate these subscales (means) as well as an overall score (mean of all).
# This then needs to applied to the control condition (C) and a fasted condition (F) scores
# Once the means are calculated I no longer need the raw scores, so bonus is to automatically remove these columns. Need to retain all other columns in orginal dataframe.
# The code below works just fine, but seems pretty verbose. Is there a more R-like, concise way to do this?
# It seems like a split-apply-combine, but applied to columns rather than rows?
% Add tooolbox code to path
addpath('~/git-local/darc-experiments-matlab')
% Run the toolbox setup code
env_setup
% Get participant information with GUI
[expt_options] = getHumanExperimentOptions();
plotting_style = 'full';
% Add tooolbox code to path
addpath('~/git-local/darc-experiments-matlab')
% Run the toolbox setup code
env_setup
% Get participant information with GUI
[expt_options] = getHumanExperimentOptions();
plotting_style = 'full';
import pymc3 as pm
import numpy as np
import arviz as az
%config InlineBackend.figure_format = 'retina'
# Data from https://twitter.com/tomstafford/status/1456914037195907079?s=20
N = np.array([1258, 280]) # total number of caffiene and non-caffiene drinkers
k = np.array([966, 168]) # total number of those who have favourite mugs
def cohens_h(p):
@drbenvincent
drbenvincent / pymc3_distribution_plotting.ipynb
Last active December 29, 2021 02:42
Plotting univariate distributions in PyMC3
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.