Skip to content

Instantly share code, notes, and snippets.

View dah33's full-sized avatar

Dan Houghton dah33

View GitHub Profile
@dah33
dah33 / jinja_partial_func.sql
Last active October 9, 2023 15:41
How to do partial function application in Jinja / dbt
{# Modelled after Python's functools.partial #}
{% macro make_partial(func) %}
{% set partial = dict(func=func, args=varargs, keywords=kwargs) %}
{% do return(partial) %}
{% endmacro %}
{% macro is_partial(partial) %}
{% do return(partial is mapping
and partial.func
and partial.args
@dah33
dah33 / jinja_closure.sql
Last active October 8, 2023 23:14
How to make a function closure in Jinja / dbt
{# Save these macros in your dbt project /macros folder: #}
{% macro enclose(fn, env) %}
{% set closure = namespace(fn=fn, env=env) %}
{% do return(closure) %}
{% endmacro %}
{% macro call1(closure, x1) %}
{% do return(closure.fn(x1, closure.env)) %}
{% endmacro %}
@dah33
dah33 / mutual_information.sql
Created July 12, 2021 08:54
Mutual information in SQL between discrete variables
with t1 as (
select
column1 as x,
column2 as y
from your_table
),
t as (
select x, y
from t1
where x is not null and y is not null
@dah33
dah33 / predicting_customer_behav_1.R
Created November 19, 2018 19:23 — forked from mattbaggott/predicting_customer_behav_1.R
Uses the BTYD package and Pareto/NBD model to predict customer behavior in R Slides are at: http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#
# PREDICTING LONG TERM CUSTOMER VALUE WITH BTYD PACKAGE
# Pareto/NBD (negative binomial distribution) modeling of
# repeat-buying behavior in a noncontractual setting
#
# Matthew Baggott, matt@baggott.net
#
# Accompanying slides at:
# http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#