All Questions
Tagged with probability c++
130
questions
27
votes
1
answer
1k
views
Why eigenvector & eigenvalue in LDA become zero?
I'd like to implement fast PLDA (Probabilistic Linear Discriminant Analysis) in OpenCV. in this, LINK fast PLDA have been implemented in Matlab and Python. One of the parts of PLDA is LDA. I've ...
24
votes
3
answers
28k
views
How to properly hash the custom struct?
In the C++ language there is the default hash-function template std::hash<T> for the most simple types, like std::string, int, etc. I suppose, that these functions have a good entropy and the ...
16
votes
4
answers
2k
views
Seeking suggestions for data representation of a probability distribution
I'm looking for an elegant and efficient way to represent and store an arbitrary probability distribution constructed by explicit sampling.
The distribution is expected to have the following ...
15
votes
3
answers
8k
views
Fast weighted random selection from very large set of values
I'm currently working on a problem that requires the random selection of an element from a set. Each of the elements has a weight(selection probability) associated with it.
My problem is that for ...
14
votes
2
answers
19k
views
Implementation of sequential monte carlo method (particle filters)
I'm interested in the simple algorithm for particles filter given here: http://www.aiqus.com/upfiles/PFAlgo.png It seems very simple but I have no idea on how to do it practically.
Any idea on how to ...
13
votes
2
answers
5k
views
Get true or false with a given probability
I'm trying to write a function in c++ that will return true or false based on a probability given. So, for example if the probability given was 0.634 then, 63.4% of the time the function would return ...
13
votes
2
answers
2k
views
Fisher Yates variation
The classic Fisher Yates looks something like this:
void shuffle1(std::vector<int>& vec)
{
int n = vec.size();
for (int i = n - 1; i > 0; --i)
{
std::swap(vec[i], vec[...
10
votes
4
answers
55k
views
Random numbers with different probabilities [duplicate]
I need to randomly determine a yes or no outcome (kind of a coin flip) based on a probability that I can define (.25, .50, .75).
So for example, I want to randomly determine yes or no where yes has a ...
10
votes
4
answers
31k
views
Using the gaussian probability density function in C++
First, is this the correct C++ representation of the pdf gaussian function ?
float pdf_gaussian = ( 1 / ( s * sqrt(2*M_PI) ) ) * exp( -0.5 * pow( (x-m)/s, 2.0 ) );
Second, does it make sense of we ...
9
votes
3
answers
5k
views
Random numbers based on a probability
I'm having trouble generating random numbers that do not follow a discrete uniform distribution.
So for example, say I have 5 numbers (to keep it simple), a probability of number k being generated ...
9
votes
2
answers
6k
views
C++ function for picking from a list where each element has a distinct probability
I have an array of structs and one of the fields in the struct is a float. I want to pick one of the structs where the probability of picking it is relative to the value of the float. ie
struct s{
...
7
votes
2
answers
1k
views
Computing the approximate population of a bloom filter
Given a bloom filter of size N-bits and K hash functions, of which M-bits (where M <= N) of the filter are set.
Is it possible to approximate the number of elements inserted into the bloom filter?
...
7
votes
5
answers
2k
views
Efficiently summing log quantities
Working in C++, I'd like to find the sum of some quantities, and then take the log of the sum:
log(a_1 + a_2 + a_3 + ... + a_n)
However, I do not have the quantities themselves, I only have their ...
6
votes
3
answers
2k
views
What exactly happens if you delete an object? (gcc) (When double-delete crashes?)
Please note that I don't want to solve any problem with my question - I was thinking about probabilities of things to happen and thus was wondering about something:
What exactly happens if you delete ...
6
votes
1
answer
2k
views
Proper boolean random generator (Bernoulli distribution)
I'd be curious to know if there is a default random boolean generator in the random C++11 library. I've been using a int generator returning 0 or 1 and then converting to bool but I'm trying to ...
6
votes
3
answers
1k
views
Probability density function from a paper, implemented using C++, not working as intended
So i'm implementing a heuristic algorithm, and i've come across this function.
I have an array of 1 to n (0 to n-1 on C, w/e). I want to choose a number of elements i'll copy to another array. Given ...
6
votes
2
answers
2k
views
C++ - critical values probability distribution
were can I find reliable code for critical values for probability distributions? For instance, F critical values for the fisher test...
?
Thanks for any relevant reference.
6
votes
1
answer
298
views
C++11 modify values in std::discrete_distribution
Is it possible to modify single value in std::discrete_distribution? I can't find a simple way of doing this. I was thinking of initializing it with std::vector with assigned probabilities and ...
6
votes
2
answers
1k
views
c++ discrete distribution sampling with frequently changing probabilities
Problem: I need to sample from a discrete distribution constructed of certain weights e.g. {w1,w2,w3,..}, and thus probability distribution {p1,p2,p3,...}, where pi=wi/(w1+w2+...).
some of wi's ...
6
votes
1
answer
3k
views
Inference in a Bayesian Network
I need to perform some inferences on a Bayesian network, such as the example I have created below.
I was looking at doing something like something like this to solve an inference such as P(F| A = ...
5
votes
4
answers
4k
views
How to select a value from a list with non-uniform probabilities?
I am looking at the k-means++ initialization algorithm. The following two steps of the algorithm give rise to non-uniform probabilities:
For each data point x, compute D(x), the distance between x ...
5
votes
5
answers
506
views
Will this give me proper random numbers based on these probabilities? C++
Code:
int random = (rand() % 7 + 1)
if (random == 1) { } // num 1
else if (random == 2) { } // num 2
else if (random == 3 || random == 4) { } // num 3
else if (random == 5 || random == 6) { } // num ...
5
votes
5
answers
8k
views
generating poisson variables in c++
I implemented this function to generate a poisson random variable
typedef long unsigned int luint;
luint poisson(luint lambda) {
double L = exp(-double(lambda));
luint k = 0;
double p = 1;...
5
votes
1
answer
187
views
Enter an If Statement Using Probabilities
I have the function mutateSequence that takes in three parameters. The parameter p is a value between 0 and 1, inclusive. I need two if statements, one that is entered with probability 4p/5 and ...
5
votes
1
answer
2k
views
How to compute CDF probability of normal distribution in C++?
Is there any function that allow me to compute the CDF probability of a normal distribution, given a mean and sigma ? i.e. for example P( X < x ) given the normal distribution with $\bar{x}$ and $\...
5
votes
4
answers
482
views
How much will this accumulate floating point errors?
I have a random process that, when called, returns a random number between 0 and K-1, where K may be decently high. I want to keep track of the number of times any outcome occurs, and normalize all ...
5
votes
6
answers
14k
views
Program with probability
In the event where we need to generate probability, for example a bias coin with 75% of tossing head and 25% tossing tail. Conventionally, I will do it this way:
#include <cstdlib>
#include <...
4
votes
2
answers
2k
views
Calculating probability for FUNPROB
Regarding -
FUNPROB
The solution is :
int N, M;
while(1) {
scanf("%d %d", &N, &M);
if (0 == N && 0 == M) break;
if (N > M) printf("0.000000\n");
else {
...
4
votes
3
answers
5k
views
How can I generate random samples from bivariate normal and student T distibutions in C++?
what is the best approach to generate random samples from bivariate normal and student T distributions? In both cases sigma is one, mean 0 - so the only parameter I am really interested in is ...
4
votes
2
answers
1k
views
Prev_permutation vs Next_permutation difficulty
I was trying out a sample program to grasp the difference between prev and next permutation. However, my program doesn't seem to work properly. I start the program by asking the number of elements in ...
4
votes
5
answers
5k
views
Sampling from a discrete probability distribution in C++
I am new to C++ and extremely surprised by the lack of accessible, common probability manipulation tools (i.e. the lack of things in Boost and the standard library). I've done a lot of scientific ...
4
votes
2
answers
3k
views
normalizing a list of very small double numbers (likelihoods)
I am writing an algorithm where, given a model, I compute likelihoods for a list of datasets and then need to normalize (to probability) each one of the likelihood. So something like [0.00043, 0.00004,...
4
votes
3
answers
254
views
Ambiguous Expectation value
I saw this problem on website (I wont be using the exact wording or mention the website),
Suppose a kid gets his pocket money on the 15th of every month,
according to which day is it on that date,...
4
votes
1
answer
851
views
What fraction of the population is married?
The problem below is one I came across on this wiki page.
Write a program to discover the answer to this puzzle:"Let's say men
and women are paid equally (from the same uniform distribution). If
...
3
votes
3
answers
212
views
C++11 Random numbers in a range and explicit likeliness
I would like to know I can generate random numbers(C++11) in a given range and have control over the likeliness of some number(s) to come up. Say I generate random numbers between 0 and 4 with number "...
3
votes
3
answers
920
views
How to generate a set of values that follow certain distribution in c++/java?
For example, I have a p.d.f f(x) = 1/25 - x/1250 (from x = 0 to 50);
My question is that how to generate a set of values that satisfy the given p.d.f.
Please give an idea about how to implement in c++ ...
3
votes
3
answers
2k
views
what are the largest and smallest numbers between 0 and 1 that C++ can represent internally without rounding?
I have a C++ function which computes probabilities based on a simple model. It seems that C++ tends to round very small probabilities to 0 and very large probabilities to 1. This results in issues in ...
3
votes
1
answer
245
views
Binomial Distribution Compression
I am currently have difficulty coming up with a fast and low memory solution for a problem. I am attempting to solve using the binomial distribution. I have a binomial distribution that can take on 5 ...
3
votes
1
answer
142
views
C++ Hash Restriction
As described in cppreference.com
The probability of h(a)==h(b) for a!=b should approach
1.0/std::numeric_limits<std::size_t>::max().
I want to create a hash table of pairs (a, b), where (a,...
2
votes
11
answers
4k
views
Representing probability in C++
I'm trying to represent a simple set of 3 probabilities in C++. For example:
a = 0.1
b = 0.2
c = 0.7
(As far as I know probabilities must add up to 1)
My problem is that when I try to represent ...
2
votes
11
answers
5k
views
Best way to calculate if there is a 1/4 chance something will happen in C++?
I was wondering if there is a smart way to find out
There is a 1/4 chance something happens.
I know we can do this with rand() % 4 and checking if it is equal to 0, but is there a way without using ...
2
votes
4
answers
653
views
Generate "characters" based on a probability
I need a little help with my tetris game
that I am coding in C++ , here is my problem:
The list of tetris blocks are 7 types:
{'I', 'J', 'L', 'S', 'Z', 'O', 'T'}
and i need to pick ONE of the above ...
2
votes
4
answers
4k
views
Random number generation with C++ or Python
I heard that computation results can be very sensitive to choice of random number generator.
1 I wonder whether it is relevant to program own Mersenne-Twister or other pseudo-random routines to get ...
2
votes
3
answers
3k
views
Random Number Generator with Modulo
I tried a small experiment with C++ random number generator code. I will post the code for everyone to see.
unsigned int array[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned int rand_seed = 4567;
int ...
2
votes
3
answers
2k
views
How to convert a double value into P.Q^-1 modulo MOD directly, where q is coprime with MOD
I am dealing with a probability question where the probability can be expressed as a fraction P/Q, where P and Q are integers (P≥0, Q>0) and Q is co-prime with 998,244,353. You should compute P⋅Q^−1 ...
2
votes
1
answer
517
views
How to find probability of drawing ball from a given container?
A container having 2 balls, one is red and second one is black.
One ball is drawn each time and placed again in the container.Drawing of ball is done ntimes where 1<=n<=10^6.I want to find out ...
2
votes
1
answer
2k
views
mixture of gaussian distribution in C++
I know library like gsl to generate gaussian distributions and generate random number based on gaussian distribution.https://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html
I ...
2
votes
1
answer
873
views
Selecting nodes with probability proportional to trust
Does anyone know of an algorithm or data structure relating to selecting items, with a probability of them being selected proportional to some attached value? In other words: http://en.wikipedia.org/...
2
votes
1
answer
425
views
Probability for each vertex
I have a graph with N vertices and M edges (N is between 1 and 15 and M is between 1 and N^2). The graph is directed and weighted (with a probability for that excact edge). You are given a start ...
2
votes
4
answers
865
views
C++ Random Number Check for Very Small Probabilities
I have a very small probability of an event occurring (of order 1e-5) and am trying to use a uniform random number to test for success. As the probability drops to around 1e-4 the fraction of ...