# How can I force my model to predict the samples that are close to zero?

I have a large amount of inventory data and I am trying to predict when the inventory gets low using one component of the change in inventory (yes I know this doesn't describe inventory very well by itself). It seems that the tendency of models is to try to predict around the mean of the data and reduce local variance (like variance on a daily scale versus yearly scale). This daily variance is what I care about though. Is there a certain loss function or method I can use to focus my predictions on these values that are near zero?

• You are going to have to be more specific. Do you have sample data that we could look at? What do you want your response variable to be? The amount of time until the inventory is low?
– Eric
Aug 17, 2015 at 20:45
• I am using past sales data to predict the next days inventory, but what I actually care about is low inventory. My dependent variables are whatever manipulations of sales I can imagine might be relevant (finite difference, various sums). Someone might have a more creative way of looking at this, but I just want to be able to train a model with a focus on the inventory data 'close' to zero. Aug 17, 2015 at 22:37
• Your question is still very unclear. Please do what you can to explain what you're doing (including what your data are) and what you want to achieve. You should aim to edit your question so that people don't need to read comments to figure out what you want. Aug 18, 2015 at 1:52
• I agree that some more information could help. In a first and crude try, you could howecer define "low" inventory as a dummy and see what predicts this variable in a simple random effects discrete choice model. You could play around with lags and forwards (maybe seasonal effects?) to see what happens. Again, this is a very crude way of dealing with the issue. I advise you to post at least a snippet of your data (or structure). Aug 18, 2015 at 7:43
• I apologize for the difficulty interpreting what I am looking for, I wasn't entirely sure myself. From what I can tell though the two answers given were almost perfect. Is there any way to implement these using Python's sklearn? I've looked around a little, but I'm not sure if there is a method I can use with a custom loss function. I could just code up my own linear regression if I don't have any other options. Aug 19, 2015 at 2:09

I'm guessing that your current loss function is quadratic error? Actual inventory $a$ minus estimated inventory $e$ squared? So your loss is $L=(a-e)^2$? Plot that for values of $a$ and $e$. Your question basically says that you're not happy with the loss function. You want higher loss when $a$ is low.

\begin{align} L = \frac{(a-e)^2}{1+a} \end{align}

This corresponds to a reweighting scheme as described by @fcoppens.

However, modifying the loss is more general than just running least squares with reweighting. You could also have, for example

\begin{align} L = \frac{e}{a} - \log e \end{align}

This is also much stronger as $a \to 0$, (I felt it might be too strong). This corresponds to an exponential model.

Maybe the best of both worlds is: \begin{align} L = \frac{e+1}{a+1} - \log (e+1) \end{align}

You probably want higher loss when $e$ is low too since that indicates a false positive "low inventory warning", but you don't mention this in your question.

• it's more than 'corresponds' I think; OLS has a quadratic loss function (which is your numerator) and you have choosen specific weights for my weight vector $w$ namely inversely proportional to (1+inventory), so it's more like 'a special case of' what I propose, with particular weights.
– user83346
Aug 18, 2015 at 9:06
• @fcoppens I said "a reweighting scheme", as in one of many posssible ways of reweighting. However, generally modifying the loss is more than general than just running least squares with reweighting. I'll explain further in my answer. Aug 18, 2015 at 9:09