-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathThetaTime.java
53 lines (41 loc) · 2.21 KB
/
ThetaTime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package tools;
/**
* This class realizes the concept of theta time described in the work of Docorogna et. al. 1993 "A geographical model
* for the daily and weekly seasonal volatility in the foreign exchange market". In the nutshell: theta time is used to
* define time intervals of equal activity (in contrast to the physical time where the time distance between two ticks
* is constant).
*/
public class ThetaTime {
private static final long MLS_WEEK = 604800000L; // number of milliseconds in a week
/**
* The method takes the weekly activity seasonality as input and creates a set of Theta timestamps. One week will
* contain numThetaBins intervals.
* @param weeklyActivitySeasonality double array
* @return array of timestamps in milliseconds starting from 0 and finishing at MLS_WEEK
*/
public static long[] thetaTimestampsFromSeasonalityArray(double[] weeklyActivitySeasonality, int numThetaBins){
int numActivityBins = weeklyActivitySeasonality.length;
long lenOfSeasonalityBin = MLS_WEEK / numActivityBins;
long[] thetaTimeStamps = new long[numThetaBins];
double sumWeeklyActivity = 0;
for (double activity : weeklyActivitySeasonality){
sumWeeklyActivity += activity;
}
double activityPerThetaBin = sumWeeklyActivity / numThetaBins;
double collectedActivity = 0;
int thetaIndex = 0;
for (int i = 0; i < numActivityBins; i++){
long tBegOfBin = i * lenOfSeasonalityBin;
collectedActivity += weeklyActivitySeasonality[i];
while (collectedActivity >= activityPerThetaBin){
double oldCollectedActivity = collectedActivity - weeklyActivitySeasonality[i];
double activityToCover = activityPerThetaBin - oldCollectedActivity; // the part of the activity covered by the new part of the seasonality
thetaTimeStamps[thetaIndex] = (long) (tBegOfBin + lenOfSeasonalityBin * activityToCover / weeklyActivitySeasonality[i]);
collectedActivity -= activityPerThetaBin;
thetaIndex += 1;
}
}
thetaTimeStamps[numThetaBins - 1] = MLS_WEEK;
return thetaTimeStamps;
}
}