kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Simulation open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Simulation

6.2.10

PyBullet 3.22

Tiramisconstrued

v2.5.0.1

v0.7.5-rc1

mongoose

6.2.10

bullet3

PyBullet 3.22

abstreet

Tiramisconstrued

Locale-Emulator

v2.5.0.1

a32nx

v0.7.5-rc1

Popular Libraries in Simulation

Trending New libraries in Simulation

Top Authors in Simulation

1

50 Libraries

235

2

13 Libraries

1284

3

13 Libraries

1396

4

13 Libraries

281

5

11 Libraries

140

6

11 Libraries

810

7

10 Libraries

45

8

9 Libraries

20

9

9 Libraries

1399

10

8 Libraries

100

1

50 Libraries

235

2

13 Libraries

1284

3

13 Libraries

1396

4

13 Libraries

281

5

11 Libraries

140

6

11 Libraries

810

7

10 Libraries

45

8

9 Libraries

20

9

9 Libraries

1399

10

8 Libraries

100

Trending Kits in Simulation

No Trending Kits are available at this moment for Simulation

Trending Discussions on Simulation

    Reverting a linear filter for time series in R
    Why should I use normalised units in numerical integration?
    Dramatic drop in numpy fromfile performance when switching from python 2 to python 3
    ValueError: Layer "sequential" expects 1 input(s), but it received 10 input tensors
    Convolution Function Latency Bottleneck
    Write custom metadata to Parquet file in Julia
    Floating point inconsistencies after upgrading libc/libm
    Missing types, namespaces, directives, and assembly references
    Iterating over an array of class objects VS a class object containing arrays
    R: Trying to recreate mean-median difference gerrymander tests

QUESTION

Reverting a linear filter for time series in R

Asked 2022-Mar-28 at 17:50

I'm using the stats::filter function in R in order to understand ARIMA simulations in R (as in the function stats::arima.sim) and estiamtion. I know that stats::filter applies a linear filter to a vector or time series, but I'm not sure how to "unfilter" my series.

Consider the following example: I want to use a recursive filter with value 0.7 to my series x = 1:5 (which is essentially generating an AR(1) with phi=0.7). I can do so by:

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11

Which returns me essentially c(y1,y2,y3,y4,y5) where:

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11y1 <- x[1]                                            
12y2 <- x[2] + ar*y1      
13y3 <- x[3] + ar*y2 
14y4 <- x[4] + ar*y3 
15y5 <- x[5] + ar*y4 
16

Now imagine I have the y = c(y1,y2,y3,y4,y5) series. How can I use the filter function to return me the original series x = 1:5?

I can write a code to do it like:

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11y1 <- x[1]                                            
12y2 <- x[2] + ar*y1      
13y3 <- x[3] + ar*y2 
14y4 <- x[4] + ar*y3 
15y5 <- x[5] + ar*y4 
16unfilt <- rep(NA, 5)
17unfilt[1] <- filt[1]
18
19for(i in 2:5){
20   unfilt[i] <- filt[i] - ar*filt[i-1]
21}
22unfilt
23[1] 1 2 3 4 5
24

But I do want to use the filter function to do so, instead of writing my own function. How can I do so? I tried stats::filter(filt, -ar, method="recursive"), which returns me [1] 1.0000 2.0000 3.4900 4.9800 6.7101 not what I desire.

ANSWER

Answered 2022-Mar-28 at 17:50

stats::filter used with the recursive option is a particular case of an ARMA filter.

copy icondownload icon

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11y1 <- x[1]                                            
12y2 <- x[2] + ar*y1      
13y3 <- x[3] + ar*y2 
14y4 <- x[4] + ar*y3 
15y5 <- x[5] + ar*y4 
16unfilt <- rep(NA, 5)
17unfilt[1] <- filt[1]
18
19for(i in 2:5){
20   unfilt[i] <- filt[i] - ar*filt[i-1]
21}
22unfilt
23[1] 1 2 3 4 5
24a[1]*y[n] + a[2]*y[n-1] + … + a[n]*y[1] = b[1]*x[n] + b[2]*x[m-1] + … + b[m]*x[1]  
25

You could implement this filter with the signal package which allows more options than stat::filter :

