how to find probabity of A given B and C happens
how to find probability of A given B and C happens What is the probability of A given B and C P(AB,C)?
See also questions close to this topic

All Combinations of Items in Boxes
There are lots of questions and answers for combination algorithms but I can't find one on the problem that I'm trying to solve.
If I have a set of items and a set of boxes, how do I find all the combinations of items in boxes? All the items must be used, order doesn't matter and all boxes are treated the same with no size limit. I can sit down with a pen and paper to draw out all the combinations but I can't work out what algorithm my brain is using.std::vector<char> unsorted = { 'a', 'b', 'c', 'd' }; int box_count = 3; std::vector<std::vector<std::vector<char>>> sorted = {}; sorted = FillBoxes(unsorted, box_count); output << sorted = { { {a,b,c,d}, {}, {}}, { {a,b,c}, {d}, {} }, { {a,b,d}, {c}, {} }, { {a,b}, {c,d}, {} }, { {a,b,d}, {b}, {} }, { {a,c}, {b,d}, {} }, { {a,d}, {b,c}, {} }, { {a}, {b,c,d}, {} }, { {a,b}, {c}, {d} }, { {a,c}, {b}, {d} }, { {a,d}, {b}, {c} }, { {a}, {b,c}, {d} }, { {a}, {b,d}, {c} }, { {a}, {c,d}, {b} } }
I have had a few ideas involving binary trees and iterative pointers but none of them have worked.
It looks like a solvable problem but if it is mathematically impossible, I'd appreciate the feedback too.
Thanks!
(prefered language c++ but I can read most common programming languages) 
Weird sign issue with mapping from cartesian to bispherical coordinates
I want to map cartesian coordinates (x,y,z) to bispherical coordinates (sigma,tau,phi) and back accourding to wikipedia.
Here are my functions:
public static Vector3 ProjectBisphericalToCartesian(Vector3 bispherical, double a) { var sigma = (double)bispherical.X; var tau = (double)bispherical.Y; var phi = (double)bispherical.Z; var d = Math.Cosh(tau)  Math.Cos(sigma); var s = a / d; var x = s * Math.Sin(sigma) * Math.Cos(phi); var y = s * Math.Sin(sigma) * Math.Sin(phi); var z = s * Math.Sinh(tau); return new Vector3( (float)x, (float)y, (float)z ); } public static Vector3 ProjectCartesianToBispherical(Vector3 cartesian, double a) { var x = (double)cartesian.X; var y = (double)cartesian.Y; var z = (double)cartesian.Z; var R = Math.Sqrt(x * x + y * y + z * z); var s = R * R + a * a; var t = 2.0 * a * z; var Q = Math.Sqrt(s * s  t * t); var sigma = Math.Acos((R * R  a * a) / Q); var tau = Asinh(t / Q); var phi = Math.Atan(y / x); return new Vector3( (float)sigma, (float)tau, (float)phi ); } // sinh^1 ("areasinus hyperbolicus") private static double Asinh(double x) { return Math.Log(x + Math.Sqrt(x * x + 1.0)); }
I test the function by generating random numbers for (x,y,z) and a, convert them to bispherical coordinates and back to cartesian and check whether or not they are equal to the original cartesian corrdinates (up to some small tolerance).
It turns out that some cartesian coordinates flip the sign of its x and y component (z is okay) while other's do not. For example {X:5,3434 Y:2,569566 Z:1,195607} flips the sign of the x and y component while {X:7,586471 Y:6,154929 Z:1,494778} works fine.
I can not find an error in my code unfortunately. The issue appears to be caused by negative x values in the original cartesian coordinates which will cause the sign of x and y to be flipped after conversion and back. I just don't see why that happens.
Does anybody have an idea what is going on here ?

