示例#1
0
def get_generalized_distribution(self,
                                 params,
                                 scale=1,
                                 exog=None,
                                 exposure=None,
                                 offset=None):
    """
	Returns a random number generator for the predictive distribution.
	Parameters
	----------
	params : array-like
		The model parameters.
	scale : scalar
		The scale parameter.
	exog : array-like
		The predictor variable matrix.
	Returns a frozen random number generator object.  Use the
	``rvs`` method to generate random values.
	Notes
	-----
	Due to the behavior of ``scipy.stats.distributions objects``,
	the returned random number generator must be called with
	``gen.rvs(n)`` where ``n`` is the number of observations in
	the data set used to fit the model.  If any other value is
	used for ``n``, misleading results will be produced.
	"""

    fit = self.predict(params, exog, exposure, offset, linear=False)

    import scipy.stats.distributions as dist

    if isinstance(self.family, families.Gaussian):
        return dist.norm(loc=fit, scale=np.sqrt(scale))

    elif isinstance(self.family, families.Binomial):
        return dist.binom(n=1, p=fit)

    elif isinstance(self.family, families.Poisson):
        return dist.poisson(mu=fit)

    elif isinstance(self.family, families.Gamma):
        alpha = fit / float(scale)
        return dist.gamma(alpha, scale=scale)

    else:
        raise ValueError(
            "get_generalized_distribution not implemented for %s" %
            self.family.name)
示例#2
0
文件: grow.py 项目: zjminglove/pcd
    def add(self):
        g = self.g
        rng = self.rng
        n = len(g)
        g.add_node(n)

        # Random base of new attachment
        n1 = random.randint(0, len(g)-2)  # exclude endpoint and n
        #print 'graph status:', len(g), g.number_of_edges(), n, n1#, g.nodes()
        g.add_edge(n, n1)

        inner_nodes = set((n, n1))
        shell_nodes = inner_nodes
        for dist, n_at_dist in enumerate(self.ns):
            if self.pois and n_at_dist>0:
                n_at_dist = poisson(n_at_dist).rvs()

            dist += 1  # dist starts from 1, not zero
            nodes_available = set.union(*(set(g.neighbors(_))
                                          for _ in shell_nodes))
            #print '  nodes_available:', nodes_available
            #print '  inner_nodes:', inner_nodes
            shell_nodes = nodes_available - inner_nodes
            #print '  shell_nodes:', shell_nodes
            #print ' ', dist, n_at_dist, len(inner_nodes), len(nodes_available)
            if len(shell_nodes) == 0:
                # We can never add any more from now on.
                break

            n_at_dist = min(n_at_dist, len(shell_nodes))
            if n_at_dist == 0:
                inner_nodes |= shell_nodes
                continue

            new_edges = rng.sample(shell_nodes, n_at_dist)
            #print '  new edges:', new_edges, len(shell_nodes)
            for n2 in new_edges:
                #print '    linking to:', n2
                if n2 == n or n2 == n1: raise
                assert not g.has_edge(n, n2)
                g.add_edge(n, n2)

            inner_nodes |= nodes_available
示例#3
0
文件: grow.py 项目: zjminglove/pcd
    def add(self):
        g = self.g
        rng = self.rng
        n = len(g)

        nodes_linked = set()
        neigh_counts = collections.defaultdict(int)

        g.add_node(n)
        nodes_linked.add(n)

        # First link
        n1 = random.choice(xrange(n))
        assert not g.has_edge(n, n1)
        g.add_edge(n, n1)
        nodes_linked.add(n1)
        #print "adding", n, n1

        # Compute neighbor counts

        for neigh in g.neighbors(n1):
            neigh_counts[neigh] += 1

        def pick_next():
            counts = collections.defaultdict(list)
            for n, count in neigh_counts.iteritems():
                if n in nodes_linked:
                    continue
                counts[count].append(n)
            max_count = max(counts)
            node = random.choice(counts[max_count])
            #print "node %s has %d triads (%s) (%s)"%(
            #    node, max_count,
            #    counts[max_count],
            #    sorted((c, len(ns)) for c,ns in counts.iteritems()))
            return node
        def pick_T():
            itemsweights = [ ]
            for n, count in neigh_counts.iteritems():
                if n in nodes_linked:
                    continue
                try:
                    weight = exp(count / float(self.T) )
                except OverflowError:
                    weight = 1e200
                itemsweights.append((n, weight))
            chooser = pcd.util.WeightedChoice(itemsweights)
            #if len(itemsweights) > 1 \
            #       and any(_>1 for _ in neigh_counts.values()) \
            #       and len(set(_ for x,_ in itemsweights)) > 1:
            #    #raise
            #    pass
            return chooser.choice()
        def pick_TN():
            counts = collections.defaultdict(list)
            for n, count in neigh_counts.iteritems():
                if n in nodes_linked:
                    continue
                counts[count].append(n)
            print [(count, len(x)) for count,x in sorted(counts.iteritems())]
            itemsweights = [ ]
            for count, ncounts in counts.iteritems():
                weight = exp(count / float(self.T) )
                itemsweights.append((count, weight))
            chooser = pcd.util.WeightedChoice(itemsweights)
            count = chooser.choice()
            return random.choice(counts[count])

        if self.mode == 'T':
            pick_next = pick_T
        elif self.mode == 'TN':
            pick_next = pick_TN
        elif self.mode is None:
            pass
        else:
            raise ValueError


        # How many extra edges should we add?
        if self.mmean > self.m:
            from scipy.stats.distributions import poisson
            pgen = poisson(self.mmean-self.m)
            m = pgen.rvs() + self.mmean
        else:
            m = self.m
        m = min(m, len(g)-1)
        # Add additional edges
        for i in range(m-1):
            if rng.uniform(0,1) <= self.p:
                # Add triadic closure edge
                n1 = pick_next()
                #if n1 is None:
                #    continue
                for neigh in g.neighbors(n1):
                    neigh_counts[neigh] += 1
            else:
                while True:
                    n1 = random.randint(0, len(g)-2)  # exclude endpoint and n
                    if n1 not in nodes_linked:
                        break

            assert not g.has_edge(n, n1)
            g.add_edge(n, n1)
            #print "adding", n, n1
            nodes_linked.add(n1)
