Exemplo n.º 1
0
    def choose_mother(self, index):
        """
        Choose a new mother on the basis of fertility rates.

        NOTE: there is still a very small possibility (in *very* small
        populations) that this will hang due to candidates remaining
        forever empty.  Should add a check to prevent this and exit gracefully.
        """

        candidates = []
        while not candidates:
            tgt_age = int(
                sample_table(self.fertility_age_probs[index], self.rng)[0])
            tgt_prev_min = 0
            tgt_prev_max = 100
            if self.params['use_parity']:
                tgt_prev_min = int(
                    sample_table(
                        self.fertility_parity_probs[(tgt_age - 15) / 5],
                        self.rng)[0])
                # effectively transform 5 into 5+
                tgt_prev_max = tgt_prev_min if tgt_prev_min < 5 else 20
            tgt_set = self.P.individuals_by_age(tgt_age, tgt_age)
            candidates = [x
                    for x in tgt_set \
                    if x.sex == 1 \
                    and x.can_birth() \
                    and not x.with_parents \
                    and tgt_prev_min <= len(x.children) <= tgt_prev_max
                    ]
        return self.rng.choice(candidates)
Exemplo n.º 2
0
    def choose_mother(self, index):
        """
        Choose a new mother on the basis of fertility rates.

        NOTE: there is still a very small possibility (in *very* small
        populations) that this will hang due to candidates remaining
        forever empty.  Should add a check to prevent this and exit gracefully.
        """

        candidates = []
        while not candidates:
            tgt_age = int(sample_table(self.fertility_age_probs[index], self.rng)[0])
            tgt_prev_min = 0; tgt_prev_max = 100
            if self.params['use_parity']:
                tgt_prev_min = int(sample_table(
                    self.fertility_parity_probs[(tgt_age-15)/5], self.rng)[0])
                # effectively transform 5 into 5+
                tgt_prev_max = tgt_prev_min if tgt_prev_min < 5 else 20
            tgt_set = self.P.individuals_by_age(tgt_age, tgt_age)
            candidates = [x
                    for x in tgt_set \
                    if x.sex == 1 \
                    and x.can_birth() \
                    and not x.with_parents \
                    and tgt_prev_min <= len(x.children) <= tgt_prev_max
                    ]
        return self.rng.choice(candidates)
Exemplo n.º 3
0
    def gen_hh_age_structured_pop(self, pop_size, hh_probs, age_probs_i,
                                  cutoffs, rng):
        """
        Generate a population of individuals with age structure and household 
        composition.

        Household composition here is approximated by the number of 
        individuals who are:

        - pre-school age (0--4)
        - school age (5--17)
        - adult (18+)

        This is a bit ad hoc, but serves current purposes.

        :param pop_size: The size of the population to be generated.
        :type pop_size: int
        :param hh_probs: A table of household size probabilities.
        :type hh_probs: list
        :param age_probs_i: A table of age probabilities.
        :type age_probs_i: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`
        """

        age_probs = [[x, int(y[0])] for x, y in age_probs_i]
        split_probs = split_age_probs(age_probs, cutoffs)
        split_probs.reverse()

        i = 0
        while i < pop_size:
            # get list of [adults, school age, preschool age]
            hh_type = [int(x) for x in sample_table(hh_probs, rng)]
            #            hh_type = [int(x) for x in sample_uniform(hh_probs, rng)]
            cur_hh = []
            for cur_hh_type, cur_prob in zip(hh_type, split_probs):
                for _ in itertools.repeat(None, cur_hh_type):
                    sex = rng.randint(0, 1)
                    cur_age = sample_table(cur_prob, rng)
                    cur_ind = self.add_individual(cur_age,
                                                  sex,
                                                  adam=True,
                                                  logging=self.logging)
                    if self.logging:
                        cur_ind.add_log(0, 'f', "Individual (bootstrap)")
                    cur_hh.append(cur_ind)
                    i += 1
            hh_id = self.add_group('household', cur_hh)
            self.households[hh_id] = Household(0, adam=True)
            if self.logging:
                self.households[hh_id].add_log(
                    0, 'f', "Household (bootstrap)",
                    len(self.groups['household'][hh_id]))
