# 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.

Also how do I begin learning about this topic being a non-statistician?

I am sorry for asking too many questions. I am at the edge of sanity. Please help me with this.

## 1 Answer

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.

Also how do I begin learning about this topic being a non-statistician?

Do you mean nonlinear least squares, or finding/fitting peaks, or modelling liquidity?

• Thanks for the reply. I will start looking at the various models suggested above. Each individual term explain effect of it on the dependent variable. Like Age and osd. How do I model the combined interaction of age and osd on MS? Separate comments about other points next. – kishore Sep 6 '13 at 8:44
• Regarding nls, I tried various combinations provided by nls without any luck. Since I don't know in which class the above model belongs to, I am not able to use any SS* function, let alone write one. By your probing questions above, I have a long long way to go. How do I begin here? I want to accomplish the following: Fitting curve for the actual data (Identifying the model), Estimate the parameters, justify the appropriateness of the model. If there are explicit models for Liquidity, it will make my life a lot easier as they will be my starting point. I sincerely request you to guide me pls. – kishore Sep 6 '13 at 8:50
• "I sincerely request you to guide me pls." -- Why would an explicit model of that curve (rather than say of the frequency and size of the bumps) be important? – Glen_b Sep 6 '13 at 8:54
• the model may give me an idea about at what age the ms of the trading is peaking? Once it does that how fast the trading seems to fall. Since the next security is to be offered, it can help design the offerings at a minimum? You had a bunch of points about the steps around fitting like derivative function, kernel/logspline estimate et al. Where do I get information about how to perform these steps? – kishore Sep 6 '13 at 9:15
• Doesn't that suggest that your interest is mainly in identifying the locations, size and widths of the peaks? While that can be done with nonlinear least squares, it's not the only way to do it. Computing derivatives would come from a basic course on calculus. Kernel density estimates? Maybe The Elements of Statistical Learning Logspline estimates? There's a little on it in Modern Applied Statistics with S – Glen_b Sep 6 '13 at 9:35