Skip to content

Instantly share code, notes, and snippets.

View ESeufert's full-sized avatar

Eric Benjamin Seufert ESeufert

View GitHub Profile
# I had an annoying situation where I wanted a multi-chart matplotlib chart to look symmetrical given some number of max
# rows and columns
# eg. if I have 9 graphs I want to plot and the max number of (rows, columns) for the chart is (4, 4),
# I don't want 2 rows with 5 charts on the top and 4 on the bottom;
# I want 3 rows with 3 charts per row
# this algorithm returns a tuple of rows, columns given some parameters:
# this_length is the number of charts to plot
# max_cols is the maximum number of columns the chart can have (the matplotlib max is 4)
# max_rows is the maximum number of rows the chart can have (matplotlib max is 4)
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.colors as pltcolors
import pandas as pd
import numpy as np
import random
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import colors as mcolors
from matplotlib import cm
from beautifultable import BeautifulTable
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from scipy.optimize import curve_fit
import statsmodels.stats.api as sms
import math
import operator
@ESeufert
ESeufert / LTV_and_Retention_Curves.py
Created January 14, 2019 16:44
Showcasing how LTV curves are dependent on Retention Curves
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pandas as pd
import numpy as np
import random
def build_userbase( n, payer_percentage ):
users = pd.DataFrame( columns=[ "user", "payer", "payment_probability", "payment" ] )
for x in range( 1, n + 1 ):
payer = True if random.randint( 1, 100 ) <= ( payer_percentage * 100 ) else 0
$(document).ready(function() {
var data = getData();
buildConsole(data);
resetCharts(data);
$('#country-selector').change(function() {
var metrics = cloneData(data);
function resetCharts(metrics) {
var countries = getCountries(); // get checked items
var metric = aggregateMetric(metrics, countries); // build one metric item out of the selected countries and the full dataset
$('#metrics').html(''); // reset the metrics div html
buildLineChart(metric, "Retention", ['d1_retention_pct', 'd7_retention_pct', 'd30_retention_pct'], 1000, 300, "Date", "Retention (percentage)");
buildLineChart(metric, "Daily New Users", ['DNU'], 500, 300, "Date", "New Users");
buildLineChart(metric, "Daily Active Users", ['DAU'], 500, 300, "Date", "Users");
buildLineChart(metric, "Average Session Length", ['average_session_length'], 500, 300, "Date", "Session Length (minutes)");
buildLineChart(metric, "Average Sessions per User", ['average_sessions_per_user'], 500, 300, "Date", "Sessions per User");
function aggregateMetric(metrics, countries) {
var metric = []; //the single metric array of dates that we'll return,
//aggregated over the selected countries
var count = 0; // a count of countries being aggregated over
for (var i = 0; i < metrics.length; i++) {
if (jQuery.inArray(metrics[i][0], countries) > -1) { //this is a country we should aggregate for
if (count == 0) {
metric = cloneMetric(metrics[i]); // since metric is empty, set metric to the first set of metrics we find
count++;
function cloneData(data) {
//create a "deep copy" of the objects within the metrics array
//ensures that objects are not copied by reference
var metrics = [];
for (var i = 0; i < data.length; i++) {
metrics.push(cloneMetric(data[i]));
}
return metrics;
}
function buildConsole(data) {
var html = "";
html += "<div id=\"country-selector\" class=\"console-element\">";
for (var i = 0; i < data.length; i++) {
html += "<div style=\"width: 150px; float: left;\">";
html += "<input name=\"countries\" id=\"country-selector-" + data[i][0] + "\" type=\"checkbox\" ";
if (i == 0) {
html += "checked"; // set the first country to checked to provide some default data for the graphs
}
function getData() {
var data = [];
var metrics =
{
"countries":
[
{
"country": "USA",