% SIMIODE-TeacherStudent-Template.tex and simiode.cls for LaTeX2e
% Version 22 November 2016.
% 
% This template is to be used with the  class file "simiode.cls". 
%  
% The latest version of this class and template files 
% can be found at
% 
%       <http://www.SIMIODE.org> in the Author section.
%
% A "Quick Start Guide" appears below.
%
% This file must be processed with version LaTeX2e or higher.
% Appreciation to Mark Frantz, IUPUI; John Thoo, Yuba College; and Eric Sullivan, Carroll College.
%
\documentclass{simiode}
%
% With this version of the tex file and the associated SIMIODE.cls file you can produce BOTH a Student Version and a Teacher Version of your modeling scenario or technique narrative.
%
% Student Version contains the title STUDENT VERSION along with the NAME OF SCENARIO, with no abstract, no keywords, no tags, no author identifiers, and no COMMENTS section. The Student Version contains only the STATEMENT of the modeling scenario only with appropriate figures, data, instructions, and perhaps a bibliography appropriate for student resourcing, but nothing in the bibliography which would "give away" the nature of the modeling scenario, etc.
%
%The Teacher Version contains the material in the Student Version, namely the title NAME OF SCENARIO, now headed with   TEACHER VERSION, but also full author  identification, abstract, key words, tag words, and, following the STATEMENT, a section of COMMENTS which can be discussion of presentation  ideas, "solutions," techniques, etc.
%
% Authors can prepare a full Teacher Version using the notions immediately below  to create a pdf for review, but in review please eliminate all information about the author as we use a double-blind referee system. Such information will be restored when accepted and published in SIMIODE at www.simiode.org.
%
%***********   Toggle the following line to turn the teacher version on (1) or off (0). ***********
%
\def\TeacherEdition{0} % Toggle this line's value.
\newcommand{\teacher}[1]{\ifnum\TeacherEdition=1 #1 \fi}
%
% The \teacher{ } command wraps any text that will appear only in the teacher version of
% the document. See this file for examples. This has been done for you in the document below.   
% Rename the pdf after you compile each final version.
%
\usepackage{amsmath,amssymb}
\usepackage{color}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{url}
\input{julia-stylings}
\usepackage{appendix}

\hypersetup{colorlinks, linkcolor=blue}
%
\renewcommand{\baselinestretch}{1.3}
%
% NOTE regarding macros %%
%
% Any macros defined for your paper should be contained in 
% the top matter. Likewise, any environment definitions such 
% as \newtheorem or \newenvironment should also go in the 
% top matter. 
% 
% Do not \input your macros as separate files within your 
% final version because more files make it harder for the 
% editors and users to keep track of and/or modify and 
% append your paper.
%
% Top matter is above and the beginning of the document below.
%
\begin{document}

\newpage
%
% You only need to change the NAME OF SCENARIO in the next line and delete the footnote. 
% See above for instructions on toggling from Teacher Version to Student Version.
%
\title{\ifnum\TeacherEdition=1 TEACHER VERSION \else STUDENT VERSION \fi\\ The flush toilet: slow and late start}





\markboth{}{}

\author{Maila Brucal-Hallare and Charles Lamb\\ 
Norfolk State University and 
Indiana University of Pennsylvania\\
Norfolk VA 23504 USA and Indiana PA 15705 USA\\
\teacher{mcbrucal-hallare@nsu.edu and clamb@iup.edu}
}

\makeStitlePDFLaTex


\begin{abstract}
This activity analyzes the spread of a technological innovation using the Bass Model from Economics. The equation is a first-order, two-parameter separable equation and the solution has a characteristic S-shaped curve or sigmoid curve.  The students derive the solution to the model, use least squares method to estimate the parameters, and investigate another technological innovation using the method outlined here.   
\end{abstract}



\section*{SCENARIO DESCRIPTION}
To flush, or not to flush?  That is not the question.  The question is, how does one use differential equations to model the spread of toilet flushing as a technological innovation in the United States?    

Various historical sources credit Sir John Harington, godson of Queen Elizabeth I, in designing the first flushable toilet in 1596.  His design contained the two components of the modern toilet: a valve to flush out the water and a wash-down system to empty the bowl.  His design, however, failed to eliminate the foul smell.  More than two hundred years after, Alexander Cumming, a Scottish mathematician and inventor, improved Harington's design by inventing the S-trap that retained water permanently in the waste pipe and prevented the foul smell from escaping.  The spread of Cumming's S-trap design was slow.  When the Great Stink in Central London happened in the summer of 1858, the legislators of the Houses of Parliament pushed to require the S-trap as part of the sewage system \cite{wikiFlushToilet}. The Houses of Parliament was located along the River Thames, which was practically an open sewer at that time. Indeed, government legislation has the power to accelerate the diffusion of technological innovations and in this case, it used its power to control the diffusion of the stink.      

In America, flush toilets were introduced in the homes of the wealthy in the 1860's.  Up until the 1980's, only 98 percent of American households have access to flush toilets.  Table \ref{tab:FlushToiletData} below summarizes the data on how this technological innovation spread in the United States:
\begin{table}[h!]
\centering
\begin{tabular}{c|c}
Year & Percentage\\
\hline
1860 & 1 \\
1880 & 7 \\ 
1890 & 13 \\
1900 & 15 \\
1920 & 20\\
1930 & 51 \\
1940 & 60 \\
1950 & 71 \\
1960 & 87 \\
1970 & 96 \\
1980 & 98 \\
1989 & 100\\
\end{tabular}
\caption{The data on the innovation and diffusion of the flush toilet, OurWorldInData.org  \cite{owidtechnologyadoption}.  In the 1860s, only the American wealthy elites (1 percent of the population) have access to flush toilets.}
\label{tab:FlushToiletData}
\end{table}

In this activity, we will investigate the Bass Model, a mathematical model that economists use to model the diffusion of an innovation:
\begin{equation}\label{Bass}
\dfrac{dF}{dt} = (p + qF)(1 - F)    
\end{equation}
where $F(t)$ is the fraction of the population who have adapted the technology, $p$ is the rate of spontaneous innovation, and $q$ is the rate of imitation, see \cite{jackson2010social}.  The model does not take into account the social structure or even government intervention that significantly contributes to the spread of innovation.  The second factor $1- F$ captures the portion of the population that has not adapted the technology.       

Observe that when $p = 0$, we obtain the classic logistic model, whose solution is shaped like an $S$,
and when $q = 0$, we obtain the exponential model whose solution is asymptotic to the horizontal $F = 1$. The solution of the Bass Model for $p + q \neq 0$ has a characteristic $S$-shape, too, with the parameter $p$ as the initial main driver of the spread, then the parameter $q$ takes over, until the function $F(t)$ approaches 1.  The first activity guides the students to a step-by-step approach to finding the closed-form of the solution to (\ref{Bass}).  

Next, the second activity uses the least-squares method to estimate the parameters when the Bass Model is applied to the data in Table \ref{tab:FlushToiletData}.  Special optimization packages, Optim in Julia and Solver in Excel, are used to approximate values for $p$ and $q$ to model the innovation and diffusion of the flush toilet in American homes.

A final activity gives the students the opportunity to fit data on another technological innovation: the household refrigerator.  Like the flush toilet, only the wealthy had refrigerators in their homes during the first quarter of the 1900's.  It was around the 1980's when finally, all American households owned a refrigerator.


\subsection*{Activity 1.  Analyzing the mathematical model.}

Consider the initial-value problem
\begin{equation}\label{IVP Bass}
\left\{
\begin{array}{ll}
\dfrac{dF}{dt} = (p + qF)(1 - F)\\   F(0)  = 0.
\end{array}
\right.
\end{equation}
\begin{enumerate}
\item Using separation of variables, we obtain,
$$
\dfrac{dF}{(p + qF)(1 - F)} = dt.
$$
The left-hand side can be integrated via the partial fraction decomposition technique, provided $p + q \neq 0$:
$$
\dfrac{1}{(p + qF)(1 - F)} = \dfrac{A}{p + qF} + \dfrac{B}{1 - F}.
$$
Verify that $A = \dfrac{q}{p + q}$ and $B = \dfrac{1}{p + q}$.

\item With the given values for $A$ and $B$, integrate the separated equation.  Verify that we obtain:
$$
t = \dfrac{1}{p + q}\ln\left|\dfrac{p+qF}{1 - F}\right| + C.
$$

\item Next, we exponentiate the previous result and use the initial value $F(0) = 0$ to compute for $C$.  Verify that the solution to the initial-value problem (\ref{IVP Bass}) is
\begin{equation}\label{solution}
F(t) = \dfrac{1 - e^{-(p + q)t}}{1 + \frac{q}{p} e^{-(p + q)t}}    
\end{equation}
\end{enumerate}


\subsection*{Activity 2.  Fitting the data using the model via the Optim package in Julia.} 

In order to approximate the parameter values $p$ and $q$ that measures rate of innovation and adoption, respectively, we will use the least squares approach.  

Using the data in Table \ref{tab:FlushToiletData}, denote the time by $t_{i}$ and the data values in the second column by $y_{i}$.   The goal is to fit the Bass model solution to the historical data.  At a given time $t_{i}$, the error $E$ between the model  $F(t_{i})$ and the actual data $y_{i} = y(t_{i})$ is given by
$$
E = \sum_{i=1}^{n}(F(t_{i}) - y_{i})^2.
$$
Simplifying, we get
$$
E = \sum_{i=1}^{n} F(t_{i})^2 - 2 F(t_{i})y_i + y_{i}^2,
$$
which is a function that depends on the parameters $p$ and $q$. The goal is to find values for $p$ and $q$ that minimizes the error $E$ over all the given values $t_{i}$.  This is a so-called \emph{least-squares} approach, see \cite{mooney1999course} for more on intrinsically nonlinear models that cannot be linearized.

In order to accomplish this, we will use Julia, an open source programming software that has powerful mathematical capabilities, \cite{bezanson2017julia}.  The Optim package \cite{optim} in Julia provides solutions to optimization problems, including unconstrained ones like what we have here. 


\vspace{1em}
\input{Bass-Model-Julia-nbCell}

Observe that in cell 3, time $T$ is rescaled so that $T = 0$ corresponds to 1860.  This modeling choice induces a small amount of error.  Recall, the form of $F(t)$ in (\ref{solution}) was found under the assumption that $F(0) = 0,$ which does not correspond to the historical data of 1\% adoption in 1860. If however, $T=0$ was chosen to correspond to 1596, we would skew the model and ultimately alter the values of the fitted parameters.  There are trade-offs to either choice. 

Observe in cell 5, the initial guess $[p_0, q_0] = [.25, .5]$ was chosen arbitrarily so that $p_0 \neq q_0$ and that both $p_0$ and $q_0$ are small.  If the choice $p_0 = q_0$ is made, the symmetry in parameters might fail to be broken.

To complete this activity, either download Julia or complete the following steps:
\begin{enumerate}
\item Create an account in \url{http://cocalc.com}.
\item Select \textit{Create New Project.}  Create a title.
\item Select \textit{Jupyter Notebook} and the kernel, \textit{Julia}.
\item Copy the cells above and SHIFT-ENTER to run a cell.
\item What parameter values fit the model to the data?  How does the graph look?   
\end{enumerate}

\begin{figure}
\centering
\includegraphics[width=8cm]{Simiode Optim Plot.PNG}
\caption{A visualization of the actual data and the fitted Bass model, with the optimal parameter values $(p, q) \approx (0.000544, 0.0649)$. }
\end{figure}

\subsection*{Activity 3.  Fitting the data using the model via the Solver package in Excel.}

Microsoft Office's spreadsheet application tool, Excel, can also be used to solve unconstrained optimization problems.  Excel has its own optimization package called Solver.  These are the steps to complete in order to estimate the parameters $p$ and $q$:
\begin{enumerate}
\item Prepare the Excel spreadsheet before using Solver (see Figure \ref{fig:ExcelSetup}): 

\begin{enumerate}
\item Create one column each for the scaled data in Table \ref{tab:FlushToiletData}: time $t_{i}$ and percentage $y_{i}$.  
\item Create a column,  \textit{Bass Model Fit}, that computes $F(t_{i})$ using  (\ref{solution}) with an initial guess for the parameters $p$ and $q$.  

\item Make a line graph to visualize the plots of $(t_{i}, y_{i})$ and $(t_{i}, F(t_{i})).$  Adjust the initial guess for the parameters $p$ and $q$ using this line graph.

\item Create columns that compute the error, $f(t_{i}) - y_{i}$, and the square of the error. Then compute the sum of squares of error (SSE); in Figure \ref{fig:ExcelSetup}, SSE is recorded in cell G15.
\begin{figure}[h!]
\centering
\includegraphics[width=8cm]{Excel SetUp.PNG}
\caption{Setting up the data before using the Solver package in Excel.}
\label{fig:ExcelSetup}
\end{figure}

\end{enumerate}

\item Go to Data and click Solver.  (If Solver is not available, then it must be added using the following steps: File $\to$ Options $\to$ Add-ins $\to$ Solver Add-in $\to$ OK.)

\item We are ready to compute the minimum of the sum of the squares of the errors.  In Solver, indicate G15 in \textit{Set Objective}.  Click \textit{minimum.}  Indicate the location of the parameters $p$ and $q$ in \textit{By Changing Variable Cells}.  Hit Solve.  See Figure \ref{fig:ExcelSolver}.

\begin{figure}[h!]
\centering
\includegraphics[width=8cm]{Excel Solver.PNG}
\caption{Using Solver in Excel.}
\label{fig:ExcelSolver}
\end{figure}

\item Solver will update the parameter values that solves the problem.  Compare the values obtained by Julia and by Excel.  See Figure \ref{fig:ExcelSolverResults}.
\begin{figure}[h!]
\centering
\includegraphics[width=8cm]{Excel Solver Results.PNG}
\caption{Estimate of the parameters using Solver in Excel.}
\label{fig:ExcelSolverResults}
\end{figure}

\end{enumerate}








\subsection*{Activity 4.  Analyzing America's Adoption of a Refrigerator.}

Having completed the previous that investigated the adoption of the flush toilet as a technological innovation in the United States, let us now look at actual data on the percentage of American households that own a refrigerator.  See Figure \ref{fig:Ref and Flush}.



\begin{enumerate}
\item Go to the website
\url{ https://ourworldindata.org/technology-adoption}.  The website contains data on the US adoption of various technological innovations - charts, tables, and CSV files are available.  Explore the website and be excited about the research possibilities that can arise from the information here!  
\item Click on \textit{Download the CSV file} and find data $(t_{i}, y_{i})$ on the adoption of the refrigerator (\emph{not ``household refrigerator"}). Evidently, there are two different refrigerator datasets.
\item Use either the Julia code or implement the Excel steps as described in the previous activities in order to estimate the parameters $p$ and $q$.   
\item What parameter values fit the model to the data?  How does the graph look? 
\begin{figure}[h!]
\centering
\includegraphics[width=8cm]{Ref and Flush.PNG}
\caption{Percentage of American households that own a refrigerator vs having a flush toilet.}
\label{fig:Ref and Flush}
\end{figure}
\item Compare the ratios $q/p$ for the flush toilet data and the refrigerator data.  Discuss the discrepancy.  See Figure \ref{fig:Ref and Flush}. Recall the interpretations of $p$ and $q$, and you  may wish to consider the speed of information spread in different eras as well as the costs of technological adoption.


\item Research the story behind the adoption of the household refrigerator.  Why is there a dip in the data during the first few years of the 1940's?
\item There are various governmental, economic, and political factors that affect both the innovation and imitation parameters of the model.  The introduction of this document discusses legislation in London in the summer of 1858 affecting the design and adoption of the flushing toilet. In the data, the landline is affected by an event in the early 1930s, and the previous question considered the refrigerator dip in the 1940's.  What has not been discussed is the influence of other technologies.  Examine the landline data.  Describe what happens to the landline in the early 2000's and why the Bass model is not an appropriate model for this data.
\end{enumerate}


\appendix 
\appendixpage
\section*{Julia Tutorials and Code Remarks}
Julia is an open source mathematical language akin to MATLAB.  Below are a few resources for further investigation:

\begin{itemize}
    \item General Purpose Tutorials  \url{https://julialang.org/learning/tutorials/}
    \item Symbolic DEs (SymPy) \url{https://docs.juliahub.com/SymPy/KzewI/}
    \item Numerical DEs (SCiML) \url{https://tutorials.sciml.ai/}
\end{itemize}
   
We remark on the included Julia code.  

\begin{enumerate}
    \item The vectorized ``dot" operator applies the operator to each entry of the vector.
    \begin{enumerate}
        \item For example, {\tt v .- 100} is equivalent to {\tt v[1] = v[1] - 100}, {\tt v[2] = v[2] - 100}, etc.
        
        \item Functions can also be applied in a vectorized manner; such was done with  {\tt F.(T, p, q)}.  The output is a vector consisting of the entries {\tt F($t_1$, p, q)}, {\tt F($t_2$, p, q)},  etc.
    \end{enumerate}
    
    \item Argument destructuring was used in the definition of the square error:  {\tt function sqErr((p,q))}.  A vector is passed into {\tt sqErr}, length is expected to be two, and the first element is named {\tt p} and the second is name {\tt q}, see \url{https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring}.
    
    \item Semicolons suppress output, like in cell 1.
    \item External packages, such as {\tt Optim} and {\tt Plots}, need to be imported with {\tt using}.  
    \item The bang {\tt !} convention is used in Julia to denote a mutable operation, i.e. a function will update/modify its arguments. For example, {\tt plot!} modifies the existing plot.
\end{enumerate}

\newpage
\section*{Single Julia Listing}
\lstinputlisting[language=Julia, style=blockJuliaStyle, caption=Julia code to fit Bass model parameters $p$ and $q$, tabsize=2]{Bass-Model-Julia-6.jl}

\teacher{
\section*{NOTES FOR TEACHERS}
We elected to let our comments to students speak to teachers throughout as well.
}
\begin{thebibliography}{99}

\bibitem{bezanson2017julia}Bezanson, J., Edelman, A., Karpinski, S. \& Shah, V. \emph{Julia: A fresh approach to numerical computing}, SIAM Review, \textbf{59} (2017), 65-98, DOI \href{https://doi.org/10.1137/141000671}{https://doi.org/10.1137/141000671}.
\bibitem{jackson2010social}Jackson, M. \emph{Social and Economic Networks}, Princeton University Press, 2010.
\bibitem{optim} Mogensen et al. \emph{Optim: A mathematical optimization package for Julia}. Journal of Open Source Software {\bf 3} (2018), no. 24, 615, DOI \href{https://doi.org/10.21105/joss.00615}{https://doi.org/10.21105/joss.00615}.
\bibitem{mooney1999course}Mooney, D. \& Swift, R. \emph{A Course in Mathematical Modeling}, American Mathematical Society, 1999.
\bibitem{owidtechnologyadoption}Ritchie, H. \& Roser, M. \emph{Technology Adoption}, Our World In Data, 2017, \href{https://ourworldindata.org/technology-adoption}{https://ourworldindata.org/technology-adoption}.
\bibitem{wikiFlushToilet} Wikipedia Contributors, \emph{Flush Toilet}, retrieved Oct, 15 2021, \url{https://en.wikipedia.org/wiki/Flush_toilet}.


\end{thebibliography}



\end{document}
