<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/different-demand-functions-and-optimal-price-estimation-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://insightr.wordpress.com/2018/06/03/different-demand-functions-and-optimal-price-estimation-in-r/"> R – insightR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><strong>By Yuri Fonseca</strong></p>
</div>
<h2>Demand models</h2>
<p>In the previous post about pricing optimization (<a rel="nofollow" href="https://insightr.wordpress.com/2017/08/27/pricing-optimization-how-to-find-the-price-that-maximizes-your-profit/" target="_blank">link here</a>), we discussed a little about linear demand and how to estimate optimal prices in that case. In this post we are going to compare three different types of demand models for homogeneous products and how to find optimal prices for each one of them.</p>
<p><span></span></p>
<p>For the linear model, demand is given by:</p>
<p><img alt="displaystyle d(p) = alpha p + beta," title="displaystyle d(p) = alpha p + beta," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%28p%29+%3D+%5Calpha+p+%2B+%5Cbeta%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>where <img alt="alpha" title="alpha" src="https://s0.wp.com/latex.php?latex=%5Calpha&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is the slope of the curve and <img alt="beta" title="beta" src="https://s0.wp.com/latex.php?latex=%5Cbeta&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> the intercept. For the linear model, the elasticity goes from zero to infinity. Another very common demand model is the constant-elasticity model, given by:</p>
<p><img alt="displaystyle ln d(p) = alpha ln p + beta," title="displaystyle ln d(p) = alpha ln p + beta," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cln+d%28p%29+%3D+%5Calpha+%5Cln+p+%2B+%5Cbeta%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
or</p>
<p><img alt="displaystyle d(p) = d_0 e^beta p^alpha = Cp^alpha," title="displaystyle d(p) = d_0 e^beta p^alpha = Cp^alpha," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%28p%29+%3D+d_0+e%5E%5Cbeta+p%5E%5Calpha+%3D+Cp%5E%5Calpha%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>where <img alt="alpha" title="alpha" src="https://s0.wp.com/latex.php?latex=%5Calpha&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is the elasticity of the demand and <img alt="C" title="C" src="https://s0.wp.com/latex.php?latex=C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is a scale factor. A much more interesting demand curve is given by the logistic/sigmoide function:</p>
<p><img alt="displaystyle d(p) = Cfrac{e^{alpha p + beta}}{1 + e^{alpha p + beta}} = frac{C}{1+e^{-alpha(p - p_0)}}," title="displaystyle d(p) = Cfrac{e^{alpha p + beta}}{1 + e^{alpha p + beta}} = frac{C}{1+e^{-alpha(p - p_0)}}," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%28p%29+%3D+C%5Cfrac%7Be%5E%7B%5Calpha+p+%2B+%5Cbeta%7D%7D%7B1+%2B+e%5E%7B%5Calpha+p+%2B+%5Cbeta%7D%7D+%3D+%5Cfrac%7BC%7D%7B1%2Be%5E%7B-%5Calpha%28p+-+p_0%29%7D%7D%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>where <img alt="C" title="C" src="https://s0.wp.com/latex.php?latex=C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is a scale factor and <img alt="alpha" title="alpha" src="https://s0.wp.com/latex.php?latex=%5Calpha&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> measures price sensitivity. We also can observe <img alt="p_0 = -alpha/beta" title="p_0 = -alpha/beta" src="https://s0.wp.com/latex.php?latex=p_0+%3D+-%5Calpha%2F%5Cbeta&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> as the inflection point of the demand.</p>
<p>Some books changes the signs of the coefficients using the assumption that <img alt="alpha" title="alpha" src="https://s0.wp.com/latex.php?latex=%5Calpha&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is a positive constant and using a minus sign in front of it. However, it does not change the estimation procedure or final result, it is just a matter of convenience. Here, we expect <img alt="alpha" title="alpha" src="https://s0.wp.com/latex.php?latex=%5Calpha&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> to be negative in the three models.</p>
<p>In the Figure below we can check a comparison among the shapes of the demand models:</p>
<pre>library(ggplot2)
library(reshape2)
library(magrittr)

linear = function(p, alpha, beta) alpha*p + beta
constant_elast = function(p, alpha, beta) exp(alpha*log(p)+beta)
logistic = function(p, c, alpha, p0) c/(1+exp(-alpha*(p-p0)))

p = seq(1, 100)
y1 = linear(p, -1, 100)
y2 = constant_elast(p, -.5, 4.5)
y3 = logistic(p, 100, -.2, 50)

