Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Delay and Smooth functions #18

Closed
JamesPHoughton opened this issue May 3, 2015 · 5 comments
Closed

Implement Delay and Smooth functions #18

JamesPHoughton opened this issue May 3, 2015 · 5 comments
Milestone

Comments

@JamesPHoughton
Copy link
Collaborator

A delay function must essentially create a number of hidden 'stocks' in the background of the model (one for each order of the delay). This requires some intelligent construction in the translation functions, and a clever ability to handle these delays.

Additionally, some types of analysis won't be quite as easy when delay stocks are hidden, (mostly things that modify the model step-at-a-time) so we'll need to think about that some as well.

@JamesPHoughton
Copy link
Collaborator Author

This is mostly implemented, some of the weird cases (Smooth3I, for example) aren't fully elaborated yet.

After we implement subscripts, it may make sense to rework the implicit stocks in a delay or smooth function to use the subscript syntax, just for ease of use.

@JamesPHoughton JamesPHoughton added this to the 1.0 milestone Feb 3, 2016
@JamesPHoughton
Copy link
Collaborator Author

Now that there is some meat on the subscripting bones, we can address delays using subscripted functions, and hopefully get some of the benefits of array mathematics. What we essentially need to do is add a dimension to a copy of the delayed variable.

@JamesPHoughton JamesPHoughton changed the title Implement 'Macro' style functions (delays, primarily) Implement Delay and Smooth functions Jun 28, 2016
@JamesPHoughton
Copy link
Collaborator Author

Just to have a quick record, here's the basic algorithm of the delay function.

First define the result that will be delayed:

inval = xr.DataArray(data=[1,2,3], coords={'Dim1':['A','B','C']})
<xarray.DataArray (Dim1: 3)>
array([1, 2, 3])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C'

Now create an empty delay structure to track values through the delay sequence:

order = 4
a = xr.DataArray(data=np.tile(0, [len(inval), order]),
                 coords={'Dim1':['A','B','C'], '_delay': range(order)})
<xarray.DataArray (Dim1: 3, _delay: 4)>
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C'
  * _delay   (_delay) int64 0 1 2 3

Now work out the derivative for the delayed stock.

delay = 8
ddt = (a/(delay/order)).shift(**{'_delay': 1}).fillna(inval) - (a/(delay/order))
<xarray.DataArray (Dim1: 3, _delay: 4)>
array([[ 1.,  0.,  0.,  0.],
       [ 2.,  0.,  0.,  0.],
       [ 3.,  0.,  0.,  0.]])
Coordinates:
  * Dim1     (Dim1) |S1 'A' 'B' 'C'
  * _delay   (_delay) int64 0 1 2 3

I haven't done any optimization here, so in the future, that work remains to be done.

@JamesPHoughton
Copy link
Collaborator Author

A few other tests we should implement and try:

@JamesPHoughton
Copy link
Collaborator Author

Existing tests pass as of 2a41b2f, so closing this issue. If we find bugs we'll open another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant