View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.simulation.calculation.deterministic;
17  
18  import p3j.experiment.results.BasicResults;
19  import p3j.misc.math.Matrix2D;
20  import p3j.simulation.calculation.deterministic.parameters.InFlowDescendantParameters;
21  
22  /**
23   * Calculation of the population containing descendants of sub-populations with
24   * in-flow matrix.
25   * 
26   * Created on July 22, 2006
27   * 
28   * @author Christina Bohk
29   * @author Roland Ewald
30   * 
31   */
32  public class InFlowDescendantPopulation extends
33      AbstractPopulation<InFlowDescendantParameters, BasicResults> {
34  
35    @Override
36    public BasicResults calculatePopulation(String subPopName, int generation,
37        InFlowDescendantParameters parameters) {
38  
39      BasicResults results = new BasicResults(subPopName, generation,
40          parameters.getNumOfYears(), parameters.getMaxAge());
41  
42      calculateSurvivalProbabilities(parameters, results);
43  
44      // Calculate rest
45  
46      // ...for females
47      calculateMeanAndEndPopulation(parameters.getOldMeanXf(),
48          parameters.getOldFertX(), parameters.getFemalePropLiveBirth(),
49          results.getMeanXf(), results.getEndXf(),
50          parameters.getSurviveProbO100f(), results.getP1f(), results.getP2f(),
51          parameters.getNumOfYears(), parameters.getMaxAge());
52  
53      // ...for males
54      calculateMeanAndEndPopulation(parameters.getOldMeanXf(),
55          parameters.getOldFertX(), parameters.getMalePropLiveBirth(),
56          results.getMeanXm(), results.getEndXm(),
57          parameters.getSurviveProbO100m(), results.getP1m(), results.getP2m(),
58          parameters.getNumOfYears(), parameters.getMaxAge());
59  
60      return results;
61    }
62  
63    /**
64     * Calculates mean and end populations.
65     * 
66     * @param oldMeanXf
67     *          old mean population of females
68     * @param oldFertX
69     *          old fertility
70     * @param propLiveBirth
71     *          proportion of live birth of this sex
72     * @param meanPopulation
73     *          mean population (filled by this method)
74     * @param endPopulation
75     *          end population (filled by this method)
76     * @param surviveProbO100
77     *          survival probability of the over-100-years-olds
78     * @param p1
79     *          P_1 matrix (infant survival probability first 6 months)
80     * @param p2
81     *          P_2 matrix (infant survival probability second half year)
82     * @param numberOfYears
83     *          number of years to be predicted
84     * @param maximumAge
85     *          the maximum age
86     */
87    protected static void calculateMeanAndEndPopulation(Matrix2D oldMeanXf,
88        Matrix2D oldFertX, Matrix2D propLiveBirth, Matrix2D meanPopulation,
89        Matrix2D endPopulation, Matrix2D surviveProbO100, Matrix2D p1,
90        Matrix2D p2, int numberOfYears, int maximumAge) {
91  
92      for (int year = 1; year < numberOfYears; year++) {
93  
94        double numOfChilds = AbstractPopulation.getNumOfChilds(oldMeanXf,
95            oldFertX, year);
96  
97        for (int age = 0; age < maximumAge + 1; age++) {
98  
99          // Calculate mean population
100         if (age == maximumAge) {
101           meanPopulation.setQuick(
102               age,
103               year,
104               endPopulation.getQuick(age - 1, year - 1)
105                   * p2.getQuick(age - 1, year - 1)
106                   + endPopulation.getQuick(age, year - 1)
107                   * surviveProbO100.getQuick(year, 0));
108         } else {
109           switch (age) {
110           case 0:
111             meanPopulation.setQuick(age, year,
112                 numOfChilds * propLiveBirth.getQuick(year, 0));
113             break;
114           default:
115             meanPopulation.setQuick(
116                 age,
117                 year,
118                 endPopulation.getQuick(age - 1, year - 1)
119                     * p2.getQuick(age - 1, year - 1));
120             break;
121           }
122         }
123 
124         // Calculate end population
125         if (age == maximumAge) {
126           endPopulation.setQuick(age, year, meanPopulation.getQuick(age, year)
127               * surviveProbO100.getQuick(year, 0));
128         } else {
129           endPopulation.setQuick(age, year, meanPopulation.getQuick(age, year)
130               * p1.getQuick(age, year));
131         }
132 
133       }
134     }
135   }
136 }