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.MigParameters;
21  
22  /**
23   * Calculation regarding the population of migrants. One method has seven
24   * parameters and therefore does not confirm to ESCA-JAVA0138.
25   * 
26   * Created on July 22, 2006
27   * 
28   * @author Christina Bohk
29   * @author Roland Ewald
30   * 
31   */
32  public class MigPopulation extends
33      AbstractPopulation<MigParameters, BasicResults> {
34  
35    @Override
36    public BasicResults calculatePopulation(MigParameters parameters) {
37  
38      BasicResults results = new BasicResults(parameters.getNumOfYears(),
39          parameters.getMaxAge());
40  
41      calculateSurvivalProbabilities(parameters, results);
42  
43      // Calculate rest:
44  
45      // ...for females
46      calculateMeanAndEndPopulation(results.getMeanXf(), results.getEndXf(),
47          parameters.getMigrantsXf(), parameters.getSurviveProbO100f(), results
48              .getP1f(), results.getP2f(), parameters.getNumOfYears(), parameters
49              .getMaxAge());
50  
51      // ...for males
52      calculateMeanAndEndPopulation(results.getMeanXm(), results.getEndXm(),
53          parameters.getMigrantsXm(), parameters.getSurviveProbO100m(), results
54              .getP1m(), results.getP2m(), parameters.getNumOfYears(), parameters
55              .getMaxAge());
56  
57      return results;
58    }
59  
60    /**
61     * Calculates mean and end populations.
62     * 
63     * @param meanPopulation
64     *          mean population (will be filled)
65     * @param endPopulation
66     *          end population (will be filled)
67     * @param migrants
68     *          the mirgants
69     * @param surviveProbO100
70     *          survival probability of the over-100-years-olds
71     * @param p1
72     *          the P_1 matrix (infant survival probability first half of first
73     *          year)
74     * @param p2
75     *          the P_2 matrix (infant survival probability second half of first
76     *          year)
77     * @param numberOfYears
78     *          number of years to be predicted
79     * @param maximumAge
80     *          the maximum age class
81     */
82    protected static void calculateMeanAndEndPopulation(Matrix2D meanPopulation,
83        Matrix2D endPopulation, Matrix2D migrants, Matrix2D surviveProbO100,
84        Matrix2D p1, Matrix2D p2, int numberOfYears, int maximumAge) {
85  
86      for (int year = 1; year < numberOfYears; year++) {
87        for (int age = 0; age < maximumAge + 1; age++) {
88  
89          // Calculate mean population
90          if (age == maximumAge) {
91            meanPopulation.setQuick(age, year, endPopulation.getQuick(age - 1,
92                year - 1)
93                * p2.getQuick(age - 1, year - 1)
94                + (migrants.getQuick(year, age) / 2)
95                * surviveProbO100.getQuick(year, 0));
96          } else {
97            switch (age) {
98            case 0:
99              meanPopulation.setQuick(age, year, 0);
100             break;
101           case 1:
102             meanPopulation.setQuick(age, year, (migrants.getQuick(year - 1,
103                 age - 1) + migrants.getQuick(year, age) / 2)
104                 * p2.getQuick(age - 1, year - 1));
105             break;
106           default:
107             meanPopulation.setQuick(age, year,
108                 (migrants.getQuick(year, age) / 2 + endPopulation.getQuick(
109                     age - 1, year - 1))
110                     * p2.getQuick(age - 1, year - 1));
111             break;
112           }
113         }
114 
115         // Calculate end population
116         if (age == maximumAge) {
117           endPopulation.setQuick(age, year, meanPopulation.getQuick(age, year)
118               * surviveProbO100.getQuick(year, 0)
119               + migrants.getQuick(year, age) / 2);
120 
121         } else {
122           switch (age) {
123           case 0:
124             endPopulation.setQuick(age, year, meanPopulation
125                 .getQuick(age, year)
126                 * p1.getQuick(age, year) + migrants.getQuick(year, age));
127             break;
128           default:
129             endPopulation.setQuick(age, year, meanPopulation
130                 .getQuick(age, year)
131                 * p1.getQuick(age, year) + migrants.getQuick(year, age) / 2);
132             break;
133           }
134         }
135       }
136     }
137   }
138 }