Generating date/time data: date of birth, ordering time

Generating date/time data: date of birth, ordering time

In business simulation, especially when you want to generate financial data, you need to generate a lot of dates. You need to track the billing date, the customer birthday, the ordering time, the delivery time. There are several ways to generate date/time data.

Days from one point

If you want to generate date values from one point onwards, you just have to use the add-day function with random numbers. So if U is a uniform random variable, then D = toDate("31-12-2017") + U will be a random date variable. You can draw the histogram to see the uniformity of the variable.

https://raw.githubusercontent.com/tutrunghieu/sharing/master/bstime/bst1-left-len.R

Fiscal month/year enumeration

For financial records, normally we do not generate random samples but we get periodic samples with regular intervals. For example, we want to get every months from year 2017 to year 2019. We will use the month loop variable and the year loop variable to complete the task.

https://raw.githubusercontent.com/tutrunghieu/sharing/master/bstime/bst2-enum.R

Rejection method

You can generate three independent variables, year within the range you want, month from 1 to 12, and day from 1 to 31, then you reject the invalid dates, and we have the date/time values.

https://raw.githubusercontent.com/tutrunghieu/sharing/master/bstime/bst3-reject.R

Conclusion

You can think of date/time data as a vector of three components (year, month, day) . Therefore you can sample three components independently and reject the invalid combination.

However, you can treat date as one single value by using center/radius method or blending methods to generate date/time values.


要查看或添加评论,请登录

Henry T.H. Tu的更多文章

社区洞察

其他会员也浏览了