mardi 23 janvier 2024

Quantiles of sums of independent discrete random variables

I have the following problem: given k (=10) discrete independent random variables X_i with n_i ( = 5 to 20) values each.

Problem: compute quantiles of the distribution of the sum X = X_1+...+X_k.

Here X has n=n_1 x n_2 ... n_k distinct values which is too large to list them all together with their probabilities.

I tried several approaches:

(A) Convolution:

each X_j is approximated with Y_j=X_j+Z, where Z is a normal N(0,sigma) variable with small sigma. Then Y_j is a probability mixture of the normal variables N(x_j,sigma), where the x_j runs over all values of X_j, and has a highly oscillatory density.

The density of Y=\sum Y_j is the convolution of the densities of the Y_j.

I need this density at a sizeable number of points and this turns out to be too slow. The issue seems to be the convergence of the convolution integrals slowed down by the oscillatory nature of the densities of the Y_j. When the densities are behaved better (e.g. normal RVs), the computation of such a convolution is quite fast.

(B) Characteristic function:

X will be approximated with Y=X+Z, where Z is normal N(0,\sigma) with small sigma. Y has a density (which it is impossible to compute directly) but the characteristic function (continuous Fourier transform) cf_Y of Y can easily be computed analytically (without knowing the density of Y)

Now let s be a numeric vector. I want to get the density f_Y(s) of Y evaluated along s. The proper way of doing this would be to apply the inverse continuous Fourier transform to the function cf_Y at each point in s.

This is far too slow. That's why I tried to apply the inverse discrete Fourier transform to the vector of values cf_Y(s) and that does not yield anything reasonable.

This baffles me since I was under the impression that the discrete Fourier transform is an approximation to the continuous Fourier transform and so should yield the values of the latter, if the value grid s is fine enough.

Should this work?

Note that this inversion would definitely work if I could compute the discrete Fourier transform of the density f_Y along s, but regrettably this is not possible since

(a) the density of f_Y is far too complicated, and

(b) the discrete Fourier transform of a sum Y = Y_1+Y_2+...+Y_k of independent random variables Y_j is not the product of the discrete Fourier transforms of the Y_j.

Any ideas how I could approach this problem with the tools of R?




mercredi 17 janvier 2024

Random Choice in If statement [closed]

I am a beginner and im wondering how to code something like this in python:

If this word from a list of given words is randomly picked print ("")

I am trying to code a little game but i cant code any further without this piece of code.




samedi 13 janvier 2024

Simple Scaling Probability With Outliers in Python

I'm creating a virtual auction game, and I need more complex NPC behavior than I have now. Basically, every item has a base price (bp), and the starting bid (sb) for that item will be calculated with this range (or something very similar): 0.8bp <= sb <= 1.1bp. The opposing NPC bidders need to judge how reasonable the current bid is, compared to the base price and/or starting value, and, with some outlying behavior (i.e. betting when it seems a little irrational), bid reasonably on the current item. Put simply, it should bid aggressively on the item when the bid is lower or around the base price, and frugally bid on the item if it's high above the base price, or just give up.

The issue is, I really have no idea how to do this. Not only that, this is going to mainly be run on a TI-84 Plus CE calculator, so the external libraries and modules have to be pretty limited (essentially just random). It'd be great if you attach a brief (or long) explanation of any formulas/code you provide, because I'd love to learn more about this.




What is the modulus of the libc pRNG LCG?

I'm a bit confused at the moment about the linear congruential generator used in the rand() function in stdlib to generate random numbers. The table on https://en.wikipedia.org/wiki/Linear_congruential_generator lists the modulus used by GCC and for ANSI C as 2^31. However, according to https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf p347, for ANSI C 2^32 is implicitly used. Does rand() in stdlib also use 2^32?

I've implemented and tested the ANSI C implementation, which works fine.




reduce a list of objects given an associated list of likelyhoods

I have a list (aka, slice) of objects. I also have a list of the same size that contains the likelyhood that each object will be useful in the future. I need to reduce my object list length so that it has n or fewer elements. (It may or may not have n+ elements to start.) How can I do this efficiently?

I was thinking I could sort the object list according to the probabilities and then truncate it. However, I want the low-probability objects to have a chance of remaining. I was also thinking that I could check the probability on each item against a random number to see if it lives or dies, but then I might end up with fewer than n items in the end.




vendredi 12 janvier 2024

