Java tutorial
/* MIT License Copyright (c) 2017 Hamdi Kavak Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.hamdikavak.humanmobility.modeling; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.log4j.Logger; import org.joda.time.Duration; import com.hamdikavak.humanmobility.modeling.spatial.ExtendedLocationTrace; /** * First passage time (FPT) is a term suggested by Gonzlez, Hidalgo, and * Barabsi (2008) to describe the probability to find a person in the same * place after a period of time. This class calculates hour-based FPT and * returns the probability values over time. It is important to note that all * traces given to this class should belong to the same person. If GPS locations * are used, these locations have to be clustered using a classification * algorithm such as DBSCAN and assigned a location id. * <p> * Source: Gonzlez, M. C., Hidalgo, C. A., & Barabsi, A.-L. (2008). * Understanding individual human mobility patterns. Nature, 453(7196), 779782. * https://doi.org/10.1038/nature06958 * * @author Hamdi Kavak * @version 0.2.1 * */ public class FirstPassageTime { protected final static Logger logger = Logger.getLogger(FirstPassageTime.class); private long lengthInDays = 10; private long lengthInHours = 24 * lengthInDays; /** * Returns first passage time, the probability to find a person in the same * place after a period of time. * * @param traces * Location traces of an individual sorted from old to new. If * GPS locations are used, these locations have to be clustered * using a classification algorithm such as DBSCAN and assigned a * location id. If cellphone towers are used, tower id should be * enough. * * @return All hour differences between a person's first appearance in a * place vs. all consequent appearances. */ public List<Long> calculateFirstPassageTime(List<ExtendedLocationTrace> traces) { ExtendedLocationTrace firstElementOfThisUniqueId; List<Long> uniqueLocationIds = new ArrayList<Long>(); HashMap<Long, Boolean> oneLocationHourList = null; List<Long> entireHourList = new ArrayList<Long>(); // identify all unique locations uniqueLocationIds = extractUniqueLocationIds(traces); // for each unique location // - find the first record that location appears in user's location // trace history // - find other records regarding visits to the same place and find time // difference between that record and the first record. // - keep all hour differences in a list for (Long aUniqueLocationId : uniqueLocationIds) { firstElementOfThisUniqueId = null; oneLocationHourList = new HashMap<Long, Boolean>(); logger.info("FPT location id:" + aUniqueLocationId); for (int i = 0; i < traces.size(); i++) { if (traces.get(i).getLocationId() == aUniqueLocationId) { // if this is the first appearance, mark as first if (firstElementOfThisUniqueId == null) { firstElementOfThisUniqueId = traces.get(i); continue; } // calculate the time interval between visits to this // location Long hourDifference = (long) new Duration(firstElementOfThisUniqueId.getUTCTime(), traces.get(i).getUTCTime()).toStandardHours().getHours(); // let's make sure hour difference is not larger than our // intended hour investigation length if (hourDifference <= lengthInHours) { // we keep hour appearances in a hashmap key to ensure we // do not overestimate multiple very close location // reports. oneLocationHourList.put(hourDifference, true); } } } entireHourList.addAll(oneLocationHourList.keySet()); } List<Long> result = new ArrayList<Long>(); for (int i = 0; i < entireHourList.size(); i++) { result.add(entireHourList.get(i)); } return result; } /** * This methods returns a list of unique location Ids from a given trace list. * @param traces * @return */ private List<Long> extractUniqueLocationIds(List<ExtendedLocationTrace> traces) { HashMap<Long, Boolean> idMap = new HashMap<Long, Boolean>(); for (ExtendedLocationTrace aTrace : traces) { idMap.put(aTrace.getLocationId(), true); } return (new ArrayList<Long>(idMap.keySet())); } }