示例#4
0
文件: grow.py 项目: rkdarst/pcd
            return random.choice(counts[count])

        if self.mode == 'T':
            pick_next = pick_T
        elif self.mode == 'TN':
            pick_next = pick_TN
        elif self.mode is None:
            pass
        else:
            raise ValueError


        # How many extra edges should we add?
        if self.mmean > self.m:
            from scipy.stats.distributions import poisson
            pgen = poisson(self.mmean-self.m)
            m = pgen.rvs() + self.mmean
        else:
            m = self.m
        m = min(m, len(g)-1)
        # Add additional edges
        for i in range(m-1):
            if rng.uniform(0,1) <= self.p:
                # Add triadic closure edge
                n1 = pick_next()
                #if n1 is None:
                #    continue
                for neigh in g.neighbors(n1):
                    neigh_counts[neigh] += 1
            else:
                while True:
if __name__ == "__main__":

    print(os.cpu_count())

    volfractions = norm(loc=0.3, scale=0.05).ppf(lhs(50, samples=1)).reshape(
        50,
    )  #norm(loc=0.3, scale=0.1).ppf(lhs(50, samples=1)).reshape(50,)  # this gives the x values having y-values equal to volume_fraction
    # rmins_1 = uniform(1.1, 2).ppf(lhs(50, samples=1)).reshape(50,) #  0.1 < volume fraction < 0.4 => 1.1 < Rmin < 3 ; Rmin suit la loi uniforme (1.1, 3 )
    # rmins_2 = uniform(2, 3).ppf(lhs(50, samples=1)).reshape(50,) #  0.4 < volume fraction < 0.6 => 2 < Rmin < 3 ; Rmin suit la loi uniforme (2, 3 )
    # rmins_3 = uniform(3, 4).ppf(lhs(50, samples=1)).reshape(50,) # volume fraction > 0.6 => 3 < Rmin < 4; Rmin suit la loi uniforme (3, 4 )
    filters = bernoulli(0.5).ppf(lhs(50, samples=1)).reshape(
        50, )  # either present (1) or absent (0)
    tetas = uniform(0, 180).ppf(lhs(100, samples=1)).reshape(100, ).tolist(
    )  #uniform(0, 60).ppf(lhs(30, samples=1)).reshape(30,).tolist()+  uniform(60, 130).ppf(lhs(30, samples=1)).reshape(30,).tolist() + uniform(130, 180).ppf(lhs(30, samples=1)).reshape(30,).tolist()
    nbr_loads = poisson(2).ppf(lhs(50, samples=1)).reshape(
        50, )  # most probable nbr_loads is 2
    windows = poisson(100).ppf(lhs(50, samples=1)).reshape(50, )
    nx = 100
    ny = 100
    window = int(nx / 2)  #
    possible_fixed_nodes = np.arange(0, ny + 1).tolist() + [
        m * (ny + 1) - 1 for m in range(2, nx + 1)
    ] + np.sort(np.arange(
        (ny + 1) * nx, (nx + 1) * (ny + 1))).tolist()[::-1] + np.sort(
            np.asarray([m * (ny + 1) for m in range(1, nx)])).tolist()[::-1]

    total_nbr_samples = 100
    params_list = []
    for cnt in range(total_nbr_samples):
        volfraction = random.choice(volfractions)
        rmin = 2.4  # after the first generation phase, we have concluded that rmin should be less than 3 and in the range 2 to 2.8
