Skip to content

Instantly share code, notes, and snippets.

View avibryant's full-sized avatar

Avi Bryant avibryant

  • Galiano Island, BC
View GitHub Profile
@avibryant
avibryant / loess.js
Created August 17, 2011 15:45
Loess smoothing
//adapted from the LoessInterpolator in org.apache.commons.math
function loess_pairs(pairs, bandwidth)
{
var xval = pairs.map(function(pair){return pair[0]});
var yval = pairs.map(function(pair){return pair[1]});
console.log(xval);
console.log(yval);
var res = loess(xval, yval, bandwidth);
console.log(res);
return xval.map(function(x,i){return [x, res[i]]});
@avibryant
avibryant / bayesab.js
Last active March 22, 2021 18:36
An implementation of http://www.evanmiller.org/bayesian-ab-testing.html with no depenedencies
function probabilityBBeatsA(aa, ba, ab, bb) {
var probability = 0.0;
for(var i = 0; i < ab; i++) {
var product = Math.log(1 + (aa + ba)/(i + bb));
var j = 1;
var start = i+1;
[bb, ba, aa, i].forEach(function(steps){
var stop = start + steps;

It had been 13 days since I'd found a spreader, and I was starting to get worried.

The last time my sero levels had been this low, I'd gone down to the arrivals level of the international terminal at the airport. I mean, obviously there wouldn't be any spreaders there unless someone screwed up the pre-flight screening. But they say that low viral loads of unfamiliar strains could still boost you in a pinch, so I scanned the room for the most exotic-looking passengers I could find and tried to cozy up to them. Once, I managed to lick the handles of a few suitcases going around the carousel without too many people noticing.

Anyway, I guess what they say is true, because my sero bounced up. But I think I got lucky; there's no substitute for prolonged contact with someone who's gone full sympto.

I texted my doctor again.

ffs you're literally killing me here

Positive.median(x)
#Exponential
Positive.mean(x)
Positive.countAndRate(n, x)
#LogNormal
Positive.meanAndStdDevOfLog(x,y)

Recent versions of Cloudera's Impala added NDV, a "number of distinct values" aggregate function that uses the HyperLogLog algorithm to estimate this number, in parallel, in a fixed amount of space.

This can make a really, really big difference: in a large table I tested this on, which had roughly 100M unique values of mycolumn, using NDV(mycolumn) got me an approximate answer in 27 seconds, whereas the exact answer using count(distinct mycolumn) took ... well, I don't know how long, because I got tired of waiting for it after 45 minutes.

It's fun to note, though, that because of another recent addition to Impala's dialect of SQL, the fnv_hash function, you don't actually need to use NDV; instead, you can build HyperLogLog yourself from mathematical primitives.

HyperLogLog hashes each value it sees, and then assigns them to a bucket based on the low order bits of the hash. It's common to use 1024 buckets, so we can get the bucket by using a bitwise & with 1023:

select
SystemOrganization addCategory: #'Diplomatik-Game'!
SystemOrganization addCategory: #'Diplomatik-Judge'!
SystemOrganization addCategory: #'Diplomatik-Users'!
SystemOrganization addCategory: #'Diplomatik-UI'!
WAComponent subclass: #DAdmin
instanceVariableNames: 'password'
classVariableNames: ''
poolDictionaries: ''
category: 'Diplomatik-UI'!
val a = Real.latent{a => a.mean(0) && a.stdDev(1)}
val b = Real.latent{b => b.mean(0) && b.stdDev(1)}
val xs: Seq[Double] = ???
val ys: Seq[Long] = ???
Model.observe(xs, ys){(x,y)=>
y.mean(a + x*b)
}
Mean(x).stdDev(y)
Mean(x).stdDev(y).exp
Mean(x).absErr(y)
Mean(x).positive
Median(x).positive
Median(x).iqr(y)
Probability(p).successes(n)
Probability(p).stdDev(y)
Probability(p).stdDev(y).successes(n)
attacker = ARGV[0].to_i
defender = ARGV[1].to_i
def roll(k, n)
(1..k).map{rand(6)}.sort.reverse.first(n)
end
while attacker > 0 && defender > 0
sleep 1
attacker_dice = [attacker, 3].min
case class DensityPlot(
nRows: Int = 20,
nColumns: Int = 80,
xLabelWidth: Int = 9,
yLabelWidth: Int = 9,
yLabelEvery: Int = 5,
logX: Boolean = false,
logY: Boolean = false,
logMarkers: Boolean = false,
markers: String = "·∘⚬") {