com.jstatcom.ts
Class TSDateRange
- java.lang.Object
-
- com.jstatcom.ts.TSDateRange
-
public final class TSDateRange extends java.lang.ObjectHolds a range for possible TSDate objects defined as[lowerBound, upperBound]and provides checks against it. The class can be used by classes that allow to specify a date that needs to be validated.This class is immutable by design, see (Joshua Bloch, "Effective Java", Item 13) .
- See Also:
TSDate
-
-
Constructor Summary
Constructors Constructor and Description TSDateRange()Creates a default range starting from the defaultTSDatewith 100 observations.TSDateRange(TSDate t1, int numberOfObs)CreatesTSDateRangefrom specified start date and number of observations.TSDateRange(TSDate t1, TSDate t2)CreatesTSDateRangefrom specified start date and end date.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description TSDateRangeaddPeriodsToStart(int periods)Gets a newTSDateRangerange object with the start date shifted byperiods.TSDateRangecommonRange(TSDateRange otherRange)Returns the biggest date range that is common to this range and another range.double[][]createSeasDum(boolean centered, boolean all)Creates seasonal dummy series asdouble[][], seecreateSeasDumTS(boolean, boolean).TS[]createSeasDumTS(boolean centered, boolean all)Creates seasonal dummy series according to the periodicity of this time series.double[]createShiftDum(TSDateRange shiftRange)Creates adouble[]that is a shift dummy for this range, seecreateShiftDumTS(java.lang.String, com.jstatcom.ts.TSDateRange).TScreateShiftDumTS(java.lang.String name, TSDateRange shiftRange)Creates a time series that is a shift dummy for this range.double[]createTrendShiftDum(TSDateRange shiftRange)Creates adouble[]that is a trend shift dummy for this range, seecreateTrendShiftDumTS(java.lang.String, com.jstatcom.ts.TSDateRange).TScreateTrendShiftDumTS(java.lang.String name, TSDateRange shiftRange)Creates a time series that is a trend shift dummy for this range.TSDatedateForIndex(int index)Returns the date corresponding to an index respective to the range.java.lang.Stringencloses(TSDate date)Checks whether a given date is within the range defined by this object.java.lang.Stringencloses(TSDateRange otherRange)Checks whether a given date range is included within the range defined by this object.booleanequals(java.lang.Object o)Overridesequalsto provide logical comparison of two differentTSDateRangeobjects.java.lang.Stringformat(java.lang.String description, int space)Gets a nicely formatted version of this date range with a description and the number of observations, for example
sample range: [1960 Q3, 1982 Q4], T = 90
with "sample range:" asdescription.static TSDateRangegetMergedRange(TS[] tsArray)Gets the merged range of all time series in the given array.inthashCode()OverrideshashCode(), because equals was overwritten.intindexForDate(TSDate date)Computes the index that a date has respective to the range specified by this object, starting with 0.int[]indicesForRange(TSDateRange otherRange)Computes the indices that a date range has within the range specified by this object, starting with 0.TSDatelowerBound()Gets reference to start date of range.TSDateRangemergedRange(TSDateRange otherRange)Returns the merged date range that starts at the earliest start date and ends at the latest enddate.intnumOfObs()Returns number of observations represented by this range.intsubPeriodicity()Gets the sub periodicity of this date range.java.lang.String[]timeAxisStringArray()This method returns the range represented as an array of Strings, for example{"1960 Q1", "1960 Q2", ...., "1982 Q4"}.java.lang.StringtoString()Returns a string representation of the specified range.TSDateupperBound()Gets reference the last date of the range.static TSDateRangevalueOf(java.lang.String exp)Static initializer that creates aTSDateRangefrom a string representation.
-
-
-
Constructor Detail
-
TSDateRange
public TSDateRange()
Creates a default range starting from the defaultTSDatewith 100 observations.
-
TSDateRange
public TSDateRange(TSDate t1, int numberOfObs)
CreatesTSDateRangefrom specified start date and number of observations.- Parameters:
t1-numberOfObs- the number of observations within the range- Throws:
java.lang.IllegalArgumentException- if numberOfObs <= 0
-
TSDateRange
public TSDateRange(TSDate t1, TSDate t2)
CreatesTSDateRangefrom specified start date and end date.- Parameters:
t1- theTSDateto start from (inklusive)t2- theTSDateto end with (inklusive)- Throws:
java.lang.IllegalArgumentException- ifupperBoundis earlier thanlowerBoundor sub periodicities do not match
-
-
Method Detail
-
addPeriodsToStart
public TSDateRange addPeriodsToStart(int periods)
Gets a newTSDateRangerange object with the start date shifted byperiods.- Parameters:
periods- the number of periods to shift the start date, can be negative, which results in an earlier start, 0 creates a copy of this- Returns:
- a new range object with the same end date and a new start date
- Throws:
java.lang.IllegalArgumentException- if new range would be invalid
-
commonRange
public TSDateRange commonRange(TSDateRange otherRange)
Returns the biggest date range that is common to this range and another range. If the two ranges do not overlap with at least a single date or if the sub periodicities do not match, null is returned.The function is symmetric in the sense that
x.commonRange(y).equals(y.commonRange(x)).- Parameters:
otherRange- the range to be checked- Returns:
- common range, null if no common range exists
- Throws:
java.lang.IllegalArgumentException- if argument is null
-
createSeasDum
public double[][] createSeasDum(boolean centered, boolean all)Creates seasonal dummy series asdouble[][], seecreateSeasDumTS(boolean, boolean).- Parameters:
centered- the sum over all dummy elements at every time index is 0, if this is true, thenallis ignored (andfalse)all- iftruethensub periodicitydummies are created, iffalsethensub periodicity - 1dummies are created- Returns:
- dummy data as
double[][],nullif sub periodicity == 1 or if not enough observations
-
createSeasDumTS
public TS[] createSeasDumTS(boolean centered, boolean all)
Creates seasonal dummy series according to the periodicity of this time series. The returned array contains the single dummy series as elements, where the 1st dummy is set to 1 always at the first possible sub period (for example Q1 for quarterly data) and the others respectively in ascending order.If the number of observations of this time series is less than the sub periodicity then
nullis returned, because the resulting array could contain a zero vector.- Parameters:
centered- the sum over all dummy elements at every time index is 0, if this is true, thenallis ignored (andfalse)all- iftruethensub periodicitydummies are created, iffalsethensub periodicity - 1dummies are created- Returns:
- array with the single dummy series,
nullif sub periodicity == 1 or if not enough observations
-
createShiftDum
public double[] createShiftDum(TSDateRange shiftRange)
Creates adouble[]that is a shift dummy for this range, seecreateShiftDumTS(java.lang.String, com.jstatcom.ts.TSDateRange).- Parameters:
shiftRange- the range within this range to specify the shift- Returns:
- the created shift dummy as
double[] - Throws:
java.lang.IllegalArgumentException- ifshiftRangeis not a valid range for this range
-
createTrendShiftDum
public double[] createTrendShiftDum(TSDateRange shiftRange)
Creates adouble[]that is a trend shift dummy for this range, seecreateTrendShiftDumTS(java.lang.String, com.jstatcom.ts.TSDateRange).- Parameters:
shiftRange- the range within this range to specify the trend shift- Returns:
- the created trend shift dummy as
double[] - Throws:
java.lang.IllegalArgumentException- ifshiftRangeis not a valid range for this range
-
createTrendShiftDumTS
public TS createTrendShiftDumTS(java.lang.String name, TSDateRange shiftRange)
Creates a time series that is a trend shift dummy for this range. A trend shift dummy has zeros outside the shift range and values [index + 1] within the shift range.- Parameters:
name- the name of the newTSshiftRange- the range within this range to specify the trend shift- Returns:
- the created trend shift dummy
- Throws:
java.lang.IllegalArgumentException- if name isnullor empty, or ifshiftRangeis not a valid range for this range
-
createShiftDumTS
public TS createShiftDumTS(java.lang.String name, TSDateRange shiftRange)
Creates a time series that is a shift dummy for this range. A shift dummy has zeros outside the shift range and values [1] within the shift range.- Parameters:
name- the name of the newTSshiftRange- the range within this range to specify the shift- Returns:
- the created shift dummy
- Throws:
java.lang.IllegalArgumentException- if name isnullor empty, or ifshiftRangeis not a valid range for this range
-
encloses
public java.lang.String encloses(TSDate date)
Checks whether a given date is within the range defined by this object. If not, an error message is returned.- Parameters:
date- the date to check for- Returns:
- null if within range, error string otherwise
-
encloses
public java.lang.String encloses(TSDateRange otherRange)
Checks whether a given date range is included within the range defined by this object. If not, an error message is returned.- Parameters:
otherRange- the range to check for- Returns:
- null if within range, error string otherwise
-
equals
public boolean equals(java.lang.Object o)
Overridesequalsto provide logical comparison of two differentTSDateRangeobjects.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- object to compare with- Returns:
trueif start and end dates are both equal,falseotherwise
-
format
public java.lang.String format(java.lang.String description, int space)Gets a nicely formatted version of this date range with a description and the number of observations, for example
sample range: [1960 Q3, 1982 Q4], T = 90
with "sample range:" asdescription.- Parameters:
description- a desriptive string that will be put in frontspace- the number of spaces betweendescriptionand the rest, useful for alignment, should be greater than 0, otherwise will be set to 0- Returns:
- the formatted string
-
dateForIndex
public TSDate dateForIndex(int index)
Returns the date corresponding to an index respective to the range.- Parameters:
index- can also specify value outside the range- Returns:
- the date for the index
-
indexForDate
public int indexForDate(TSDate date)
Computes the index that a date has respective to the range specified by this object, starting with 0.- Parameters:
date- theTSDateto compute the index for- Returns:
- 0 if date is equal to lower bound, difference of periods otherwise
- Throws:
java.lang.IllegalArgumentException- if subPeriodicities do not match or if date is null
-
indicesForRange
public int[] indicesForRange(TSDateRange otherRange)
Computes the indices that a date range has within the range specified by this object, starting with 0.- Parameters:
otherRange- the range to compute the bounds for- Returns:
- array with int[0] = index of lower bound within this range, int[1] = index of upper bound within this range
- Throws:
java.lang.IllegalArgumentException- if sub periodicities do not match or ifotherRangeis not in the defined range, because this usually means a programming error
-
lowerBound
public TSDate lowerBound()
Gets reference to start date of range.- Returns:
- the date this range starts from
-
getMergedRange
public static TSDateRange getMergedRange(TS[] tsArray)
Gets the merged range of all time series in the given array. The merge range starts from the earliest start date till the latest end date of all series.- Parameters:
tsArray- an array ofTSobjects- Returns:
- the range starting from the earliest till the latest observation over all time series in the array
- Throws:
java.lang.IllegalArgumentException- if oneTSisnullor if sub periodicities are different
-
numOfObs
public int numOfObs()
Returns number of observations represented by this range. Every range object contains at least one observation.- Returns:
- the number of obervations within this range
-
subPeriodicity
public int subPeriodicity()
Gets the sub periodicity of this date range. It quantifies the number of sub periods which constitute one main period.- Returns:
- the sub periodicity
-
upperBound
public TSDate upperBound()
Gets reference the last date of the range.- Returns:
- the date this range ends with
-
hashCode
public int hashCode()
OverrideshashCode(), because equals was overwritten. The algorithm was taken from (Joshua Bloch, "Effective Java", Item 8) .- Overrides:
hashCodein classjava.lang.Object- Returns:
- hash code
-
mergedRange
public TSDateRange mergedRange(TSDateRange otherRange)
Returns the merged date range that starts at the earliest start date and ends at the latest enddate. If the sub periodicities do not match, null is returned.The function is symmetric in the sense that
x.mergedRange(y).equals(y.mergedRange(x)).- Parameters:
otherRange- the range to merge with- Returns:
- the merged range
- Throws:
java.lang.IllegalArgumentException- if argument is null or if sub periodicities do not match
-
timeAxisStringArray
public java.lang.String[] timeAxisStringArray()
This method returns the range represented as an array of Strings, for example{"1960 Q1", "1960 Q2", ...., "1982 Q4"}.- Returns:
- time axis as string array
-
toString
public java.lang.String toString()
Returns a string representation of the specified range. Do not use for comparisons, but use equals instead.- Overrides:
toStringin classjava.lang.Object- Returns:
- string representing this range
-
valueOf
public static TSDateRange valueOf(java.lang.String exp)
Static initializer that creates aTSDateRangefrom a string representation. The string should be constructed asTSDate string , TSDate string, and can optionally be enclosed with[ ], for example[1960 Q1, 1982 Q4]1970, 20031953 6/12, 1994 1/121/1/60 (5), 10/3/73 (5)
1960.1, 1982.4is NOT possible, because it is not unique. For the string representation of aTSDateobject, see the documentation of itsvalueOfmethod.- Parameters:
exp- the string to parse to aTSDateRange- Returns:
- a
TSDateRangeobject created fromexp - Throws:
java.lang.IllegalArgumentException- if object could not be constructed from given string or ifexp == null- See Also:
TSDate
-
-
DMelt 3.0 © DataMelt by jWork.ORG