Estimating a trimmed mean

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

rahildevgan
Posts: 7
Joined: Wed Jul 08, 2015 8:40 pm

Estimating a trimmed mean

Postby rahildevgan » Mon Aug 31, 2015 12:47 am

Hi,

I would like to estimate a trimmed mean inflation measure and thought whether it was easily doable in EViews rather than R. Essentially what I have to do is this:

DATA:
1) Time series of CPI weights
2) Time series of monthly changes of components

For each i, I need to:
a) sort a row of data (the row containing all the monthly changes of components)
b) order that row by magnitude
c) delete the top x and bottom x observations
d) use the remaining data to multiply with the row of weights (sorted similarly/simultaneously) and calculate the monthly change

Can this be done in a relatively easy way? Would be grateful for any help.

Example: I have 50 components and 10 months of data.
- weights[j] where i=1 to 50, j = 1 to 10
- monthly changes mom[j] where i =1 to 50, j = 1 to 10

(for i = 1 to 10) #i need to cycle through the months
{
for (j= 1 to 50) #loop through the components
{
sort(mom[j]) (order the monthly changes)
then AT THE SAME TIME sort the weights[j]
}
}
then cut off the top x and bottom x values
then multiply remaining mom values by weights

Is this too complicated to be done in eviews? Thanks a lot!

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: Estimating a trimmed mean

Postby EViews Gareth » Mon Aug 31, 2015 8:23 am

It is possible if you convert the series into matrices, but not particularly pleasant.

If you're used to R, it might be easier for you to use xopen/xput/xrun to quickly send the series over to R, do the calculations there, then bring them back.

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Estimating a trimmed mean

Postby EViews Glenn » Mon Aug 31, 2015 8:53 am

I think there's a way to do this in EViews. Let me give it some thought when I get into the office.

[edit: can you describe in a bit more detail how your workfile is set up?]

rahildevgan
Posts: 7
Joined: Wed Jul 08, 2015 8:40 pm

Re: Estimating a trimmed mean

Postby rahildevgan » Wed Sep 02, 2015 1:57 am

Thanks for the reply! Sure I will try my best because I ran into a bit of trouble in R.

The main trouble i'm facing is "sorting one row on the basis of another". My workfile essentially would have:

1) matrix (X) of CPI component weights (dimensions [7][299] because I have 7 months of data and 299 components)
2) matrix (Y) of CPI component m/m% changes (dimensions same as above)

what i need to do is:
1) loop through the months (1 to n) (in this case n=7)
2) for each month, i need to sort Y from smallest to largest values but simultaneously, the corresponding row/month in X needs to be reordered/sorted as well.
3) then i need to 'trim' the re-ordered row (remove the top and bottom x%) and sum up the cross-product (basically multiply the m/m% changes (Y) by weights (X) and then sum them to get the monthly contribution)

Step 2 is the crucial issue which i'm having trouble with. It seems to be impossible in eviews. Thanks in advance for any help!

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Estimating a trimmed mean

Postby EViews Glenn » Wed Sep 02, 2015 8:46 am

Should be easy to do without the sorting using a couple of by-statistics functions.

Can you post your workfile here? It's easier to illustrate if I have your data. Best if your data is panel structured, but we'll address that when I see your workfile.

rahildevgan
Posts: 7
Joined: Wed Jul 08, 2015 8:40 pm

Re: Estimating a trimmed mean

Postby rahildevgan » Sun Sep 06, 2015 11:38 pm

Thanks a lot! Sorry for the delayed response.

group_weights is matrix X, group_mom is matrix Y. do let me know if other information, is there any way i can loop and name?
Attachments
in_cpi_trim.wf1
(926.67 KiB) Downloaded 490 times

speruzzo
Posts: 1
Joined: Fri Sep 04, 2015 7:46 am

Re: Estimating a trimmed mean

Postby speruzzo » Mon Sep 07, 2015 4:31 pm

Thanks rahildevgan and Glenn,

I am actually trying to address exactly the same estimation and very keen to read your followups.

All the best.

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Estimating a trimmed mean

Postby EViews Glenn » Tue Sep 08, 2015 10:16 pm

Open your workfile, and run the following program snippet:

Code: Select all

delete group_weights rename _*_weight weight* group group_weights weight?? pagestack series? @ *? * series high = @quantilesby(series, @date, .95) series low = @quantilesby(series, @date, .05) series wsums = @sumsby(series*weight, @date, "if series>low and series<high") series wcounts=@sumsby(1, @date, "if series>low and series<high")
The first block reshapes the workfile page into a panel structure by stacking the weight and the data series. The naming convention for the weight series differed from the convention for the data (which is a problem when stacking) so I've renamed all of those series. I think this matches how the data are structured; my apologies if I misunderstood the structure.

The second block is the actual computation. As I said, it's only a couple of lines. First we computed quantiles for the data for each date (month) in the stacked workfile. Note that the quantiles use the Cleveland definition as described in the manual. You may have to adjust the quantile values slightly to get the trimming percentages to match exatly what you want. Here, we're computing quantiles for the approximate highest 5% and lowest 5%.

We then compute the weighted sums for each period over a restricted sample using the quantile values computed above. I've also computed a counts equivalent so you can see the number of observations used in each quantile calculation. Note that the @sumsby will match merge the weighted sums to each period, repeating the values for a given period across all of the cross-section units.

I think this is what was requested. Let me know if anything is unclear.

akd
Posts: 1
Joined: Mon Nov 02, 2020 10:36 am

Re: Estimating a trimmed mean

Postby akd » Mon Nov 02, 2020 11:16 am

This looks like the trimming is done without reference to the weights, and then a weighted average is taken of the remaining items.

How would this be done if we wanted to account for weights in the trimming process? For example, say we want to take the inflation rate of the middle 50% of the consumption basket, the weight series are the consumption shares, and the series "series" are the item inflation rates. I don't see how to do that in eviews.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests