JuliaDynamics / ChaosTools.jl

Tools for the exploration of chaos and nonlinear dynamics
https://juliadynamics.github.io/DynamicalSystemsDocs.jl/chaostools/stable/
MIT License
187 stars 35 forks source link

Massive reform of all basins-related code #248

Closed Datseris closed 2 years ago

Datseris commented 2 years ago

cc @awage and @KalelR . Also see the latest status quo of code in PR #237

so I've been pondering over our basins code, and many things are suboptimal. Not in terms of performance, but in terms of code clarity, things being where they do not really belong, and code duplication. So I will outline here a massive code reform that is also inspired by the new AttractorMapper functionality, which will itself be given to the basin_fractions function.

Once I'm finished working on PR #237 things will become even more clear, but I'm writing my notes here anyways.

Definition of Proximity Version. By "proximity version" I refer to the algorithm that maps initial conditions to attractors based on the distance of the current trajectory state from the attractors. The first attractor that is < ε from the current state gets mapped as the attractor of the initial condition. This is called the "supervised method" so far in our code.



As you can see, this is a lot of work, but it will be a huge advantage to the library to do these changes. It will make everything simpler, and easier to extend. Also, it will create transferable functionality. E.g., at the moment this nice handling of projected systems can only be used within the basins, but it may be useful in other algorithms as well. Any help is welcomed!

KalelR commented 2 years ago

The first attractor that is < ε from the current state gets mapped as the attractor of the initial condition.

Is it the first attractor, or the one closest to the current state?

The supervised method in basin_fractions follows directly from the bSTAB paper, where they use a first nearest neighbors algorithm ( kNN, with k=1). So it identifies the attractor ("template" in the code) which is closest to the current state ("features"). That would be the difference to "proximity version", if the latter finds the first attractor, not the closest. And its an important difference, finding the closest attractor makes the most sense Id say. If proximity version finds the closest attractor, then there should be no difference. The only practical scenario I can think for this supervised scenario is the obvious, when the user knows the features defining the attractors, and possibly wants to be sure these are ones the algorithm finds/uses.

Otherwise, I really have no clue if this 1NN is less performant than the "proximity version". What I can say from the tests I made is that it is really quick. Much quicker than the unsupervised method, if there are lots of ics. It also allows the user to pass other distance metrics and a maximum distance threshold. I guess "proximity version" could implement these without much difficulty, so I don't see any real advantage to the proximity version in this performance sense, except kNN is already implemented.

awage commented 2 years ago

Is it the first attractor, or the one closest to the current state?

This the one closest to the current state.

If proximity version finds the closest attractor, then there should be no difference. The only practical scenario I can think for this supervised scenario is the obvious, when the user knows the features defining the attractors, and possibly wants to be sure these are ones the algorithm finds/uses.

The difference is that the "proximity version" checks the distances at each time-step and stops when the distance falls bellow ε. When using the feature, you first integrate the full trajectory, compute the feature vector and then match it to the template. I think this approach can be more robust in some cases (ill defined attractors or two attractors very nearby points in the state space).

awage commented 2 years ago

As you can see, this is a lot of work.

You love titanic tasks!

Any help is welcomed!

Sure! Happy to contribute. Tell me what you need.

Datseris commented 2 years ago

That would be the difference to "proximity version", if the latter finds the first attractor, not the closest.

No. I mean, yes, technically. It maps a trajectory to the first attractor whose distance to the trajectory is < ε. But this happens DURING the time evolution of the orbit. So it is almost certainly guaratneed that the first attractor with distance < ε is also the closest. Alex described it well. Notice that by default ε is really small, 1e-3.

As far as I can tell, the proximity version is superior to the "supervised featurizing version" for the following reasons:


except kNN is already implemented.

Yes. this is a good point. Actually, I can imagine a scenario where one would prefer the features: for extremely high-dimensional dynamical systems, e.g. 100s or 1000s of dimensions. There our approach, which uses KDTrees, would become visibly slow, because KDTrees do not perform so well in such high dimensions. However, the featurizing would typically project the system to 1-10 dimensional space. So yes, I agree, we will keep this version.


You love titanic tasks!

Absolutely! haha, but that's how you make great things! Similarly to re-writing a paper 3 times until you get it right. Fun fact: I have re-written DynamicalSystems.jl twice in its early stages. Looking back, it was a great choice!!!


Sure! Happy to contribute. Tell me what you need.

The first two steps is to make (1) the ProjectedSystem structure and (2) the StroboscopicMap structure. The first should be a wrapper that contains the dynamical system and the complete_state business and then defines an integrator method that makes a new wrapper around new_integ = integrator(projected_system.ds) so that get_state(new_integ) = projection(get_state(new_integ.integ)). StroboscopicMap is pretty much the same as the PoincareMap.

Datseris commented 2 years ago

@awage some quick comments:

  1. Please add the stroboscopic map code in DynamicalSystemsBase.jl
  2. Make a dedicated file for it
  3. Do not worry at the moment for making Stroboscopic map work with the basins code. The stroboscopic map is a feature on its own that the basins must utilize. hence, the basins needs to be adapted after teh stroboscopic code is merged in DynamicalSystemsBase.jl.
KalelR commented 2 years ago

I see, thanks for the explanations, guys! I have a question: for systems of oscillators with multistability there are several attractors in a bounded state space. Would "proximity version" work well in finding the correct attractor?

As far as I can tell, the proximity version is superior to the "supervised featurizing version" for the following reasons:

Yes, those are very good points.

Actually, I can imagine a scenario where one would prefer the features: for extremely high-dimensional dynamical systems, e.g. 100s or 1000s of dimensions.

Ah, excellent point. In systems of oscillators for instance some good features are very clear, like the degree of synchronization. But their values at the attractors are generally unknown, so the supervised version would not be better anyway. As far as I understand, Euclidian distance in high dimensional spaces can be problematic, which could be an issue besides just the perfomance of KDTree. So supervised version can come in handy, though I guess not as often as proximity version. Since it is already implemented, I see no reason to remove it.

Datseris commented 2 years ago

This hjas been implemented in the ongoing #237