A marketing application.
%pylab inline
import os
from ipypublish import nb_setup
%load_ext rpy2.ipython
Populating the interactive namespace from numpy and matplotlib The rpy2.ipython extension is already loaded. To reload it, use: %reload_ext rpy2.ipython
This topic is an interesting one in that it is more mathematical than some of the others in machine learning. It requires basic calculus and differential equations. Keith Devlin at Stanford has bemoaned the loss of mathematical ability as technology has created ways to substitute these skills. But, as he points out, it is hard to do math at the computer, the cognitive load is overwhelming and good old-fashioned paper and pencil is best. We will see some of that here. For a read of his article, see: https://drive.google.com/file/d/1hpeb5DgKYgGQvo6va-OOb2uBCgtUg26k/view?usp=sharing. This is taken from a chapter in this fascinating book: https://www.edge.org/annual-question/what-should-we-be-worried-about.
The Bass product diffusion model is a classic one in the marketing literature. It has been successfully used to predict the market shares of various newly introduced products, as well as mature ones.
The main idea of the model is that the adoption rate of a product comes from two sources:
The propensity of consumers to adopt the product independent of social influences to do so.
The additional propensity to adopt the product because others have adopted it. Hence, at some point in the life cycle of a good product, social contagion, i.e. the influence of the early adopters becomes sufficiently strong so as to drive many others to adopt the product as well. It may be going too far to think of this as a network effect, because Frank Bass did this work well before the concept of network effect was introduced, but essentially that is what it is.
The Bass model shows how the information of the first few periods of sales data may be used to develop a fairly good forecast of future sales. One can easily see that whereas this model came from the domain of marketing, it may just as easily be used to model forecasts of cashflows to determine the value of a start-up company.
There are some classic examples from the literature of the Bass model providing a very good forecast of the ramp up in product adoption as a function of the two sources described above. See for example the actual versus predicted market growth for VCRs in the 80s and the adoption of answering machines shown in the Figures below.
nb_setup.images_hconcat(["DSTMAA_images/VCRs.png"], width=650)
nb_setup.images_hconcat(["DSTMAA_images/AnsweringMachines.png"], width=650)
We follow the exposition in Bass (1969).
Define the cumulative probability of purchase of a product from time zero to time $t$ by a single individual as $F(t)$. Then, the probability of purchase at time $t$ is the density function $f(t) = F'(t)$.
The rate of purchase at time $t$, given no purchase so far, logically follows, i.e.
$$ \frac{f(t)}{1-F(t)}. $$Modeling this is just like modeling the adoption rate of the product at a given time $t$.
Bass suggested that this adoption rate be defined as
$$ \frac{f(t)}{1-F(t)} = p + q\; F(t). $$where we may think of $p$ as defining the independent rate of a consumer adopting the product, and $q$ as the imitation rate, because it modulates the impact from the cumulative intensity of adoption, $F(t)$.
Hence, if we can find $p$ and $q$ for a product, we can forecast its adoption over time, and thereby generate a time path of sales. To summarize:
We rewrite the Bass equation:
$$ \frac{dF/dt}{1-F} = p + q\; F. $$and note that $F(0)=0$.
The steps in the solution are: $$ \begin{eqnarray} \frac{dF}{dt} &=& (p+qF)(1-F) \\ \frac{dF}{dt} &=& p + (q-p)F - qF^2 \\ \int \frac{1}{p + (q-p)F - qF^2}\;dF &=& \int dt \\ \frac{\ln(p+qF) - \ln(1-F)}{p+q} &=& t+c_1 \quad \quad (*) \\ t=0 &\Rightarrow& F(0)=0 \\ t=0 &\Rightarrow& c_1 = \frac{\ln p}{p+q} \\ F(t) &=& \frac{p(e^{(p+q)t}-1)}{p e^{(p+q)t} + q} \end{eqnarray} $$
An alternative approach (this was suggested by students Muhammad Sagarwalla based on ideas from Alexey Orlovsky) goes as follows. First, split the integral above into partial fractions.
$$ \int \frac{1}{(p+qF)(1-F)}\;dF = \int dt $$So we write $$ \begin{eqnarray} \frac{1}{(p+qF)(1-F)} &=& \frac{A}{p+qF} + \frac{B}{1-F}\\ &=& \frac{A-AF+pB+qFB}{(p+qF)(1-F)}\\ &=& \frac{A+pB+F(qB-A)}{(p+qF)(1-F)} \end{eqnarray} $$
This implies that $$ \begin{eqnarray} A+pB &=& 1 \\ qB-A &=& 0 \end{eqnarray} $$
Solving we get $$ \begin{eqnarray} A &=& q/(p+q)\\ B &=& 1/(p+q) \end{eqnarray} $$
so that $$ \begin{eqnarray} \int \frac{1}{(p+qF)(1-F)}\;dF &=& \int dt \\ \int \left(\frac{A}{p+qF} + \frac{B}{1-F}\right) \; dF&=& t + c_1 \\ \int \left(\frac{q/(p+q)}{p+qF} + \frac{1/(p+q)}{1-F}\right) \; dF&=& t+c_1\\ \frac{1}{p+q}\ln(p+qF) - \frac{1}{p+q}\ln(1-F) &=& t+c_1\\ \frac{\ln(p+qF) - \ln(1-F)}{p+q} &=& t+c_1 \end{eqnarray} $$
which is the same as equation (*). The solution as before is
$$ F(t) = \frac{p(e^{(p+q)t}-1)}{p e^{(p+q)t} + q} $$We may also solve for
$$ f(t) = \frac{dF}{dt} = \frac{e^{(p+q)t}\; p \; (p+q)^2}{[p e^{(p+q)t} + q]^2} $$Therefore, if the target market is of size $m$, then at each $t$, the adoptions are simply given by $m \times f(t)$.
For example, set $m=100,000$, $p=0.01$ and $q=0.2$. Then the adoption rate is shown in the Figure below.
%%R
f = function(p,q,t) {
res = (exp((p+q)*t)*p*(p+q)^2)/(p*exp((p+q)*t)+q)^2
}
t = seq(1,20)
m = 100000
p = 0.01
q = 0.20
plot(t,m*f(p,q,t),type="l",col="blue",lwd=3,xlab="Time (years)",ylab="Adoptions")
grid(lwd=2)
%%R
#BASS MODEL
FF = expression(p*(exp((p+q)*t)-1)/(p*exp((p+q)*t)+q))
print(FF)
expression(p * (exp((p + q) * t) - 1)/(p * exp((p + q) * t) + q))
%%R
#Take derivative
ff = D(FF,"t")
print(ff)
p * (exp((p + q) * t) * (p + q))/(p * exp((p + q) * t) + q) - p * (exp((p + q) * t) - 1) * (p * (exp((p + q) * t) * (p + q)))/(p * exp((p + q) * t) + q)^2
%%R
#SET UP THE FUNCTION
ff = function(p,q,t) {
res = D(FF,"t")
}
%%R
#NOTE THE USE OF eval
m=100000; p=0.01; q=0.20; t=seq(1,20)
plot(t,m*eval(ff(p,q,t)),type="l",col="red",lwd=3)
grid(lwd=2)
nb_setup.images_hconcat(["DSTMAA_images/WolframAlphaSol.png"], width=650)
How do we get coefficients $p$ and $q$? Given we have the current sales history of the product, we can use it to fit the adoption curve.
Substituting for $f(t)$ and $F(t)$ in the Bass equation gives:
$$ \frac{s(t)/m}{1-S(t)/m} = p + q\; S(t)/m $$We may rewrite this as
$$ s(t) = [p+q\; S(t)/m][m - S(t)] $$Therefore, $$ \begin{eqnarray} s(t) &=& \beta_0 + \beta_1 \; S(t) + \beta_2 \; S(t)^2 \quad (BASS) \\ \beta_0 &=& pm \\ \beta_1 &=& q-p \\ \beta_2 &=& -q/m \end{eqnarray} $$
Equation (BASS) may be estimated by a regression of sales against cumulative sales. Once the coefficients in the regression $\{\beta_0, \beta_1, \beta_2\}$ are obtained, the equations above may be inverted to determine the values of $\{m,p,q\}$. We note that since
$$ \beta_1 = q-p = -m \beta_2 - \frac{\beta_0}{m}, $$we obtain a quadratic equation in $m$:
$$ \beta_2 m^2 + \beta_1 m + \beta_0 = 0 $$Solving we have
$$ m = \frac{-\beta_1 \pm \sqrt{\beta_1^2 - 4 \beta_0 \beta_2}}{2 \beta_2} $$and then this value of $m$ may be used to solve for
$$ p = \frac{\beta_0}{m}; \quad \quad q = - m \beta_2 $$As an example, let's look at the trend for iPhone sales (we store the quarterly sales in a file and read it in, and then undertake the Bass model analysis). We get the data from: http://www.statista.com/statistics/263401/global-apple-iphone-sales-since-3rd-quarter-2007/
The R code for this computation is as follows:
%%R
#USING APPLE iPHONE SALES DATA
data = read.table("DSTMAA_data/iphone_sales.txt",header=TRUE)
print(head(data))
print(tail(data))
Quarter Sales_MM_units 1 Q3_07 0.27 2 Q4_07 1.12 3 Q1_08 2.32 4 Q2_08 1.70 5 Q3_08 0.72 6 Q4_08 6.89 Quarter Sales_MM_units 30 Q4_14 39.27 31 Q1_15 74.47 32 Q2_15 61.17 33 Q3_15 47.53 34 Q4_15 48.05 35 Q1_16 74.78
%%R
#data = data[1:31,]
data = data[1:length(data[,1]),]
%%R
isales = data[,2]
cum_isales = cumsum(isales)
cum_isales2 = cum_isales^2
res = lm(isales ~ cum_isales+cum_isales2)
print(summary(res))
b = res$coefficients
Call: lm(formula = isales ~ cum_isales + cum_isales2) Residuals: Min 1Q Median 3Q Max -14.050 -3.413 -1.429 2.905 19.987 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 3.696e+00 2.205e+00 1.676 0.1034 cum_isales 1.130e-01 1.677e-02 6.737 1.31e-07 *** cum_isales2 -5.508e-05 2.110e-05 -2.610 0.0136 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 7.844 on 32 degrees of freedom Multiple R-squared: 0.8729, Adjusted R-squared: 0.865 F-statistic: 109.9 on 2 and 32 DF, p-value: 4.61e-15
%%R
#FIT THE MODEL
m1 = (-b[2]+sqrt(b[2]^2-4*b[1]*b[3]))/(2*b[3])
m2 = (-b[2]-sqrt(b[2]^2-4*b[1]*b[3]))/(2*b[3])
print(c(m1,m2))
m = max(m1,m2); print(m)
p = b[1]/m
q = -m*b[3]
print(c("p,q=",p,q))
cum_isales cum_isales -32.20691 2083.82202 [1] 2083.822 (Intercept) cum_isales2 "p,q=" "0.00177381124189972" "0.114767511363674"
%%R
#PLOT THE FITTED MODEL
nqtrs = 100
t=seq(0,nqtrs)
ff = D(FF,"t")
fn_f = eval(ff)*m
plot(t,fn_f,type="l",ylab="Qtrly Units (MM)",main="Apple Inc Sales")
n = length(isales)
lines(1:n,isales,col="red",lwd=2,lty=2)
The estimated Apple coefficients are: $p=0.0018$ and $q=0.1148$. For several other products, the table below shows the estimated coefficients reported in Table I of the original Bass (1969) paper.
nb_setup.images_hconcat(["DSTMAA_images/BassTable.png"], width=750)