df = data.frame('Prices' = p, 'Linear' = y1, 'Constant_elast' = y2, 'Logistic' = y3)
df.plot = melt(df, id = 'Prices') %&amp;gt;% set_colnames(c('Prices', 'Model', 'Demand'))

ggplot(df.plot) + aes(x = Prices, y = Demand) +
  geom_line(color = 'blue', alpha = .6, lwd = 1) +
  facet_grid(~Model)
</pre>
<p><img alt="plot of chunk demand_models" src="https://i1.wp.com/i.imgur.com/k9uIY2F.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Of course that in practice prices does not change between 1 and 100, but the idea is to show the main differences in the shape of the models.</p>
<p>All the models presented above have positive and negative points. Although local linear approximation may be reasonable for small changes in prices, sometimes this assumption is too strong and does not capture the correct sensitivity of bigger price changes. In the constant elasticity model, even though it is a non-linear relationship between demand and price, the constant elasticity assumption might be too restrictive. Moreover, it tends to over estimate the demand for lower and bigger prices. In a fist moment, I would venture to say that the logistic function is the most robust and realistic among the three types.</p>
<h2>Pricing with demand models</h2>
<p>In a general setting, one have for the total profit function:</p>
<p><img alt="displaystyle L(p) = d(p)(p-c)," title="displaystyle L(p) = d(p)(p-c)," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+L%28p%29+%3D+d%28p%29%28p-c%29%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>where, <img alt="L" title="L" src="https://s0.wp.com/latex.php?latex=L&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> gives the profit, <img alt="d" title="d" src="https://s0.wp.com/latex.php?latex=d&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is the demand function that depends of the price and <img alt="c" title="c" src="https://s0.wp.com/latex.php?latex=c&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is the marginal cost. Taking the derivative with respect to price we have:</p>
<p><img alt="displaystyle L'(p) = d'(p)(p - c) + d(p)." title="displaystyle L'(p) = d'(p)(p - c) + d(p)." src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+L%27%28p%29+%3D+d%27%28p%29%28p+-+c%29+%2B+d%28p%29.&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Making <img alt="L'(p) = 0" title="L'(p) = 0" src="https://s0.wp.com/latex.php?latex=L%27%28p%29+%3D+0&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> to calculate the optimum price (first order condition), we have:</p>
<p><img alt="displaystyle d'(p^star)(p^star - c) + d(p^star) = 0" title="displaystyle d'(p^star)(p^star - c) + d(p^star) = 0" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%27%28p%5E%5Cstar%29%28p%5E%5Cstar+-+c%29+%2B+d%28p%5E%5Cstar%29+%3D+0&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle d'(p^star)p^star + d(p^star) = d'(p^star)c," title="displaystyle d'(p^star)p^star + d(p^star) = d'(p^star)c," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%27%28p%5E%5Cstar%29p%5E%5Cstar+%2B+d%28p%5E%5Cstar%29+%3D+d%27%28p%5E%5Cstar%29c%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>which is the famous condition that in the optimal price, marginal cost equals marginal revenue. Next, let’s see how to calculate the optimum prices for each demand functions.</p>
<h4>Linear model</h4>
<p>For the linear model <img alt="d'(p) = alpha" title="d'(p) = alpha" src="https://s0.wp.com/latex.php?latex=d%27%28p%29+%3D+%5Calpha&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. Hence:</p>
<p><img alt="displaystyle d'(p^star)p + d(p^star) = d'(p^star)c," title="displaystyle d'(p^star)p + d(p^star) = d'(p^star)c," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%27%28p%5E%5Cstar%29p+%2B+d%28p%5E%5Cstar%29+%3D+d%27%28p%5E%5Cstar%29c%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle alpha p^star + alpha p^star + beta = alpha c," title="displaystyle alpha p^star + alpha p^star + beta = alpha c," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha+p%5E%5Cstar+%2B+%5Calpha+p%5E%5Cstar+%2B+%5Cbeta+%3D+%5Calpha+c%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle p^star = frac{alpha c - beta}{2alpha}." title="displaystyle p^star = frac{alpha c - beta}{2alpha}." src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+p%5E%5Cstar+%3D+%5Cfrac%7B%5Calpha+c+-+%5Cbeta%7D%7B2%5Calpha%7D.&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Example:</p>
<pre>library(tidyverse)

# Synthetic data
p = seq(80,130)
d = linear(p, alpha = -1.5, beta = 200) + rnorm(sd = 5, length(p))
c = 75
profit = d*(p-c)

# Fit of the demand model
model1 = lm(d~p)
profit.fitted = model1$fitted.values*(p - c)

