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

Calculate and return diagonal calculations for nth diagonal measures #126

Open
rs7q5 opened this issue Dec 5, 2021 · 14 comments
Open

Calculate and return diagonal calculations for nth diagonal measures #126

rs7q5 opened this issue Dec 5, 2021 · 14 comments
Labels
wanted feature Feature we would like to have!

Comments

@rs7q5
Copy link
Contributor

rs7q5 commented Dec 5, 2021

Would like to return results on certain nth diagonals of a recurrence matrix.

tau_recurrence only calculates the average recurrence rate on the nth diagonal (averages the RR on the nth diagonal above and nth diagonal below), but I would like it to return these measures separately and maybe only up to the nth diagonal.

Other metrics at particular tau values like average diagonal length could also maybe be implemented.

@Datseris Datseris added the wanted feature Feature we would like to have! label Dec 5, 2021
@Datseris
Copy link
Member

Datseris commented Dec 5, 2021

yeah for cross recurrence plots especially it makes more sense to separate up and down diagonals than not. For up to the nth diagonal, why can't you do this already via cumsum?

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 5, 2021

I guess you could with cumsum, but I was referring to only returning the first +/- of the nth diagonal, not the sum of it. It would be to plot the value of each individual diagonal.

@Datseris
Copy link
Member

Datseris commented Dec 5, 2021

I think this needs to be a different function alltogether. It isn't the recurrence rate, but you don't want the histograms given by recurrecenstructures either. Point is, does this actually need to be a function? Because you just do [R[i+k, i] for i in 1...] to get the k-th diagonal.

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 5, 2021

Were you referring to cumsum on each diagonal? or cumsum of the rr_τ τ=0:k? Either way I don't disagree this should not be a separate function, but I do think it should be a function, because as you mentioned it makes more sense to separate up and down diagonals.

I have not tried your for loop solution, but I would assume that it would work. I was just trying to use what was mostly already written. I think the big change would be in where the value is stored in rr_τ. As you iterated you would always have += 1/(n-|d|), but depending on if d is 0, <0, or >0 would depend on where it is stored. Note d=r-col instead of the absolute value. Now whether you stored this in separate vectors or one vector in some index fashion I'm not sure, could do +/- or indices 2:2:end-1 d<0 and 3:2:end is d>0 and the first index is d=0, I'm not sure.

I would also assume this same concept would apply to getting the average or max diagonal length on a kth diagonal. But I have not actually thought through any implementation of this to be honest. I figured one step at a time though.

@Datseris
Copy link
Member

Datseris commented Dec 5, 2021

yeah best to just make this a function. It can return a dictionary mapping indices away from diagonal (with positive above, negative below) to vector of diagonal line lengths contained in the given scanned diagonal. Is this okay? Similarly with all other functions, an l_min would make it so that only lines of length > than that are stored.

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 5, 2021

that seems sufficient. I think it would just have to be clear what is above or below. Since mathematically diag(x,-1) is the first diagonal above the matrix x. In other words if one were to run the calculation using diag(x,-1) they should get the value at some expected index.

I am guessing a separate function would also be necessary for the mean or max length on the kth diagonal as well? Or since you would need the same histogram for either of those parameters?

@Datseris
Copy link
Member

Datseris commented Dec 5, 2021

just return vector of lengths for each scanned diagonal. Then mean or max can be computed as derivatives without the need of functions provided by this package. same with histograms

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 5, 2021

wouldn't the lengths still need to be returned using _linehistograms or are you referring to just using something like cumsum and then using the mean and derivative on each diagonal? I figured pulling diag(R,k) or something was super inefficient.

@Datseris
Copy link
Member

Datseris commented Dec 5, 2021

I think we are on different pages and have lost each other now. Or at least, I have for sure lost you...

Can you please explain as precisely as possible, exactly what you would like to have, given a recurrence matrix R? R is a boolean square matrix with entries being either 0 or 1 (or true or false).

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 6, 2021

I thought that may be the case.

I want to know the recurrence rate and eventually the average/max diagonal length at each diagonal of the recurrence matrix R. I think we have figured out the recurrence rate issue earlier in the discussion.

In other words I want to apply _dl_average and _dl_max to each diagonal of R. The output would be a vector of each of the parameters that is calculated at each diagonal. I plan to plot the resulting vector where the x-axis thus becomes the number of diagonals (e.g. lags) above or below the LOI.

Let me know if that doesn't clear anything up. Sorry for the confusion.

--
P.S. As a quick MWE (I just thought up for a single diagonal) of what would give the output for the average length of a single diagonal length is below. I'm sure there is a much more efficient and concise way to calculate this though.

val,len = rle(diag(R,-1)) #rle is from `StatsBase, but could probably use cumsum or something instead
bins = [0]
nbins = 1
for idx=1:length(val)
    if val[idx]==1 #add an extra condition with `lmin`?
            nbins = RecurrenceAnalysis.extendhistogram!(bins,nbins,len[idx])
    end
end
result = RecurrenceAnalysis._dl_average(bins) #have a value here for each diagonal above/below in `R`

@Datseris
Copy link
Member

Datseris commented Dec 6, 2021

In other words I want to apply _dl_average and _dl_max to each diagonal of R. The output would be a vector of each of the parameters that is calculated at each diagonal. I plan to plot the resulting vector where the x-axis thus becomes the number of diagonals (e.g. lags) above or below the LOI.

Yeah, and I am saying, let's not do that. Instead let's simply collect all diagonal lengths in a given diagonal and return them as a vector, for each diagonal of the matrixs. This way, you can trivially get the average or max by doing [mean(x) for x in diagonal_lengths] or even the entropy. Here diagonal_lengths is a vector of vectors or a vector valued dictionary.

The existing functions we have are not appropriate to do this because they directly go to mean and/or frequency estimation while we want the entire thing so you can do stuff per diagonal instead.

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 6, 2021

Oh okay, yeah I mean if it returns a vector of vectors of info on each diagonal that's fine, I guess I just don't know why you also wouldn't give the user the option and just go ahead and return the vector of [mean(x) for x in diagonal_lengths] along with other options (granted I guess that's your point, is you can do whatever you want with it once you have those lengths...).

@Datseris
Copy link
Member

Datseris commented Dec 6, 2021

because it costs nothing to write this extra 1 line of code if you need it, while it costs plenty to support one more API function.

@rs7q5
Copy link
Contributor Author

rs7q5 commented Dec 7, 2021

That's a very valid point, I didn't think about that....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wanted feature Feature we would like to have!
Projects
None yet
Development

No branches or pull requests

2 participants