demand_source Module¶
Overview¶
This module contains the DemandSource class. A DemandSource
object represents external demand observed by a node.
The demand can be random or deterministic. Attributes specify the type of demand
distribution and its parameters. The object can generate demands from the specified distribution.
Note
The notation and references (equations, sections, examples, etc.) used below refer to Snyder and Shen, Fundamentals of Supply Chain Theory (FoSCT), 2nd edition (2019).
Example: Create a DemandSource object representing demand that has a normal
distribution with a mean of 50 and a standard deviation of 10. Generate a random demand from the distribution.
>>> ds = DemandSource(type='N', mean=50, standard_deviation=10) >>> ds.generate_demand() 46.75370030596123 >>> # Tell object to round demands to integers. >>> ds.round_to_int = True >>> ds.generate_demand() 63
API Reference¶
- class DemandSource(**kwargs)[source]¶
A
DemandSourceobject represents external demand observed by a node. The demand can be random or deterministic. Attributes specify the type of demand distribution and its parameters. The object can generate demands from the specified distribution.- Parameters
**kwargs – Keyword arguments specifying values of one or more attributes of the
DemandSource, e.g.,type='N'.
- type¶
The demand type, as a string. Currently supported strings are:
None
‘N’ (normal)
‘P’ (Poisson)
‘UD’ (uniform discrete)
‘UC’ (uniform continuous)
‘NB’ (negative binomial)
‘D’ (deterministic)
‘CD’ (custom discrete)
- Type
str
- round_to_int¶
Round demand to nearest integer?
- Type
bool
- demand_list¶
List of demands, one per period (for deterministic demand types), or list of possible demand values (for custom discrete demand types). For deterministic demand types, if demand is required in a period beyond the length of the list, the list is restarted at the beginning. This also allows
demand_listto be a singleton, in which case it is used in every period. Required iftype== ‘D’ or ‘CD’. [\(d\)]- Type
list, optional
- probabilities¶
List of probabilities of each demand value (for custom discrete demand types). Required if
type== ‘CD’.- Type
list, optional
- lo¶
Low value of demand range (for uniform demand types). Required if
type== ‘UD’ or ‘UC’.- Type
float, optional
- hi¶
High value of demand range (for uniform demand types). Required if
type== ‘UD’ or ‘UC’.- Type
float, optional
- n¶
Parameter for negative binomial distribution indicating number of trial successes. Required if
type== ‘NB’. [\(n\)]- Type
int, optional
- p¶
Parameter for negative binomial distribution indicating probability of success for each trial. Required if
type== ‘NB’. [\(p\)]- Type
float, optional
- property mean¶
Return mean set by user, if any; or, for distributions whose mean is not set but is calculated from other parameters, returns the calculated mean. If neither is true, return
None.
- property standard_deviation¶
Return standard deviation set by user, if any; or, for distributions whose standard deviation is not set but is calculated from other parameters, returns the calculated standard deviation. If neither is true, return
None.
- property demand_distribution¶
Demand distribution, as a
scipy.stats.rv_continuousorscipy.stats.rv_discreteobject. ReturnsNoneif demand sourcetypeis'D'. Read only.
- property is_discrete¶
Trueif the distribution is discrete,Falseif it is continuous. Read only.The distribution is discrete if
self.typeis ‘P’, ‘UD’, ‘CD’, ‘NB’, or ‘D’.- Returns
Trueif the distribution is discrete,Falseif it is continuous.- Return type
bool
- validate_parameters()[source]¶
Check that appropriate parameters have been provided for the given demand type. Raise an exception if not.
- to_dict()[source]¶
Convert the
DemandSourceobject to a dict. List attributes (demand_list,probabilities) are deep-copied so changes to the original object do not get propagated to the dict.- Returns
The dict representation of the object.
- Return type
dict
- classmethod from_dict(the_dict)[source]¶
Return a new
DemandSourceobject with attributes copied from the values inthe_dict. List attributes (demand_list,probabilities) are deep-copied so changes to the original dict do not get propagated to the object. Any missing attributes are set to their default values.- Parameters
the_dict (dict) – Dict representation of a
DemandSource, typically created usingto_dict().- Returns
The object converted from the dict.
- Return type
- generate_demand(period=None)[source]¶
Generate a demand value using the demand type specified in
type. IftypeisNone, returnsNone.- Parameters
period (int, optional) – The period to generate a demand value for. If
type= ‘D’ (deterministic), this is required ifdemand_listis a list of demands, one per period. If omitted, will return first (or only) demand in list.- Returns
demand – The demand value.
- Return type
float
- cdf(x)[source]¶
Cumulative distribution function of demand distribution.
In some cases, this is just a wrapper around
cdf()function ofscipy.stats.rv_continuousorscipy.stats.rv_discreteobject.- Parameters
x (float) – Value to calculate cdf for.
- Returns
F – cdf of
x.- Return type
float
- lead_time_demand_distribution(lead_time)[source]¶
Return lead-time demand distribution, as a
scipy.stats.rv_continuousorscipy.stats.rv_discreteobject.Note
If
lead_timeequals 0, this method returns anrv_discreteobject with a single value (0) and a single probability (1).Note
For ‘UC’, ‘UD’, ‘NB’, and ‘CD’ demands, this method calculates the lead-time demand distribution as the sum of
lead_timeindependent random variables. Therefore, the method requireslead_timeto be an integer for these distributions. If it is not, it raises aValueError.- Parameters
lead_time (float or int) – The lead time. [\(L\)]
- Returns
distribution – The lead-time demand distribution object.
- Return type
rv_continuous or rv_discrete
- Raises
ValueError – If
typeis ‘UC’, ‘UD’, ‘NB’, or ‘CD’ andlead_timeis not an integer.