# Pricing Optimization
alpha = model1$coefficients[2]
beta = model1$coefficients[1]
p.max.profit = (alpha*c - beta)/(2*alpha)

# Plots
df.linear = data.frame('Prices' = p, 'Demand' = d,
                       'Profit.fitted' = profit.fitted, 'Profit' = profit)

ggplot(select(df.linear, Prices, Demand)) + aes(x = Prices, y = Demand) +
  geom_point() + geom_smooth(method = lm)
</pre>
<p><img alt="plot of chunk profit_linear_demand" src="https://i0.wp.com/i.imgur.com/F0V3AhN.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>ggplot(select(df.linear, Prices, Profit)) + aes(x = Prices, y = Profit) +
  geom_point() + geom_vline(xintercept = p.max.profit, lty = 2) +
  geom_line(data = df.linear, aes(x = Prices, y = Profit.fitted), color = 'blue')
</pre>
<p><img alt="plot of chunk profit_linear_demand" src="https://i2.wp.com/i.imgur.com/aTrMWOv.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h4>Constant elasticity model</h4>
<p>For the constant elasticity model, since <img alt="lim_{Delta rightarrow 0}frac{Delta D}{Delta p} = d'(p)" title="lim_{Delta rightarrow 0}frac{Delta D}{Delta p} = d'(p)" src="https://s0.wp.com/latex.php?latex=%5Clim_%7B%5CDelta+%5Crightarrow+0%7D%5Cfrac%7B%5CDelta+D%7D%7B%5CDelta+p%7D+%3D+d%27%28p%29&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">, we have that:</p>
<p><img alt="displaystyle epsilon = frac{%D}{%p} = frac{pDelta D}{DDelta p} = -frac{d'(p)p}{D}." title="displaystyle epsilon = frac{%D}{%p} = frac{pDelta D}{DDelta p} = -frac{d'(p)p}{D}." src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cepsilon+%3D+%5Cfrac%7B%5C%25D%7D%7B%5C%25p%7D+%3D+%5Cfrac%7Bp%5CDelta+D%7D%7BD%5CDelta+p%7D+%3D+-%5Cfrac%7Bd%27%28p%29p%7D%7BD%7D.&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Therefore,</p>
<p><img alt="displaystyle d'(p^star)p^star + d(p^star) = d'(p^star)c," title="displaystyle d'(p^star)p^star + d(p^star) = d'(p^star)c," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%27%28p%5E%5Cstar%29p%5E%5Cstar+%2B+d%28p%5E%5Cstar%29+%3D+d%27%28p%5E%5Cstar%29c%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle frac{d'(p^star)p^star}{d(p^star)} + 1 = frac{d'(p^star)c}{d(p^star)}," title="displaystyle frac{d'(p^star)p^star}{d(p^star)} + 1 = frac{d'(p^star)c}{d(p^star)}," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Bd%27%28p%5E%5Cstar%29p%5E%5Cstar%7D%7Bd%28p%5E%5Cstar%29%7D+%2B+1+%3D+%5Cfrac%7Bd%27%28p%5E%5Cstar%29c%7D%7Bd%28p%5E%5Cstar%29%7D%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle -epsilon + 1 = epsilon frac{c}{p^star}," title="displaystyle -epsilon + 1 = epsilon frac{c}{p^star}," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+-%5Cepsilon+%2B+1+%3D+%5Cepsilon+%5Cfrac%7Bc%7D%7Bp%5E%5Cstar%7D%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle p^star = frac{epsilon c}{1-epsilon} = frac{c}{1-1/epsilon}." title="displaystyle p^star = frac{epsilon c}{1-epsilon} = frac{c}{1-1/epsilon}." src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+p%5E%5Cstar+%3D+%5Cfrac%7B%5Cepsilon+c%7D%7B1-%5Cepsilon%7D+%3D+%5Cfrac%7Bc%7D%7B1-1%2F%5Cepsilon%7D.&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Moreover, knowing that <img alt="frac{%D}{%p} sim frac{Delta ln D}{Delta ln p}" title="frac{%D}{%p} sim frac{Delta ln D}{Delta ln p}" src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5C%25D%7D%7B%5C%25p%7D+%5Csim+%5Cfrac%7B%5CDelta+%5Cln+D%7D%7B%5CDelta+%5Cln+p%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and using the constant elasticity model, we have that:</p>
<p><img alt="displaystyle epsilon sim lim_{Delta rightarrow0} frac{Delta ln D}{Delta ln P} = frac{dln D}{dln p} = alpha." title="displaystyle epsilon sim lim_{Delta rightarrow0} frac{Delta ln D}{Delta ln P} = frac{dln D}{dln p} = alpha." src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cepsilon+%5Csim+%5Clim_%7B%5CDelta+%5Crightarrow0%7D+%5Cfrac%7B%5CDelta+%5Cln+D%7D%7B%5CDelta+%5Cln+P%7D+%3D+%5Cfrac%7Bd%5Cln+D%7D%7Bd%5Cln+p%7D+%3D+%5Calpha.&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Thus, we can calculate the optimum profit price for the constant elasticity model as:</p>
<p><img alt="displaystyle p^star = frac{c}{1 - frac{1}{|alpha|}}" title="displaystyle p^star = frac{c}{1 - frac{1}{|alpha|}}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+p%5E%5Cstar+%3D+%5Cfrac%7Bc%7D%7B1+-+%5Cfrac%7B1%7D%7B%7C%5Calpha%7C%7D%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>It is interesting to note that one needs <img alt="|alpha| &gt; 1" title="|alpha| &gt; 1" src="https://s0.wp.com/latex.php?latex=%7C%5Calpha%7C+%3E+1&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">, otherwise the profit function will be convex with respect to price and the optimal price will be <img alt="infty" title="infty" src="https://s0.wp.com/latex.php?latex=%5Cinfty&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. If one have a monopolistic market, normally this assumption holds.</p>
<p>Example:</p>
<pre># Synthetic data
p = seq(80,130)
d = constant_elast(p, alpha = -3, beta = 15)*exp(rnorm(sd = .15, length(p)))
c = 75
profit = d*(p-c)

# Fitting of demand model
model2 = lm(log(d)~log(p))
profit.fitted = exp(model2$fitted.values)*(p - c)

# pricing optimization
alpha = model2$coefficients[2]
p.max.profit = c/(1-1/abs(alpha))

# Plots
df.const_elast = data.frame('Prices' = p, 'Demand' = d,
                       'Profit.fitted' = profit.fitted, 'Profit' = profit)

ggplot(select(df.const_elast, Prices, Demand)) + aes(x = log(Prices), y = log(Demand)) +
  geom_point() + geom_smooth(method = lm)
</pre>
<p><img alt="plot of chunk profit_constant_elastc" src="https://i2.wp.com/i.imgur.com/OZ37tnS.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>ggplot(select(df.const_elast, Prices, Profit)) + aes(x = Prices, y = Profit) +
  geom_point() + geom_vline(xintercept = p.max.profit, lty = 2) +
  geom_line(data = df.const_elast, aes(x = Prices, y = Profit.fitted), color = 'blue')
</pre>
<p><img alt="plot of chunk profit_constant_elastc" src="https://i0.wp.com/i.imgur.com/AQ2RQJz.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h4>Logistic model</h4>
<p>For the logistic function, one can check that <img alt="d'(p) = alpha d(p)(1-d(p)/C)" title="d'(p) = alpha d(p)(1-d(p)/C)" src="https://s0.wp.com/latex.php?latex=d%27%28p%29+%3D+%5Calpha+d%28p%29%281-d%28p%29%2FC%29&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. Thus:</p>
<p><img alt="displaystyle d'(p^star)(p^star - c) + d(p^star) = 0," title="displaystyle d'(p^star)(p^star - c) + d(p^star) = 0," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%27%28p%5E%5Cstar%29%28p%5E%5Cstar+-+c%29+%2B+d%28p%5E%5Cstar%29+%3D+0%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle alpha d(p^star)(1-d(p^star)/C)(p^star-c) + d(p^star) = 0," title="displaystyle alpha d(p^star)(1-d(p^star)/C)(p^star-c) + d(p^star) = 0," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha+d%28p%5E%5Cstar%29%281-d%28p%5E%5Cstar%29%2FC%29%28p%5E%5Cstar-c%29+%2B+d%28p%5E%5Cstar%29+%3D+0%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle alpha(1-d(p^star)/C)(p^star-c) + 1 = 0," title="displaystyle alpha(1-d(p^star)/C)(p^star-c) + 1 = 0," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha%281-d%28p%5E%5Cstar%29%2FC%29%28p%5E%5Cstar-c%29+%2B+1+%3D+0%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle frac{alpha e^{-alpha(p^star - p_0)}(p^star - c) + 1+ e^{-alpha(p^star - p_0)}}{1+ e^{-alpha(p^star - p_0)}} = 0," title="displaystyle frac{alpha e^{-alpha(p^star - p_0)}(p^star - c) + 1+ e^{-alpha(p^star - p_0)}}{1+ e^{-alpha(p^star - p_0)}} = 0," src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7B%5Calpha+e%5E%7B-%5Calpha%28p%5E%5Cstar+-+p_0%29%7D%28p%5E%5Cstar+-+c%29+%2B+1%2B+e%5E%7B-%5Calpha%28p%5E%5Cstar+-+p_0%29%7D%7D%7B1%2B+e%5E%7B-%5Calpha%28p%5E%5Cstar+-+p_0%29%7D%7D+%3D+0%2C&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle alpha(p^star-c)+1]e^{-alpha(p^star - p_0)} + 1 = 0." title="displaystyle alpha(p^star-c)+1]e^{-alpha(p^star - p_0)} + 1 = 0." src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha%28p%5E%5Cstar-c%29%2B1%5De%5E%7B-%5Calpha%28p%5E%5Cstar+-+p_0%29%7D+%2B+1+%3D+0.&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Since the last equation above does not have an analytical solution (at least we couldn’t solve it), one can easily find the result with a newton-step algorithm or minimization problem. We will use the second approach with the following formulation:</p>
<p><img alt="displaystyle min_{p in mathbb{R}} big{(}[alpha(p-c)+1]e^{-alpha(p - p_0)} + 1big{)}^2" title="displaystyle min_{p in mathbb{R}} big{(}[alpha(p-c)+1]e^{-alpha(p - p_0)} + 1big{)}^2" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cmin_%7Bp+%5Cin+%5Cmathbb%7BR%7D%7D+%5Cbig%7B%28%7D%5B%5Calpha%28p-c%29%2B1%5De%5E%7B-%5Calpha%28p+-+p_0%29%7D+%2B+1%5Cbig%7B%29%7D%5E2&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Example:</p>
<pre># Objective functions for optimization
demand_objective = function(par, p, d) sum((d - logistic(p, par[1], par[2], par[3]))^2)
price_objective = function(p, alpha, c, p0) (exp(-alpha*(p-p0))*(alpha*(p-c)+1) + 1)^2 

