Esempio n. 1
0
    def cinference(self, nperm=99, maxiter=1000):
        """Compare the within sum of squares for the solution against
        conditional simulated solutions where areas are randomly assigned to
        regions that maintain the cardinality of the original solution and
        respect contiguity relationships.

        Parameters
        ----------

        nperm       : int
                      number of random permutations for calculation of
                      pseudo-p_values

        maxiter     : int
                      maximum number of attempts to find each permutation

        Attributes
        ----------

        pvalue      : float
                      pseudo p_value

        feas_sols   : int
                      number of feasible solutions found

        Notes
        -----

        it is possible for the number of feasible solutions (feas_sols) to be
        less than the number of permutations requested (nperm); an exception
        is raised if this occurs.

        Examples
        --------

        Setup is the same as shown above except using a 5x5 community.

        >>> import numpy as np
        >>> import pysal
        >>> np.random.seed(100)
        >>> w=pysal.weights.lat2W(5,5)
        >>> z=np.random.random_sample((w.n,2))
        >>> p=np.ones((w.n,1),float)
        >>> floor=3
        >>> solution=pysal.region.Maxp(w,z,floor,floor_variable=p,initial=100)

        Set nperm to 9 meaning that 9 random regions are computed and used for
        the computation of a pseudo-p-value for the actual Max-p solution. In
        empirical work this would typically be set much higher, e.g. 999 or
        9999.

        >>> solution.cinference(nperm=9, maxiter=100)
        >>> solution.cpvalue
        0.1

        """
        ids = self.w.id_order
        num_regions = len(self.regions)
        wsss = np.zeros(nperm + 1)
        self.cwss = self.objective_function()
        cards = [len(i) for i in self.regions]
        sim_solutions = RR.Random_Regions(ids,
                                          num_regions,
                                          cardinality=cards,
                                          contiguity=self.w,
                                          maxiter=maxiter,
                                          permutations=nperm)
        self.cfeas_sols = len(sim_solutions.solutions_feas)
        if self.cfeas_sols < nperm:
            raise Exception('not enough feasible solutions found')
        cv = 1
        c = 1
        for solution in sim_solutions.solutions_feas:
            wss = self.objective_function(solution.regions)
            wsss[c] = wss
            if wss <= self.cwss:
                cv += 1
            c += 1
        self.cpvalue = cv / (1. + self.cfeas_sols)
        self.cwss_perm = wsss
        self.cwss_perm[0] = self.cwss
Esempio n. 2
0
    def inference(self, nperm=99):
        """Compare the within sum of squares for the solution against
        simulated solutions where areas are randomly assigned to regions that
        maintain the cardinality of the original solution.

        Parameters
        ----------

        nperm       : int
                      number of random permutations for calculation of
                      pseudo-p_values

        Attributes
        ----------

        pvalue      : float
                      pseudo p_value

        Examples
        --------

        Setup is the same as shown above except using a 5x5 community.

        >>> import numpy as np
        >>> import pysal
        >>> np.random.seed(100)
        >>> w=pysal.weights.lat2W(5,5)
        >>> z=np.random.random_sample((w.n,2))
        >>> p=np.ones((w.n,1),float)
        >>> floor=3
        >>> solution=pysal.region.Maxp(w,z,floor,floor_variable=p,initial=100)

        Set nperm to 9 meaning that 9 random regions are computed and used for
        the computation of a pseudo-p-value for the actual Max-p solution. In
        empirical work this would typically be set much higher, e.g. 999 or
        9999.

        >>> solution.inference(nperm=9)
        >>> solution.pvalue
        0.1

        """
        ids = self.w.id_order
        num_regions = len(self.regions)
        wsss = np.zeros(nperm + 1)
        self.wss = self.objective_function()
        cards = [len(i) for i in self.regions]
        sim_solutions = RR.Random_Regions(ids,
                                          num_regions,
                                          cardinality=cards,
                                          permutations=nperm)
        cv = 1
        c = 1
        for solution in sim_solutions.solutions_feas:
            wss = self.objective_function(solution.regions)
            wsss[c] = wss
            if wss <= self.wss:
                cv += 1
            c += 1
        self.pvalue = cv / (1. + len(sim_solutions.solutions_feas))
        self.wss_perm = wsss
        self.wss_perm[0] = self.wss