标签:i++ math pre color nextval 包含 图片 extends public
时间序列
TimeSeries.java
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.Map; 4 import java.util.TreeMap; 5 import java.util.concurrent.TimeUnit; 6 7 public class TimeSeries<T> implements Iterable<TimeSeries.Entry> { 8 private final Map<Long,T> map = new TreeMap(); 9 10 public void add(long time, T event) { 11 map.put(time, event); 12 try { 13 TimeUnit.MICROSECONDS.sleep(1); // 0.000001 sec delay 14 } catch(InterruptedException e) { 15 System.err.println(e); 16 } 17 } 18 19 public T get(long time) { 20 return map.get(time); 21 } 22 23 ArrayList getList() { 24 ArrayList<Entry> list = new ArrayList(); 25 for (Entry entry : this) { 26 list.add(entry); 27 } 28 return list; 29 } 30 31 public int size() { 32 return map.size(); 33 } 34 35 @Override 36 public Iterator iterator() { 37 return new Iterator() { // anonymous inner class 38 private final Iterator it = map.keySet().iterator(); 39 40 @Override 41 public boolean hasNext() { 42 return it.hasNext(); 43 } 44 45 @Override 46 public Entry<T> next() { 47 long time = (Long)it.next(); 48 T event = map.get(time); 49 return new Entry(time, event); 50 } 51 }; 52 } 53 54 public static class Entry<T> { 55 private final Long time; 56 private final T event; 57 58 public Entry(long time, T event) { 59 this.time = time; 60 this.event = event; 61 } 62 63 public long getTime() { 64 return time; 65 } 66 67 public T getEvent() { 68 return event; 69 } 70 71 @Override 72 public String toString() { 73 return String.format("(%d, %s)", time, event); 74 } 75 } 76 }
TimeSeriesTester.java
1 import java.util.ArrayList; 2 3 public class TimeSeriesTester { 4 final static String[] EVENTS = {"It", "was", "the", "best", "of", "times"}; 5 6 public static void main(String[] args) { 7 TimeSeries<String> series = new TimeSeries(); 8 load(series); 9 10 for (TimeSeries.Entry<String> entry : series) { 11 long time = entry.getTime(); 12 String event = entry.getEvent(); 13 System.out.printf("%16d: %s%n", time, event); 14 } 15 16 ArrayList list = series.getList(); 17 System.out.printf("list.get(3) = %s%n", list.get(3)); 18 } 19 20 static void load(TimeSeries<String> series) { 21 for (String event : EVENTS) { 22 series.add(System.currentTimeMillis(), event); 23 } 24 } 25 }
移动平均
MovingAverage.java
1 import java.util.Iterator; 2 3 public class MovingAverage extends TimeSeries<Double> { 4 private final TimeSeries parent; 5 private final int length; 6 7 public MovingAverage(TimeSeries parent, int length) { 8 this.parent = parent; 9 this.length = length; 10 if (length > parent.size()) { 11 throw new IllegalArgumentException("That‘s too long."); 12 } 13 14 double[] tmp = new double[length]; // temp array to compute averages 15 double sum = 0; 16 int i=0; 17 Iterator it = parent.iterator(); 18 for (int j = 0; j < length; j++) { 19 sum += tmp[i++] = nextValue(it); 20 } 21 this.add(System.currentTimeMillis(), sum/length); 22 23 while (it.hasNext()) { 24 sum -= tmp[i%length]; 25 sum += tmp[i++%length] = nextValue(it); 26 this.add(System.currentTimeMillis(), sum/length); 27 } 28 } 29 30 /* Returns the double value in the Entry currently located by it. 31 */ 32 private static double nextValue(Iterator it) { 33 TimeSeries.Entry<Double> entry = (TimeSeries.Entry)it.next(); 34 return entry.getEvent(); 35 } 36 }
MovingAverageTester.java
1 public class MovingAverageTester { 2 static final double[] DATA = {20, 25, 21, 26, 28, 27, 29, 31}; 3 4 public static void main(String[] args) { 5 TimeSeries<Double> series = new TimeSeries(); 6 for (double x : DATA) { 7 series.add(System.currentTimeMillis(), x); 8 } 9 System.out.println(series.getList()); 10 11 TimeSeries<Double> ma3 = new MovingAverage(series, 3); 12 System.out.println(ma3.getList()); 13 14 TimeSeries<Double> ma5 = new MovingAverage(series, 5); 15 System.out.println(ma5.getList()); 16 } 17 }
指数分布
1 import java.util.Random; 2 3 public class ArrivalTimesTester { 4 static final Random random = new Random(); 5 static final double LAMBDA = 0.25; 6 7 static double time() { 8 double p = random.nextDouble(); 9 return -Math.log(1 - p)/LAMBDA; 10 } 11 12 public static void main(String[] args) { 13 double sum=0; 14 for (int i = 0; i < 8; i++) { 15 System.out.println(time()); 16 sum += time(); 17 } 18 System.out.println("\n"); 19 System.out.println(sum/8); 20 } 21 }
标签:i++ math pre color nextval 包含 图片 extends public
原文地址:https://www.cnblogs.com/cxc1357/p/14675173.html