Skip to content

Instantly share code, notes, and snippets.

@maxbeutel
Created February 28, 2018 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxbeutel/3df20dfa327096efc882b8ee0d52a641 to your computer and use it in GitHub Desktop.
Save maxbeutel/3df20dfa327096efc882b8ee0d52a641 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
// Cumulative distribution function for the standard normal distribution
// Percentage of data that lies within the standard distribution
func cdfNorm(x float64) float64 {
return (1.0 + math.Erf(x/math.Sqrt(2.0))) / 2.0
}
func convRate(visitors, conversions float64) float64 {
return visitors / conversions
}
func stdErr(convRate, visitors float64) float64 {
return math.Sqrt((convRate * (1 - convRate) / visitors))
}
func zScore(control_p, variation_p, control_se, variation_se float64) float64 {
return (control_p - variation_p) / math.Sqrt(math.Pow(control_se, 2)+math.Pow(variation_se, 2))
}
type lim struct {
from float64
to float64
}
func limit(p, se float64) lim {
var from, to float64
if p-1.65*se < 0 {
from = 0
} else {
from = p - 1.65*se
}
if p+1.65*se > 1 {
to = 1
} else {
to = p + 1.65*se
}
return lim{from: from, to: to}
}
func main() {
// input variables:
var controlVisitors = 8368.
var controlConversions = 116.
var variationVisitors = 7956.
var variationConversions = 133.
// ------- computations
controlp := convRate(controlConversions, controlVisitors)
fmt.Println("Conversion rate control", controlp*100, "%")
variationp := convRate(variationConversions, variationVisitors)
fmt.Println("Conversion rate variation", variationp*100, "%")
controlse := stdErr(controlp, controlVisitors)
fmt.Println("Control standard error", controlse*100, "%")
variationse := stdErr(variationp, variationVisitors)
fmt.Println("Control standard error", variationse*100, "%")
score := zScore(controlp, variationp, controlse, variationse)
fmt.Println("z-score", score*100, "%")
pval := cdfNorm(score)
fmt.Println("P-value:", pval)
if pval < 0.1 || pval > 0.9 {
fmt.Println("Significance: YES")
} else {
fmt.Println("Significance: NO")
}
fmt.Println("Control 90% Conversion Rate Limits", limit(controlp, controlse))
fmt.Println("Variation 90% Conversion Rate Limits", limit(variationp, variationse))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment