# How do I incorporate 2 peaks in a liquidity model using curve-fitting and nls

This is my first time at stack exchange, hence please pardon me if I miss something. I have multiple questions and I am tearing myself as curve-fitting and estimation is foreign land to me, I am ready to learn given directions and ideas.

I am trying to model liquidity (ease of trading) of a security. In my case the security has very sparse trading. Once it is issued, trading is quite active and dies out then. After some years it may again have some hectic trading and becomes inactive for the rest of its life. Typically trading marketshare of the security depends on its age and outstanding amount. A sample datafile can be found here. In the data file ms_vol is the marketshare based on volume, ms_trds is the marketshare based on number of trades, osd is the outstanding amount and age is the age of the security. A pdf of ms_vol vs age plot can be found here.

A research note has modeled such relationship as

$$MS = \beta_1 \exp[-\beta_2 (Age-\beta_3)^2] + \beta_4 \beta_5^{Age}$$

All the $\beta$'s are > 0. This models only one hump, not two. I tried to use nls in R unsuccessfully with the following code and error message

dfX = read.csv("trading.csv")
mod_msV = nls(
ms_vol ~ beta1*exp(-beta2*(age-beta3)^2) + beta4*beta5^age,
start=list(beta1=0.3, beta2=0.83, beta3=0.55, beta4=0.5, beta5=0.5),
data=dfX, trace=T)

29310.88 :  0.30 0.83 0.55 0.50 0.50
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model


Following are my questions:

1. How do I extend the above model to incorporate two humps which may occur some age apart?

2. The above model has only one independent variable, age. In my case osd is another variable I would like to incorporate. ms_vol behaves with osd just like age, though it may not show 2 humps.

3. How do I estimate teh parameters? The error message by nls baffles me completely. My hunch is it may be because of the starting values. I have tried several sets but all of them lead to same error message.

4. Somewhere I read about "Flowering Curve" and it may be a curve of choice. I have tried unsuccessfully locating info on it, but without success. Is there any other curve I can try out.

How do I extend the above model to incorporate two humps which may occur some age apart?

If you want a similar functional description of the peak, $MS = \beta_1 \exp[-\beta_2 (Age-\beta_3)^2] + \beta_4 \exp[-\beta_5 (Age-\beta_6)^2] + \beta_7 \beta_8^{Age}$

The above model has only one independent variable, age. In my case osd is another variable I would like to incorporate. ms_vol behaves with osd just like age, though it may not show 2 humps.

$MS = \beta_1 \exp[-\beta_2 (\text{Age}-\beta_3)^2] + \beta_4 \exp[-\beta_5 (\text{Age}-\beta_6)^2] + \beta_7 \beta_8^{\text{Age}}\\ +\beta_4 \exp[-\beta_5 (\text{osd}-\beta_6)^2] + \beta_7 \beta_8^{\text{osd}}$

How do I estimate the parameters? The error message by nls baffles me completely. My hunch is it may be because of the starting values. I have tried several sets but all of them lead to same error message.

What does the initial curve look like (plotted over the data)? Have you tried supplying a derivative function? Have you tried anything other than the default optimization method? Have you tried something like a kernel or logspline esimate of the function to get a sense of what your fit should look like?

Somewhere I read about "Flowering Curve" and it may be a curve of choice.

While I had never heard of it, it wasn't hard to find a bunch of links just by typing the term into google

eg 1

As far as I can discern from reading a handful of such hits, the requirement is to fit s variety of peaked shapes (flowering curves differ across species), and so 'flowering curve' seems to be in this sense pretty much a synonym for a class of 'smooth unimodal bump'-type functions.