Exemplo n.º 4
0
    def gen_hh_age_structured_pop(self, pop_size, hh_probs, age_probs_i,     
            cutoffs, rng):
        """
        Generate a population of individuals with age structure and household 
        composition.

        Household composition here is approximated by the number of 
        individuals who are:

        - pre-school age (0--4)
        - school age (5--17)
        - adult (18+)

        This is a bit ad hoc, but serves current purposes.

        :param pop_size: The size of the population to be generated.
        :type pop_size: int
        :param hh_probs: A table of household size probabilities.
        :type hh_probs: list
        :param age_probs_i: A table of age probabilities.
        :type age_probs_i: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`
        """

        age_probs = [[x, int(y[0])] for x, y in age_probs_i]
        split_probs = split_age_probs(age_probs, cutoffs)
        split_probs.reverse()

        i = 0
        while i < pop_size:
            # get list of [adults, school age, preschool age]
            hh_type = [int(x) for x in sample_table(hh_probs, rng)]
#            hh_type = [int(x) for x in sample_uniform(hh_probs, rng)]
            cur_hh = []
            for cur_hh_type, cur_prob in zip(hh_type, split_probs):
                for _ in itertools.repeat(None, cur_hh_type):
                    sex = rng.randint(0, 1)
                    cur_age = sample_table(cur_prob, rng)
                    cur_ind = self.add_individual(cur_age, sex, 
                            adam=True, logging=self.logging)
                    if self.logging:
                        cur_ind.add_log(0, 'f', "Individual (bootstrap)")
                    cur_hh.append(cur_ind)
                    i += 1
            hh_id = self.add_group('household', cur_hh)
            self.households[hh_id] = Household(0, adam=True)
            if self.logging:
                self.households[hh_id].add_log(0, 'f', "Household (bootstrap)",
                        len(self.groups['household'][hh_id]))
Exemplo n.º 5
0
    def allocate_groups_by_age(self, group_type, size_probs, min_age, max_age,
                               rng):
        """
        Allocate individuals in a given age range to groups
        with a given size distribution.

        :param group_type: The type of groups to create.
        :type group_type: string
        :param size_probs: A table mapping probability to group size.
        :type size_probs: list
        :param min_age: The minimum age to include.
        :type min_age: int
        :param max_age: The maximum age to include.
        :type max_age: int
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`

        """

        # TODO: move out of class? FIX ind/ID

        assert not self.groups.has_key(group_type)

        self.init_group_type(group_type)

        inds = self.individuals_by_age(min_age, max_age)
        rng.shuffle(inds)
        while len(inds) > 0:
            size = int(sample_table(size_probs, rng)[0])
            members = [x for x in inds[:size]]
            group_id = self.add_group(group_type, members)
            for x in members:
                x.groups.__setitem__(group_type, group_id)
            inds = inds[size:]
Exemplo n.º 6
0
    def allocate_groups_by_age(self, group_type, 
            size_probs, min_age, max_age, rng):
        """
        Allocate individuals in a given age range to groups
        with a given size distribution.

        :param group_type: The type of groups to create.
        :type group_type: string
        :param size_probs: A table mapping probability to group size.
        :type size_probs: list
        :param min_age: The minimum age to include.
        :type min_age: int
        :param max_age: The maximum age to include.
        :type max_age: int
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`

        """

        # TODO: move out of class? FIX ind/ID

        assert not self.groups.has_key(group_type)

        self.init_group_type(group_type)

        inds = self.individuals_by_age(min_age, max_age)
        rng.shuffle(inds)
        while len(inds) > 0:
            size = int(sample_table(size_probs, rng)[0])
            members = [x for x in inds[:size]]
            group_id = self.add_group(group_type, members)
            for x in members:
                x.groups.__setitem__(group_type, group_id)
            inds = inds[size:]
Exemplo n.º 7
0
    def gen_age_structured_pop(self, pop_size, age_probs, rng):
        """
        Generate a population of individuals with given age structure.

        :param pop_size: The number of individuals to generate.
        :type pop_size: int
        :param age_probs: A table mapping probabilities to age.
        :type age_probs: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`
        """

        # TODO: move out of class?

        for _ in itertools.repeat(None, pop_size):
            self.add_individual(age=int(sample_table(age_probs, rng)[0]))
Exemplo n.º 8
0
    def gen_age_structured_pop(self, pop_size, age_probs, rng):
        """
        Generate a population of individuals with given age structure.

        :param pop_size: The number of individuals to generate.
        :type pop_size: int
        :param age_probs: A table mapping probabilities to age.
        :type age_probs: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`
        """

        # TODO: move out of class?

        for _ in itertools.repeat(None, pop_size):
            self.add_individual(age=int(sample_table(age_probs, rng)[0]))
