Scarcity vs. excess is a constant challenge for business owners. Having the right number of whatever you are selling, to meet customer need (whether you’re selling something tangible like cell phones, or a less tangible item, like consulting hours) can be a puzzle. If you have too many, your product may go to waste. If you have too few, you are probably missing an opportunity.

So, how do you determine the correct number of products or amount of anything else to have on hand? As an example, let’s say that your company sells sandwiches from a food cart. The first step is to estimate the number of sandwiches you will sell each day. If you have historic information about the number of sandwiches you have sold, your estimate will be useful. If not, use your best estimate, but also collect data going forward.

To do this, the simplest approach is to come up with the total number of sandwiches you sold over the past year on days when you didn’t run out of sandwiches and divide that number by the number of days that you didn’t run out of product.

This will give you an estimate of the number of sandwiches you sell daily. Consider only days when you didn’t run out, because on days when you did, you won’t actually know how many sandwiches you might have sold.

Once you have this simple point forecast, consider additional factors such as:

Trends — If your business is growing, you will want to forecast sales that are above the average for the past year. Similarly, if sales are declining, you will need to forecast below the average for the past year.

Seasonality — You may find that you sell more sandwiches when children are in school, for instance, because fewer people are on vacation.

Day of the week — It may be that you sell fewer sandwiches on Fridays than other days, because people are taking long weekends.

Weather — The number of sandwiches people purchase may change with temperature and/or precipitation.

There are quantitative techniques that can help with demand forecasting, but applying good business judgment is always important. Once you understand the impact of factors such as the ones described above, you can adjust the current day’s forecast up or down, as appropriate. The result will be your best estimate of the number of sandwiches you will sell on any given day.

Related: Intuit Launches New Online QuickBooks App for Windows

The cost of overage, or surplus (having to throw out a sandwich, for example), is the money you spent to make each sandwich. The opposite cost (a lost sale) is the profit you lose when you miss a sale because you ran out of product. Note that the cost of this loss will always be the price minus the cost of overage.

We’ll assume that the actual demand for your sandwiches is equally likely to be above or below your point estimate. In this case, if the cost of surplus is equal to the cost of shortage, you should simply make the number of sandwiches equal to your point estimate.

If the cost of overage is greater than the cost of loss, you’ll want to make fewer sandwiches than your point estimate. On the other hand, if the cost of overage is less than the cost of loss, you’ll want to make more sandwiches than your point estimate. This makes sense because you want to avoid the more costly consequence.

You can use the formula below to optimize the quantity of sandwiches you should make. Admittedly, this is somewhat of an oversimplification, but if we attempted to be more precise, the math gets very complex.

Optimal number = point estimate X (1 – (cost of overage – cost of loss) / sandwich price)

Therefore, if the point estimate is 426 sandwiches, the cost of overage is $1.00, the cost of loss is $3.50, and a sandwich sells for $4.50, the optimal number of sandwiches to make is 663.

Admittedly, most of the time you will be throwing sandwiches out, but that will cost you only $1.00 per sandwich. Each unit of lost sales because you ran out will cost you three-and-a-half times that much. Therefore, it makes sense that you would want to have a few extra sandwiches. Finally, in deciding how many sandwiches to make, you may want to consider longer-term implications. For example, regularly running out of sandwiches may drive customers away.

Whether you are selling a product (sandwiches, cell phones or doorknobs) or service hours, determining how much to have available will be critical to your businesses success. You will want to use good business judgment when adjusting quantities due to various factors. As is nearly always the case, analysis can help you understand the situation, but almost every business decision has an element of judgment.