# A cleaner alternative for pricing optimization is to min:
price_objective2 = function(p, c, alpha, C, p0) -logistic(p, C, alpha, p0)*(p-c)

# synthetic data
p = seq(80,130)
c = 75
d = logistic(p, 120, -.15, 115) + rnorm(sd = 10, length(p))
profit = d*(p-c)

# Demand fitting, we can't use lm anymore
par.start = c(max(d), 0, mean(d)) # initial guess

demand_fit = optim(par = par.start, fn = demand_objective, method = 'BFGS',
                   p = p, d = d)

par = demand_fit$par # estimated parameters for demand function
demand.fitted = logistic(p, c = par[1], alpha = par[2], p0 = par[3])
profit.fitted = demand.fitted*(p - c)

# Pricing Optimization, we don't have a closed expression anymore
price_fit = optim(mean(p), price_objective, method = 'BFGS',
                  alpha = par[2], c = c, p0 = par[3])

# or

price_fit2 = optim(mean(p), price_objective2, method = 'BFGS',
                  c = c, C = par[1], alpha = par[2], p0 = par[3]) 

# both results are almost identical
p.max.profit = price_fit$par

# Graphics
df.logistic = data.frame('Prices' = p, 'Demand' = d, 'Demand.fitted' = demand.fitted,
                       'Profit.fitted' = profit.fitted, 'Profit' = profit)

ggplot(select(df.logistic, Prices, Demand)) + aes(x = Prices, y = Demand) +
  geom_point() +
  geom_line(data = df.logistic, aes(x = Prices, y = Demand.fitted), color = 'blue')
</pre>
<p><img alt="plot of chunk profit_logistic_demand" src="https://i0.wp.com/i.imgur.com/EUfnB1a.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>ggplot(select(df.logistic, Prices, Profit)) + aes(x = Prices, y = Profit) +
  geom_point() + geom_vline(xintercept = p.max.profit, lty = 2) +
  geom_line(data = df.logistic, aes(x = Prices, y = Profit.fitted), color = 'blue')
</pre>
<p><img alt="plot of chunk profit_logistic_demand" src="https://i1.wp.com/i.imgur.com/745EeA1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I hope you liked the examples. In the next post we will discuss about choice models, which are demand models when products are heterogeneous. Goodbye and good luck!</p>
<h2>References</h2>
<p>Phillips, Robert Lewis. Pricing and revenue optimization. Stanford University Press, 2005.</p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://insightr.wordpress.com/2018/06/03/different-demand-functions-and-optimal-price-estimation-in-r/"> R – insightR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/CPlOkW3j6S8" height="1"></a></div>