Skip to content

Instantly share code, notes, and snippets.

View cpfiffer's full-sized avatar
🤙
LOVING IT

Cameron Pfiffer cpfiffer

🤙
LOVING IT
View GitHub Profile
@cpfiffer
cpfiffer / candle_blows.r
Created January 17, 2017 09:07
Solves for the birthday candle riddle at FiveThirtyEight
#The riddle can be found at https://fivethirtyeight.com/features/how-long-will-it-take-to-blow-out-the-birthday-candles/
tests = 10000000
blows <- rep(0,tests)
i = 1
while (i <=tests) {
candles = 30
while (candles > 0) {
@cpfiffer
cpfiffer / integer_chain.jl
Created July 31, 2017 19:30
This is a Julia implementation to solve the Riddler Classic problem this week, whereby you have to make the longest chain of integers according to some rules.
# Cameron Pfiffer
# July 31st, 2017
# Julia Does Numbers 2: The Mathening
# See the link below for information about the challenge.
# https://fivethirtyeight.com/features/pick-a-number-any-number/
#=
From Itay Bavly, a chain-link number problem:
# Misc functions for conservation biology stats in R.
shannon <- function (x) {
proportion_x = x/sum(x)
print(proportion_x)
print(log(proportion_x))
print(proportion_x * log(proportion_x))
print(sum(proportion_x * log(proportion_x)))
return(-sum(proportion_x*log(proportion_x)))
}
@cpfiffer
cpfiffer / token-scrum.jl
Created February 24, 2018 20:11
For the Riddler.
# From Keith Wynroe, a battle for the tokens:
#
# You have one token, and I have two tokens. Naturally, we both crave more tokens, so we play a game of skill that unfolds over a number of rounds in which the winner of each round gets to steal one token from the loser. The game itself ends when one of us is out of tokens — that person loses. Suppose that you’re better than me at this game and that you win each round two-thirds of the time and lose one-third of the time.
#
# What is your probability of winning the game?
@everywhere function play_game()
p1 = 2
p2 = 1
play = true

Keybase proof

I hereby claim:

  • I am cpfiffer on github.
  • I am cpfiffer (https://keybase.io/cpfiffer) on keybase.
  • I have a public key ASBuOyV4zsOUIxWVtBtAh0aRwqkKu25BwWPjUIxgfL8R4go

To claim this, I am signing this object:

@cpfiffer
cpfiffer / P21.hs
Created March 25, 2018 16:32
Solves problem 21 of Project Euler with list comprehensions.
module P21 (properDivisors, sumDivisors, sumAmicable, isAmicable, amicableList) where
properDivisors :: Int -> [Int]
properDivisors x = [xs | xs <- [1..x-1], x `mod` xs == 0]
sumDivisors :: Int -> Int
sumDivisors x = sum $ properDivisors x
@cpfiffer
cpfiffer / diag_test.jl
Created August 3, 2019 17:27
Test ESS draws
using CmdStan, DynamicHMC
using StatsPlots, Random, MCMCDiagnostics
using Revise
using Turing, AdvancedHMC; const AHMC = AdvancedHMC
Random.seed!(1239911)
ProjDir = @__DIR__
cd(ProjDir)
Nsamples = 2000
@cpfiffer
cpfiffer / diag_test_2.jl
Created August 3, 2019 18:05
Test bias
using CmdStan, StatsPlots, Random, MCMCDiagnostics
Random.seed!(12395391)
ProjDir = @__DIR__
cd(ProjDir)
berstanmodel = "
data {
int<lower=0> N;
@cpfiffer
cpfiffer / diag_test_3.jl
Created August 3, 2019 18:07
Test ESS cap
using MCMCChains
function sim(nsamples)
cnt = 0
for i in 1:1000
chn = Chains(randn(nsamples,1,1))
ess = describe(chn)[1].df[:ess][1]
ess > nsamples ? cnt+=1 : nothing
end
return cnt
@cpfiffer
cpfiffer / garch.jl
Created November 26, 2019 14:32
GARCH in Turing.jl
using Turing
using CSVFiles
using DataFrames
using Dates
using StatsPlots
# The TV syntax allows sampling to be type-stable.
@model garch(r, ::Type{TV}=Vector{Float64}) where {TV} = begin
T = length(r)