copy icondownload icon

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11y1 <- x[1]                                            
12y2 <- x[2] + ar*y1      
13y3 <- x[3] + ar*y2 
14y4 <- x[4] + ar*y3 
15y5 <- x[5] + ar*y4 
16unfilt <- rep(NA, 5)
17unfilt[1] <- filt[1]
18
19for(i in 2:5){
20   unfilt[i] <- filt[i] - ar*filt[i-1]
21}
22unfilt
23[1] 1 2 3 4 5
24a[1]*y[n] + a[2]*y[n-1] + … + a[n]*y[1] = b[1]*x[n] + b[2]*x[m-1] + … + b[m]*x[1]  
25a = c(1,-ar)
26b = 1
27
28filt_Arma <- signal::filter(signal::Arma(b = b, a = a),x)
29filt_Arma
30
31# Time Series:
32# Start = 1 
33# End = 5 
34# Frequency = 1 
35# [1]  1.0000  2.7000  4.8900  7.4230 10.1961
36
37identical(filt,filt_Arma)
38# [1] TRUE
39

Reverting an ARMA filter can be done by switching b and a, provided that the inverse filter stays stable (which is the case here):

copy icondownload icon

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11y1 <- x[1]                                            
12y2 <- x[2] + ar*y1      
13y3 <- x[3] + ar*y2 
14y4 <- x[4] + ar*y3 
15y5 <- x[5] + ar*y4 
16unfilt <- rep(NA, 5)
17unfilt[1] <- filt[1]
18
19for(i in 2:5){
20   unfilt[i] <- filt[i] - ar*filt[i-1]
21}
22unfilt
23[1] 1 2 3 4 5
24a[1]*y[n] + a[2]*y[n-1] + … + a[n]*y[1] = b[1]*x[n] + b[2]*x[m-1] + … + b[m]*x[1]  
25a = c(1,-ar)
26b = 1
27
28filt_Arma <- signal::filter(signal::Arma(b = b, a = a),x)
29filt_Arma
30
31# Time Series:
32# Start = 1 
33# End = 5 
34# Frequency = 1 
35# [1]  1.0000  2.7000  4.8900  7.4230 10.1961
36
37identical(filt,filt_Arma)
38# [1] TRUE
39signal::filter(signal::Arma(b = a, a = b),filt)
40
41# Time Series:
42# Start = 2 
43# End = 6 
44# Frequency = 1 
45# [1] 1 2 3 4 5
46

This corresponds to switching numerator and denominator in the z-transform:

copy icondownload icon

1x    <- 1:5 
2ar   <-0.7 
3filt <- filter(x, ar, method="recursive")
4filt 
5
6Time Series:
7Start = 1 
8End = 5 
9Frequency = 1 
10[1]  1.0000  2.7000  4.8900  7.4230 10.1961
11y1 <- x[1]                                            
12y2 <- x[2] + ar*y1      
13y3 <- x[3] + ar*y2 
14y4 <- x[4] + ar*y3 
15y5 <- x[5] + ar*y4 
16unfilt <- rep(NA, 5)
17unfilt[1] <- filt[1]
18
19for(i in 2:5){
20   unfilt[i] <- filt[i] - ar*filt[i-1]
21}
22unfilt
23[1] 1 2 3 4 5
24a[1]*y[n] + a[2]*y[n-1] + … + a[n]*y[1] = b[1]*x[n] + b[2]*x[m-1] + … + b[m]*x[1]  
25a = c(1,-ar)
26b = 1
27
28filt_Arma <- signal::filter(signal::Arma(b = b, a = a),x)
29filt_Arma
30
31# Time Series:
32# Start = 1 
33# End = 5 
34# Frequency = 1 
35# [1]  1.0000  2.7000  4.8900  7.4230 10.1961
36
37identical(filt,filt_Arma)
38# [1] TRUE
39signal::filter(signal::Arma(b = a, a = b),filt)
40
41# Time Series:
42# Start = 2 
43# End = 6 
44# Frequency = 1 
45# [1] 1 2 3 4 5
46Y(z) = a(z)/b(z) X(z)
47
48X(z) = b(z)/a(z) Y(z)
49

Source https://stackoverflow.com/questions/71623626