How to randomly choose coefficients of a linear combination in Python?
I have a linear combination of some functions as
I want to randomly select the coefficients
c_i
with the constraintAs a minimal example in Python:
import numpy as np x = 0.1 func_list = [np.sin(x), np.cos(x), x**2] def linear_comb(a, b, c): return a*func_list[0] + b*func_list[1] + c*func_list[2]
How should I sample
a
,b
andc
from uniform distributions with the constrainta^2+b^2+c^2=1
? 
Where are these negatives coming from in Maple execution?
I am interested in simulating the phenomenon of "regression to the mean". Say a 01 vector V of length N is "gifted" if the number of 1s in V is greater than N/2 + 5*sqrt(N).
I want Maple to evaluate a string of M 01 lists, each of length N, to determine whether they are gifted. Then, given that list V[i] is gifted, I want to evaluate the probability that list V[i+1] is gifted.
So far my code is failing in a strange way. So far all the code is supposed to do is create the list of sums (called 'total') and the list 'g' which carries a 0 if total[i] <= N/2 + 5sqrt(N), and a 1 otherwise.
Here is the code:
RS:=proc(N) local ra,i: ra:=rand(0..1): [seq(ra(),i=1..N)]: end: Gift:=proc(N,M) local total, i, g : total:=[seq(add(RS(N)),i=1..M)]: g:=[seq(0,i=1..M)]: for i from 1 to M do if total[i] > (N/2 + 5*(N^(1/2))) then g[i]:=1 fi: od: print(total, g) end:
The trouble is, Maple responds, when I try Gift(100,20), "Error, (in Gift) cannot determine if this expression is true or false: 5*100^(1/2) < 2" or, when I try Gift(10000,20), "Error, (in Gift) cannot determine if this expression is true or false: 5*10000^(1/2) < 103."
Where are these negative numbers coming from? And why can't Maple tell whether 5(10000)^{1/2} < 103 or not?

Flow duration curve using facet_wrap of ggplot in R?
I am using
fdc
ofhydroTSM package
. I have threedata.frame
and i would like to construct Flow duration curves (FDC) of thedata.frame
usingfacet_wrap
functionality ofggplot
to have theplots
inthree rows
andone column
. the following will produceFDC curves
forDF1
.library(tidyverse) library(hydroTSM) library(gridExtra) DF1 = data.frame(Ob = runif(1000,0,500), A = runif(1000,0,700), B = runif(1000,2,800)) DF2 = data.frame(Ob = runif(1000,0,500), A = runif(1000,0,700), B = runif(1000,2,800)) DF3 = data.frame(Ob = runif(1000,0,500), A = runif(1000,0,700), B = runif(1000,2,800)) fdc(DF1, plot = TRUE)
I tried to use
gridExtra package
withgrid.arrange
to force the three plots on a single figure. I not only failed to do it but it is not the preferred method. I would want to used thefacet_wrap
options of theggplot
. In fact the figure is wrongly drawn by usingDF1
data. i am looking for something like below:Update: This is based on @Jon Spring suggestions.
graphics.off() rm(list = ls()) library(tidyverse) library(hydroTSM) library(gridExtra) DF1 = data.frame(Ob = runif(800,0,500), M1= runif(800,0,700), M2 = runif(800,2,800), df = rep("Upstream", 800)) DF2 = data.frame(Ob = runif(1000,0,500), M1 = runif(1000,0,700), M2 = runif(1000,2,800), df = rep("Midstream", 1000)) DF3 = data.frame(Ob = runif(1000,0,500), M1 = runif(1000,0,700), M2 = runif(1000,2,800), df = rep("Downstream", 1000)) # combine data into one table with id column for the source bind_rows(DF1, DF2, DF3) %>% # reshape into longer format pivot_longer(df, names_to = "src", values_to = "flow") %>% arrange(flow) %>% group_by(df, src) %>% mutate(flow_pct = 1  percent_rank(flow)) %>% ungroup() %>% ggplot(aes(flow_pct, flow, color = src)) + geom_line() + theme_light() + facet_wrap(~df, ncol = 1) + labs(x = "% Time flow equalled or exceeded", y = "Q, [m3/s]") + theme(strip.text = element_text(hjust = 0, color = "black"), strip.background = element_blank())

Computing and generating plot for the Probability of Exceedance in R?
I am trying to compute
probability of exceedance
in R. Here is the link that has detail and formula for computing exceedance probability. I tried to replicate the procedure with the following sample code, however, I am no way near the attached example plotting. Once, i figured out how to do this i would then like to apply the procedure on adata.frame
that has multiplevariables
.library(tidyverse) A = sample(0:5000, 2500) A = A[order(A, decreasing = TRUE)] Rank = 1:2500 DF = data.frame(cbind(A,Rank)) DF$Prob = 100*(DF$Rank/(length(DF$Prob+1))) ggplot(data = DF, aes(x=Prob, y=A))+ geom_line() + scale_y_continuous(trans = "log10")