Skip to content

Instantly share code, notes, and snippets.

View pengelbrecht's full-sized avatar

Peter Engelbrecht pengelbrecht

View GitHub Profile
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
import time
start_time = time.time()
# Model / data parameters
num_classes = 10
input_shape = (28, 28, 1)
var MAX_AGENTS_TO_TEST = 200;
function determineAgents(callDuration, calls, periodLength, answerTarget, serviceLevelTarget) {
if(calls === 0) return(0);
for (var agents = 1; agents <= MAX_AGENTS_TO_TEST; agents++) {
if(serviceLevel(callDuration, calls, periodLength, agents, answerTarget) >= serviceLevelTarget) break;
}
return(agents);
}
library(lubridate) # needs >= v1.6.0.9 for correct handling of lubridate.week.start with floor_date
options(lubridate.week.start = 1) # Weeks start on Mondays, use 7 for Sundays
library(dplyr)
library(ggplot2)
library(scales)
library(glue)
source("../analytics/utils/db.R", local = FALSE)
query <- function(sql, db = mainframe_db) {return(collect(tbl(db, sql(sql))))}
period_diff <- function(start, end, unit = "month") {
@pengelbrecht
pengelbrecht / segment_cohorts.sql
Created September 6, 2017 18:23
SQL Cohort Segment Analysis
select
product_segment as Segment,
round(avg(revenue_then)) as `Average Revenue`,
count(distinct company_id) as n,
round(power(power(sum(revenue_now) / sum(revenue_then), 1/period_diff(date_format(month_now, "%Y%m"), date_format(month_then, "%Y%m"))), 12), 2) as `Annual Revenue Retention`,
round(1 - power(1 - sum(churned)/count(*), 1/period_diff(date_format(month_now, "%Y%m"), date_format(month_then, "%Y%m"))), 3) as `Monthly Churn`
from (
select old.company_id, product_segment, size_segment, channel_segment, old.revenue as revenue_then, new.revenue as revenue_now, new.revenue is null as churned, new.revenue_month as month_now, old.revenue_month as month_then
from (
select
library(RPostgreSQL)
library(dplyr)
library(whisker)
source("config.R")
redshift <<- src_postgres(
dbname = redshift_database,
host = redshift_server,
port = redshift_port,
user = redshift_username,
library(rvest)
library(ggvis)
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
to_number <- function(string) {
string <- trim(string)
len <- nchar(string)
last_char <-substr(string, len, len)
suppressWarnings(
@pengelbrecht
pengelbrecht / cohort_analysis.R
Created September 30, 2014 15:58
R Cohort Analysis Example
require(RMySQL)
require(ggplot2)
require(scales)
myusername = "peter"
mypassword = "sekret"
system('ssh -f pe@192.168.0.10 -L 3306:localhost:3306 -N -o ExitOnForwardFailure=yes')
con <- dbConnect(MySQL(),
user=myusername, password=mypassword,
@pengelbrecht
pengelbrecht / testasdf.js
Last active August 29, 2015 14:07
wise words...
// Global variables
var BASEURI = 'https://app.firmafon.dk/api/v1';
var OPENTIME, CLOSETIME;
var SS = SpreadsheetApp.getActiveSpreadsheet();
function readConfig() {
OPENTIME = new Date();
OPENTIME.setHours(SS.getRangeByName("OpenHour").getValue());
OPENTIME.setMinutes(SS.getRangeByName("OpenMinute").getValue());
OPENTIME.setSeconds(0);
@pengelbrecht
pengelbrecht / testasdf
Created September 30, 2014 13:33
wise words...
// Global variables
var BASEURI = 'https://app.firmafon.dk/api/v1';
var OPENTIME, CLOSETIME;
var SS = SpreadsheetApp.getActiveSpreadsheet();
function readConfig() {
OPENTIME = new Date();
OPENTIME.setHours(SS.getRangeByName("OpenHour").getValue());
OPENTIME.setMinutes(SS.getRangeByName("OpenMinute").getValue());
OPENTIME.setSeconds(0);
# Formulas from http://www.mitan.co.uk/erlang/elgcspsh.htm
#input
n = 80 # number of calls
t = 8*60*60 # period length in seconds
d = 4.5*60 # call duration in seconds
M = 2 # number of agents
s = 20 # target SLA in seconds
# Output