Exemplo n.º 9
0
    def gen_hh_size_structured_pop(self, pop_size, hh_probs, rng):
        """
        Generate a population of individuals with household size structure.

        :param pop_size: The size of the population to be generated.
        :type pop_size: int
        :param hh_probs: A table of household size probabilities.
        :type hh_probs: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`
        """

        i = 0  
        while i < pop_size:
            size = int(sample_table(hh_probs, rng)[0])
            cur_hh = []
            for _ in itertools.repeat(None, size):
                cur_ind = self.add_individual(logging=self.logging)
                cur_hh.append(cur_ind)
                i += 1
            self.add_group('household', cur_hh)
Exemplo n.º 10
0
    def gen_hh_size_structured_pop(self, pop_size, hh_probs, rng):
        """
        Generate a population of individuals with household size structure.

        :param pop_size: The size of the population to be generated.
        :type pop_size: int
        :param hh_probs: A table of household size probabilities.
        :type hh_probs: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`
        """

        i = 0
        while i < pop_size:
            size = int(sample_table(hh_probs, rng)[0])
            cur_hh = []
            for _ in itertools.repeat(None, size):
                cur_ind = self.add_individual(logging=self.logging)
                cur_hh.append(cur_ind)
                i += 1
            self.add_group('household', cur_hh)
Exemplo n.º 11
0
    def allocate_groups_by_group(self, target_type, source_type, size_probs,
                                 rng):
        """
        Create 'groups of groups' by randomly aggregating lower level groups
        into higher level groups according to the given size distribution.

        For example, build neighbourhoods out of households by
        grouping households according to some distribution over number of 
        households per neighbourhood.

        :param target_type: The type of groups to create.
        :type target_type: int
        :param source_type: The type of groups to aggregate.
        :type source_type: int
        :param size_probs: A table mapping probability to group size.
        :type size_probs: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`

        """

        # TODO: factor out

        assert self.groups.has_key(source_type)
        assert not self.groups.has_key(target_type)

        self.init_group_type(target_type)

        ids = self.groups[source_type].keys()
        rng.shuffle(ids)
        while len(ids) > 0:
            size = int(sample_table(size_probs, rng)[0])
            members = []
            group_id = self.add_group(target_type, members)
            for source_id in ids[:size]:
                for i in self.groups[source_type][source_id]:
                    self.I[i].groups[target_type] = group_id
                    members.append(self.I[i])
                self.add_individuals_to_group(target_type, group_id, members)
            del ids[:size]
Exemplo n.º 12
0
    def allocate_groups_by_group(self, target_type, source_type, 
            size_probs, rng):
        """
        Create 'groups of groups' by randomly aggregating lower level groups
        into higher level groups according to the given size distribution.

        For example, build neighbourhoods out of households by
        grouping households according to some distribution over number of 
        households per neighbourhood.

        :param target_type: The type of groups to create.
        :type target_type: int
        :param source_type: The type of groups to aggregate.
        :type source_type: int
        :param size_probs: A table mapping probability to group size.
        :type size_probs: list
        :param rng: The random number generator to use.
        :type rng: :class:`random.Random`

        """

        # TODO: factor out

        assert self.groups.has_key(source_type)
        assert not self.groups.has_key(target_type)
        
        self.init_group_type(target_type)

        ids = self.groups[source_type].keys()
        rng.shuffle(ids)
        while len(ids) > 0:
            size = int(sample_table(size_probs, rng)[0])
            members = []
            group_id = self.add_group(target_type, members)
            for source_id in ids[:size]:
                for i in self.groups[source_type][source_id]:
                    self.I[i].groups[target_type] = group_id
                    members.append(self.I[i])
                self.add_individuals_to_group(target_type, group_id, members)
            del ids[:size]
Exemplo n.º 13
0
from utils import sample_table

X_train, X_test, y_train, y_test = load_cancer()
attributes = cancer_attributes()
# Train
svc_model = SVC(verbose=True)  # C-Support Vector Classification
# svc_model = LinearSVC(verbose=True)  # Linear Support Vector Classification
svc_model.fit(X_train, y_train)

# Confusion Matrix for both Test and training sets
y_predict = svc_model.predict(X_test)
pretty_cmatrix(y_predict, y_test, "SVM", "Test")  # , filename="SVM_TEST.png")
sample_table(
    X_test,
    y_test,
    y_predict.flatten(),
    columns=attributes,
    # write_csv="SVM_TEST_DATA.csv",
)

y_predict = svc_model.predict(X_train)
pretty_cmatrix(y_predict, y_train, "SVM", "Train")  # , filename="SVN_TRAIN.png")

sample_table(
    X_train,
    y_train,
    y_predict.flatten(),
    columns=attributes,
    # write_csv="SVM_TRAIN_DATA.csv",
)