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

[Feature] Probability of Direction (pd) #428

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/MCMCChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export rafterydiag
export rstar

export hpd
export p_direction

"""
Chains
Expand Down Expand Up @@ -85,5 +86,6 @@ include("modelstats.jl")
include("plot.jl")
include("tables.jl")
include("rstar.jl")
include("significance.jl")

end # module
19 changes: 19 additions & 0 deletions src/significance.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function p_direction(x::Vector{Float64})
Copy link
Member

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.

return maximum([sum(x .> 0) ./ length(x), sum(x .< 0) ./ length(x)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this creates a few unnecessary allocations (the array inside of maximum, x .> 0, and x .< 0). This could be fixed e.g. by

Suggested change
return maximum([sum(x .> 0) ./ length(x), sum(x .< 0) ./ length(x)])
ntotal = 0
npositive = 0
nnegative = 0
for xi in x
if xi > 0
npositive += 1
elseif xi < 0
nnegative += 1
end
ntotal += 1
end
return max(npositive, nnegative) / ntotal

Copy link
Author

Choose a reason for hiding this comment

The 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 @btime and indeed... 🤯

Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Member

Choose a reason for hiding this comment

The 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 @btime and indeed... 🤯

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid alloctions:

Suggested change
# Store everything.
funs = [p_direction]
func_names = [:p_direction]
# Summarize.
summary_df = summarize(
chains, funs...;
func_names=func_names,
# Summarize.
summary_df = summarize(
chains, p_direction;
func_names=[:p_direction],

name="Probability of Direction (pd)",
kwargs...
)

return summary_df
end