-
Notifications
You must be signed in to change notification settings - Fork 30
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
[Feature] Probability of Direction (pd) #428
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||||||||||||||||||||||||
function p_direction(x::Vector{Float64}) | ||||||||||||||||||||||||||||
return maximum([sum(x .> 0) ./ length(x), sum(x .< 0) ./ length(x)]) | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this creates a few unnecessary allocations (the array inside of
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am in disbelief that this code is more efficient than the above one-liner, but I checked with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The one-liner creates quite a few allocations (zero allocations in the alternative) and it iterates over the array at least twice (one loop in the suggestion). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If I had a nickle for each time I thought I had written the best code and then David has a better solution -- I'd be so rich |
||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
function p_direction(chains::Chains; kwargs...) | ||||||||||||||||||||||||||||
# Store everything. | ||||||||||||||||||||||||||||
funs = [p_direction] | ||||||||||||||||||||||||||||
func_names = [:p_direction] | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Summarize. | ||||||||||||||||||||||||||||
summary_df = summarize( | ||||||||||||||||||||||||||||
chains, funs...; | ||||||||||||||||||||||||||||
func_names=func_names, | ||||||||||||||||||||||||||||
Comment on lines
+6
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid alloctions:
Suggested change
|
||||||||||||||||||||||||||||
name="Probability of Direction (pd)", | ||||||||||||||||||||||||||||
kwargs... | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return summary_df | ||||||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be problematic that this is only defined for
Vector
- that means e.g. that it won't work for slices of rows or columns.