How to use monthly data to calculate quarterly average

I am trying to use a monthly data to calculate the quarter average, suppose now I randomly generate 10000 month, and try to use this to get the quarterly average by using the loop:

T = 10000
a=rand(Int,T) # monthly data
u=zeros(Int64((p.T - p.T %3) /3))  # indicate size of quartly data
for i ∈ 1:3:(T - T %3)
    u[i]=mean(a[i:i+3])
end 


I understand that the problem is the i, since the size for u and a is different. But I don’t know how tofix this

Is this what you’re looking for?

T = 10000
a = rand(Int,T) # monthly data
u = zeros(T ÷ 3)  # indicate size of quartly data
j = 1
for i ∈ 1:3:T-3
    u[j] = mean(a[i:i+3])
    j += 1
end 

I use integer division ÷ (\div<TAB>) and introduce the iteration index j into u for simplicity

Regarding previous post, there are possibly some typos in the loop: 1:3:(T-2) and in mean(a[i:i+2])?

Alternative one-liner:

[mean(view(a, i:min(i+2,T))) for i in 1:3:T] 
2 Likes

You’re dropping the last value (at index 10000). We’ve got 3334 quarters. Assuming that the last quarter contained only one month we could do:

T = 10000
a = rand(Int, T)
u = zeros(ceil(Int, T/3))  
a1 = [a; fill(last(a), 3-T%3)]  # complete the last, partial quarter with the last value
for (j, i) ∈ enumerate(1:3:T)
    u[j] = mean(a1[i:i+2])
end

Working with date and time can be messy. How about a missing month? How about our data start in the middle of a quarter? I would really make proper time stamps of type Date and handle the quarters with the Dates STDLIB. Also, TimeSeries.jl offers a collapse function that can handle quarterly data (see Combine methods · TimeSeries.jl).

The easiest in this case is to use the partition function. It also properly handles the last part, that is shorter than three elements.

julia> map(mean, Iterators.partition(a, 3))
3334-element Vector{Float64}:
<...>
3 Likes

Here an example with TimeSeries.jl. The data may start and end with partial quarters:

using Dates, Statistics, TimeSeries
T = 10000
d = firstdayofmonth(today())
timestamps = d-Month(T-1):Month(1):d  # range with the latest 10000 months
ta = TimeArray(timestamps, rand(Int, T))
u = collapse(ta, Dates.quarter, first, mean)

You may want to use Tsframes package - you can easily change the frequencies of time series.

Wow. That is a very old organization. Are you reviewing the finances of the Vatican?

2 Likes
vec(mean(reshape(@view(a[1:T-(T % 3)]), (3, :)); dims=1))

is efficient on my machine.

2 Likes