Calendar
The functions described in this section are utilities, but they are extremely important. There is no way to perform finance computations correctly without reliable calendar functions. The Finance Pack has to follow the conventions of the industry, therefore, it does not use the functions from the standard Calendar package.
This loads the subpackage Calendar. We don't need to do this if we previously loaded Finance`.
In[1]:=<< Finance`Calendar`
Check what new functions are now available.
In[2]:=?Finance`Calendar`*
BaseYear DayOfWeek DaysBetween ToCalendar ToStringDate
DateQ DayOfYear LeapYearQ ToJulian YearsBetween
DayCountBasis
Every function has an on-line help message.
In[3]:=?LeapYearQ
Dates can be represented using any of three formats.
The date is represented by a list of three integers {month, day, year}. For example, {10, 9, 1987} denotes October 9, 1987. This is the "calendar date".
The date is represented by the Julian date number. For example, the "Julian date" for October 9, 1987 is 2447078. This format is particularly useful for counting days.
The date is represented by a six-digit string "yymmdd". The first two digits represent the year, the middle two give the month, and the last two denote the day of the month. This is called the "string date".
The string date representation is limited because it only covers one hundred years. For finance calculations, the first part of the twentieth century is usually unimportant compared to the first part of the twenty-first century. Therefore, we count the years from a given base year. If the year part of the string date is less than the base year, we assume that this date is in the twenty-first century. The default value of the base year is 1950. Therefore "480508" and "820622" represent May 8, 2048, and June 22, 1982, respectively. You will soon see how to choose a different base year. Strictly speaking, "yymmdd" represents the calendar date {mm, dd, 19yy} if yy > BaseYear 1900 and {mm, dd, 20yy} if yy BaseYear 1900.
Calendar test functions.
While any nonnegative integer may be interpreted as a Julian date, it is possible that a six-digit string or a list of three elements does not represent any date. For example, "921330" and {2, 29, 1993} are not dates. The function DateQ checks whether a given string or list really represents a date. The function LeapQ tests for leap years.
1992 and 2000 are both leap years.
In[4]:=LeapYearQ[1992]
Out[4]=
In[5]:=LeapYearQ[2000]
Out[5]=
1993 and 2100 are not leap years.
In[6]:=LeapYearQ[1993]
Out[6]=
In[7]:=LeapYearQ[2100]
Out[7]=
This is a valid date.
In[8]:=DateQ[{1, 13, 1993}]
Out[8]=
No month has 32 days so {8, 32, 1995} is not a valid calendar date.
In[9]:=DateQ[{8, 32, 1995}]
Out[9]=
1993 is not a leap year, therefore, February has only 28 days.
In[10]:=DateQ[{2, 29, 1993}]
Out[10]=
The string "991201" represents a date, namely December 1, 1999.
In[11]:=DateQ["991201"]
Out[11]=
The string "981301" does not represent a valid date.
In[12]:=DateQ["991301"]
Out[12]=
Calendar conversion functions.
It is often necessary to convert from one date format to another. The Calendar subpackage provides the ToJulian, ToCalendar, and ToStringDate conversion functions. For string dates all three conversion functions work from January 1, BaseYear to December 31, (BaseYear 99), where the default value for BaseYear is 1950.
This gives the Julian date for February 1, 1993.
In[13]:=ToJulian[{2, 1, 1993}]
Out[13]=
The function ToCalendar converts a Julian date to a calendar date.
In[14]:=ToCalendar[%]
Out[14]=
Option for ToCalendar and ToJulian.
Converting from a string date depends on the base year, so the ToCalendar and ToJulian functions have the option BaseYear.
The string date "510101" represents January 1, 1951 since the default for BaseYear is 1950.
In[15]:=ToCalendar["510101"]
Out[15]=
If 1951 is chosen as the base year, "500101" represents January 1, 2050.
In[16]:=ToCalendar["500101", BaseYear -> 1951]
Out[16]=
Here is the Julian date representation of June 22, 1982.
In[17]:=ToJulian["820622"]
Out[17]=
This converts a calendar date to a string date.
In[18]:=ToStringDate[{10, 9, 1987}]
Out[18]=
Here is an example of conversion from a Julian date to a string date.
In[19]:=ToStringDate[2445143]
Out[19]=
Calendar functions.
Option for DaysBetween and YearsBetween.
The functions DaysBetween and YearsBetween have the option DayCountBasis.
In[20]:=Options[DaysBetween]
Out[20]=
In[21]:=Options[YearsBetween]
Out[21]=
This returns the number of days between January 13, 1992 and August 13, 1995.
In[22]:=DaysBetween[{8, 31, 1995}, {1, 13, 1992}]
Out[22]=
Using the 30/360 day count basis we get a different number of days.
In[23]:=DaysBetween[{8, 13, 1995}, {1, 13, 1992}, DayCountBasis -> "30/360"]
Out[23]=
We can change the default value of DayCountBasis globally.
In[24]:=SetOptions[DaysBetween, DayCountBasis -> "30/360"]
Out[24]=
Now Mathematica uses the "30/360" day count basis as the default.
In[25]:=DaysBetween[{8, 13, 1995}, {1, 13, 1992}]
Out[25]=
We can still use the "Actual/Actual" day count basis, but we have to specify it explicitly.
In[26]:=DaysBetween[{8, 13, 1995}, {1, 13, 1992}, DayCountBasis -> "Actual/Actual"]
Out[26]=
We return to the original setting.
In[27]:=SetOptions[DaysBetween, DayCountBasis -> "Actual/Actual"]
Out[27]=
This gives the number of years between January 13, 1992 and August 13, 1995.
In[28]:=YearsBetween[{8, 31, 1995}, {1, 13, 1992}]
Out[28]=
Here is a numerical approximation of the above result.
In[29]:=N[%]
Out[29]=
We compute the same interval in the 30/360 day count basis.
In[30]:=YearsBetween[{8, 31, 1995}, {1, 13, 1992}, DayCountBasis -> "30/360"]
Out[30]=
Here is a numerical approximation of the above result.
In[31]:=N[%]
Out[31]=
In 1994, Independence Day will be on a Monday.
In[32]:=DayOfWeek[{7, 4, 1994}]
Out[32]=
March 1 is day 61 of a leap year and day 60 of a non-leap year.
In[33]:=DayOfYear[{3, 1, 1992}]
Out[33]=
In[34]:=DayOfYear[{3, 1, 1993}]
Out[34]=
|