All Questions

Tagged with
Filter by
Sorted by
Tagged with
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 ...
Saeid's user avatar
  • 548
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 ...
abyss.7's user avatar
  • 14.2k
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 ...
George Skoptsov's user avatar
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 ...
user avatar
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 ...
shn's user avatar
  • 5,206
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 ...
user2317084's user avatar
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[...
fredoverflow's user avatar
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 ...
Josh Brittain's user avatar
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 ...
shn's user avatar
  • 5,206
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 ...
DillPixel's user avatar
  • 955
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{ ...
Stuart's user avatar
  • 1,723
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? ...
Xander Tulip's user avatar
  • 1,468
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 ...
Mike B's user avatar
  • 175
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 ...
IanH's user avatar
  • 3,998
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 ...
Learning is a mess's user avatar
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 ...
hfingler's user avatar
  • 1,996
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.
kiriloff's user avatar
  • 25.9k
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 ...
pablo432's user avatar
  • 185
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 ...
therefore's user avatar
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 = ...
suphug22's user avatar
  • 181
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 ...
zebra's user avatar
  • 6,501
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 ...
DillPixel's user avatar
  • 955
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;...
zzzbbx's user avatar
  • 10.9k
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 ...
Daniel Lobo's user avatar
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 $\...
shn's user avatar
  • 5,206
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 ...
Svalorzen's user avatar
  • 5,443
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 <...
user3437460's user avatar
  • 17.3k
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 { ...
hack3r's user avatar
  • 573
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 ...
Grzenio's user avatar
  • 36.3k
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 ...
Josh's user avatar
  • 3,271
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 ...
ely's user avatar
  • 76k
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,...
Ikram Ullah's user avatar
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,...
Harshit Singhal's user avatar
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 ...
Quaxton Hale's user avatar
  • 2,500
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 "...
Medard Rb's user avatar
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++ ...
user499568's user avatar
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 ...
Aciel's user avatar
  • 95
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 ...
Nick's user avatar
  • 75
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,...
ABu's user avatar
  • 11.1k
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 ...
Dave's user avatar
  • 137
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 ...
user avatar
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 ...
Shehryar Farooq's user avatar
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 ...
kiriloff's user avatar
  • 25.9k
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 ...
Psypher's user avatar
  • 436
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 ...
itskarad's user avatar
  • 105
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 ...
Enigma's user avatar
  • 179
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 ...
printemp's user avatar
  • 869
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/...
Stephen Cross's user avatar
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 ...
Agnar's user avatar
  • 23
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 ...
zooombini's user avatar
  • 153