def simulate_gsm(demand_mean=3, lead_time=12, threshold=0.95, N=200000):
    """
    This simulates the "inventory position" at a single node/service station or a single stage,
    under a Possion demand profile for each time step.

    It is an attempt to simulate, as precisely as possible and consequently, as abstractly as
    necessarily, a policy generated in accordance with Willem's published GSM framework.

    It models the "inventory position" for a given demand and lead time for when we expect a given
    "failure rate".

    Lead time: Time to collect any necessary materials from where ever they are process an SKU by
               the node/warehouse/service station.

    Failure rate is described at one point in the original Willem's paper as "the percentage of time
    that the safety stock covers the demand variation".  This is an over-arching explanation, but to
    be more precise, it is the percentage of time steps for which the "inventory position" is
    negative.

    N.B.  This is not the same as the percentage of SKUs that were not available when required.

    Inventory position: Calculated per time step, after any deliveries have been recieved, it is
    the number of SKUs in stock - the number of SKUs that are outstanding because they were not
    available when required.

    We assume that SKUs that were not available when required are ordered on the time step that
    were found to be unavailable and these unfullfilled orders are fullfilled in exactly the time
    in takes to replenish an SKU.

    The time to replenish an SKU (tau [Willems]) is lead time because we assume the service time of
    the node is zero and it has no upstream supply nodes, so incoming service time is zero.

    As part of the simulation. the base stock levels required at t=0 for the desired failure rate
    to be achieved, according to Willem's algorithm, are calculated for this simple, single station
    "network".

    :param demand_mean: The mean number of SKUs demanded per timestep.
    :param lead_time: The time it takes to replenish an SKU.
    :param threshold: The percentage of days for which the "inventory position" is negative.
    :param N: The number of time steps in the simulation
    """

    p = poisson(demand_mean * lead_time)
    # Base stock in a integer, so we define bounds:
    max_base_stock = p.ppf(threshold)
    min_base_stock = max_base_stock - 1

    inventory_position = np.array([])

    s = np.random.poisson(size=N,
                          lam=demand_mean)  # Sample demand for all time steps
    s = np.append(
        [0] * lead_time,
        s)  # Add a buffer of no demand to simulate the time nothing has
    # had a chance to be replenished.

    # Compute the inventory position at "the end of" each (simulated-instantaneous) time step:
    for i in range(N):
        inventory_position = np.append(
            inventory_position, min_base_stock - np.sum(s[i:i + lead_time]))

    # Calculate the percentage of "failed" days:
    lowest_expected_failure_rate = np.mean(
        inventory_position < -(max_base_stock - min_base_stock))
    highest_expected_failure_rate = np.mean(inventory_position < 0)

    print("With a base stock of: {}, the simulation has an error rate of {}".
          format(min_base_stock, highest_expected_failure_rate))

    print("With a base stock of: {}, the simulation has an error rate of {}".
          format(max_base_stock, lowest_expected_failure_rate))

    failure_rate = (1 - threshold)
    print("The desired failure rate of {} IS{}within the simulated bounds".
          format(
              round(failure_rate, 3), " " if
              (lowest_expected_failure_rate < failure_rate <
               highest_expected_failure_rate) else " NOT "))

    return inventory_position
示例#7
0
def poisson(lam=1.0):
    return dists.poisson(mu=lam)
示例#8
0
文件: dists.py 项目: compmem/RunDEMC
def poisson(lam=1.0):
    return dists.poisson(mu=lam)
示例#9
0
    al_df = DataFrame({ 'events' : x ,
                        'agg_loss' : z})
    return al_df 

def ragglosses_dnq(n, k, rvlf, rvld):
    d_size = int(n / k)
    al_df = ragglosses(d_size, rvlf, rvld)
    for i in arange(1, k):
        al_df = concat([al_df, ragglosses(d_size,
                                          rvlf,
                                          rvld)])
        gc.collect()
    al_df = al_df.sort('agg_loss')
    al_df.index = arange(n)
    return al_df 
    
rtlg_0 = ract_tlgamma(29.35061739, 3.29749312, 5063.457993, Inf)

rpois_0 = poisson(44.51)

df_0 = ragglosses_dnq(1e6, 10, rpois_0, rtlg_0)

df_0.to_hdf('test_01.hdf5', key='al_df')

""" ----------------------------------------------------------------------------
  
    End note:
        (end note starts here)     
   
============================================================================ """