Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / happy_eyeballs.py
Created July 1, 2018 02:09
Happy eyeballs in Trio
import trio
async def open_tcp_socket(host_name, port, patience_in_seconds=1):
targets = await trio.socket.getaddrinfo(
host_name, port, type=trio.socket.SOCK_STREAM
)
winning_socket = None
async with trio.open_nursery() as nursery:
for *socket_config, _, target in targets:
def gamma(z: Real): Real = {
// The Nemes approximation to the log Gamma function isn't very accurate for 0 < z < 0.5,
// so we use a trick taken from Boost's lgamma function and calculate Log(Gamma(z + 1)) - Log(z) instead.
// See https://www.boost.org/doc/libs/1_50_0/libs/math/doc/sf_and_dist/html/math_toolkit/special/sf_gamma/lgamma.html
nemesGamma(z + 1) - z.log
}
private def nemesGamma(z: Real): Real = {
// An approximation to Log(Gamma(z)) due to Gergő Nemes. See https://en.wikipedia.org/wiki/Stirling%27s_approximation
val w = z + (Real.one / ((12 * z) - (Real.one / (10 * z))))
def pow(original: Real, exponent: Real): Real =
exponent match {
case Constant(exponent) => pow(original, exponent)
case _ => (original.log * exponent).exp
}
// delegates to def pow(original: Real, exponent: BigDecimal): Real = ...
@cqfd
cqfd / Kumaraswamy.scala
Created June 8, 2018 13:04
(Attempt at) the Kumaraswamy distribution in Rainier.
// Doesn't work yet!
object Kumaraswamy {
def apply(a: Real, b: Real): Continuous = new Continuous {
def realLogDensity(x: Real): Real =
If(x <= 0,
Real.zero.log,
If(x >= 1, Real.zero.log, (a - 1) * x.log + (b - 1) * (1 - x.pow(a))))
def param: RandomVariable[Real] = {
// From Distribution.scala
def gamma(z: Real): Real = {
val w = (z + 1) + (Real.one / ((12 * (z + 1)) - (Real.one / (10 * (z + 1)))))
(Real(Math.PI * 2).log / 2) - ((z + 1).log / 2) + ((z + 1) * (w.log - 1)) - z.log
}
def main(args: Array[String]): Unit = {
val _ =
SBC(Uniform(0.5, 1)) { x =>
Beta(x, x)
@cqfd
cqfd / Beta.scala
Created June 4, 2018 20:53
Rainier Beta distribution
import com.stripe.rainier.compute._
import com.stripe.rainier.core._
object Beta {
def apply(a: Real, b: Real): Continuous = new Continuous {
def realLogDensity(p: Real): Real =
If(p <= 0,
Real.zero.log,
If(p >= 1,
Real.zero.log,
@cqfd
cqfd / promisey.js
Last active September 1, 2017 17:14
Promisey props
// @flow
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import invariant from 'assert'
type Props = {
onSubmit(comment: string): Promise<void>
}
type State = {
@cqfd
cqfd / deminimnist.py
Created August 27, 2017 21:07
Little MNIST classifier from scratch.
import numpy as np
class Classifier(object):
def __init__(self):
self.net = Composed([
Affine.randomized(input_dim=28*28, output_dim=512),
ReLU(),
Affine.randomized(input_dim=512, output_dim=10),
Softmax()
])
@cqfd
cqfd / spooky.rs
Created February 17, 2017 03:05
Spooky (empty) Rust iterable
struct Spooky<T> {
_phantom: std::marker::PhantomData<T>
}
impl<T> Spooky<T> {
fn new() -> Spooky<T> {
Spooky { _phantom: std::marker::PhantomData }
}
}
struct SpookyIter<T> {
@cqfd
cqfd / keybase.md
Created February 8, 2017 20:08
Keybase proof of identity.

Keybase proof

I hereby claim:

  • I am cqfd on github.
  • I am cqfd (https://keybase.io/cqfd) on keybase.
  • I have a public key whose fingerprint is 59E4 B699 17F6 EE1A 9BEE 34FB A9E9 A7E1 64BD 8DCB

To claim this, I am signing this object: