Skip to content

Instantly share code, notes, and snippets.

@seantalts
seantalts / BinarySearch.hs
Created June 19, 2016 15:58
Binary Search to find first occurrence with duplicates
module BinarySearch where
import Data.Array
binarySearch :: (Ord a1) => Array Int a1 -> a1 -> Int -> Int -> Maybe Int
binarySearch haystack needle lo hi
| hi < lo = Nothing
| pivot > needle = binarySearch haystack needle lo (mid-1)
| pivot < needle = binarySearch haystack needle (mid+1) hi
| lo /= mid = binarySearch haystack needle lo mid
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Maybe
swords :: String -> Bool
swords "hello" = True
swords "my" = True
swords "sam" = True
swords _ = False
@seantalts
seantalts / MicroKanren.hs
Last active July 27, 2016 19:12
MicroKanren (μKanren) in Haskell
module MicroKanren where
import Data.List (transpose)
import Data.Maybe
import Control.Monad
data Term = Var Int | Atom String
deriving (Eq, Show)
------------------------------------------
data {
int<lower=0> N; // num individuals
int<lower=1> K; // num ind predictors
int<lower=1> J; // num groups
int<lower=1> L; // num group predictors
int<lower=1,upper=J> jj[N]; // group for individual
matrix[N, K] x; // individual predictors
row_vector[L] u[J]; // group predictors
vector[N] y; // outcomes
}
data {
int<lower=0> N; // num individuals
int<lower=1> K; // num ind predictors
int<lower=1> J; // num groups
int<lower=1> L; // num group predictors
int<lower=1,upper=J> jj[N]; // group for individual
matrix[N, K] x; // individual predictors
matrix[J, L] u; // group predictors
vector[N] y; // outcomes
}
data {
int<lower=0> N; // num individuals
int<lower=1> K; // num ind predictors
int<lower=1> J; // num groups
int<lower=1> L; // num group predictors
int<lower=1,upper=J> jj[N]; // group for individual
}
generated quantities {
matrix[K, J] z;
dans.rng = function(N_samp, n_eff, planck_length) {
pnorm(rnorm(N_samp, sd=1 + runif(N_samp, -1/sqrt(n_eff), 1/sqrt(n_eff))))
}
discrete.dan = function(N_samp, n_eff, pl) {
round(pnorm(rnorm(N_samp, sd=1 + runif(N_samp, -1/sqrt(n_eff), 1/sqrt(n_eff)))) * pl) / pl
}
linear_neff_plots = function(xlab, gen_data, n_samp, n_eff, planck_length) {
data = gen_data(n_samp, n_eff, planck_length)
bins <- n_eff * c(0.5, 1, 2, 3, 10, 50)
diff --git a/stan/math/prim/mat/fun/log_mix.hpp b/stan/math/prim/mat/fun/log_mix.hpp
index 3c7063ba3f..de82cc89b7 100644
--- a/stan/math/prim/mat/fun/log_mix.hpp
+++ b/stan/math/prim/mat/fun/log_mix.hpp
@@ -125,20 +125,20 @@ typename return_type<T_theta, T_lam>::type log_mix(const T_theta& theta,
* @param lambda array containing vectors of log densities.
* @return log mixture of densities in specified proportion
*/
-template <typename T_theta, typename T_lam>
-typename return_type<T_theta, std::vector<Eigen::Matrix<T_lam, -1, 1> > >::type
cdf = function(x, pdf) {
sapply(x, function(x) {
integrate(pdf, -Inf, x)$value
})
}
u = cdf(rnorm(1e5), dnorm)
hist(u, breaks=100)
@seantalts
seantalts / silly.cpp
Last active August 3, 2018 00:20
silly micro benchmarks for std::function vs custom functors
#include <vector>
#include <functional>
#include <Eigen/Dense>
#include <algorithm>
#include <iostream>
// CONFIGURABLE SIZES
typedef unsigned int index_t; // determines max expression size
typedef double real_t; // basic scalar type