Why are all the strings the same

For test my hashmap on C language I need to generate many keys (string). I wrote this code for generate keys. And this code doesn't works.

    char* keys [15] = { 0 };

    for (int i = 0; i < 15; i++)
    {
        char key [7] = { 0 };
        for(int j = 0; j < 5; ++j)
        {
            int rand_char = rand()%26+'A';
            key[j] = (char)rand_char;
        }
        *(keys + i) = key; 
    }

    for (int i = 0; i < 15; i++)
        printf("%s\n", keys[i]); 

The problem is all keys are the same and I don't know why this happens. OUPUT:

MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ

I tried putting the random result into a variable (int rand_char) and also tried the debug keys during the generation process and saw that all the letters change when new letters are generated, as if all the lines were one string.

char* keys [15] = { 0 };

for (int i = 0; i < 15; i++)
{
    char key [7] = { 0 };
    for(int j = 0; j < 5; ++j)
    {
        int rand_char = rand()%26+'A';
        key[j] = (char)rand_char;
        for (int k = 0; k < i; k++)
            printf("%s\n", keys[k]); 
    }
    *(keys + i) = key; 
}

OUPUT:

B
BM
BMQ
BMQB
BMQBH
C
C
CD
CD
CDA
CDA
CDAR
CDAR
CDARZ
CDARZ
O
O
O
OW
OW
OW
OWK
OWK
OWK
OWKK
OWKK
OWKK
OWKKY
OWKKY
OWKKY
H
H
H
H
HI
HI
HI
HI
HID
HID
HID
HID
HIDD
HIDD
HIDD
HIDD
HIDDQ
HIDDQ
HIDDQ
HIDDQ
S
S
S
S
S
SC
SC
SC
SC
SC
SCD
SCD
SCD
SCD
SCD
SCDX
SCDX
SCDX
SCDX
SCDX
SCDXR
SCDXR
SCDXR
SCDXR
SCDXR
J
J
J
J
J
J
JM
JM
JM
JM
JM
JM
JMO
JMO
JMO
JMO
JMO
JMO
JMOW
JMOW
JMOW
JMOW
JMOW
JMOW
JMOWF
JMOWF
JMOWF
JMOWF
JMOWF
JMOWF
R
R
R
R
R
R
R
RX
RX
RX
RX
RX
RX
RX
RXS
RXS
RXS
RXS
RXS
RXS
RXS
RXSJ
RXSJ
RXSJ
RXSJ
RXSJ
RXSJ
RXSJ
RXSJY
RXSJY
RXSJY
RXSJY
RXSJY
RXSJY
RXSJY
B
B
B
B
B
B
B
B
BL
BL
BL
BL
BL
BL
BL
BL
BLD
BLD
BLD
BLD
BLD
BLD
BLD
BLD
BLDB
BLDB
BLDB
BLDB
BLDB
BLDB
BLDB
BLDB
BLDBE
BLDBE
BLDBE
BLDBE
BLDBE
BLDBE
BLDBE
BLDBE
F
F
F
F
F
F
F
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ELLN
RELLN
RELLN
M
M
M
M
M
M
M
M
M
M
M
M
M
M
MP
MP
MP
MP
MP
MP
MP
MP
MP
MP
MP
MP
MP
MP
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPA
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAP
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ



jeudi 11 janvier 2024

How do I get a random choice from a list to not give me duplicates?

Sorry if it's unclear, beginner here. Basically I have this to pick random questions for a quiz:

import random

econ = ["eq1", "eq2", "eq3", "eq4", "eq5", "eq6", "eq7", "eq8"]
mil = ["mq1", "mq2", "mq3", "mq4", "mq5", "mq6", "mq7", "mq8"]
oro = ["oq1", "oq2", "oq3", "oq4", "oq5", "oq6", "oq7", "oq8"]
pol = ["pq1", "pq2", "pq3", "pq4", "pq5", "pq6", "pq7", "pq8"]

cats = [econ,mil, oro, pol]
 
for x in cats:
        for y in range(3):
            print(random.choice(x))

However, I sometimes get duplicates, example: eq3 <- here eq3 <- here eq1 mq5 mq7 mq2 oq1 oq4 oq5 pq3 pq7 pq2

I've scoured through forums and the python coding book but was unable to find something that didn't confuse me more!

TL;DR: How do I avoid repetition of random items in the code?