Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: jnesselr on September 26, 2010, 09:55:46 pm

Title: Weighted average in Axe
Post by: jnesselr on September 26, 2010, 09:55:46 pm
Okay, so say I have 7 values that add up to 255. A, B, C, D, E, F, and G.  So, (A+B+C+D+E+F+G)=255.  How, in Axe, can I choose one of them, when their values are weighted toward the probability of choosing that one. Eg, if one of them is 0, then it has no chance of being picked, and if one is 255, then it is guaranteed to get picked.  Thats how they are weighted. So, is that understandable? I basically want this to be simple, in the axe programming language, and return the number from 0-7 of which number it should pick.

The easiest I thought would be to assume that they are all lined up, then find a random number between 0 and 255, and whichever "sector" it landed in, that was the attack.  The problem is, that I'm not quite sure how to do this, and not sure how good this would be.

So, please give advice.
Title: Re: Weighted average in Axe
Post by: Happybobjr on September 26, 2010, 10:04:24 pm
!!! these internal server 500 errors are bugging me!.

well to the point.
try this. (based off your idea)

.axample
:
:(set the values of A-G)
:A+B->H+C->I+D->J+E->K+f->L+G->M
:Rand^256->N
:If N(less than)H:then: (code for being in A)
:If N(greater than or equal to)H and N(less than)I: then: (code for B)
:If N(greater than or equal to)I and N(less than)J: then: (code for C)
:If N(greater than or equal to)j and N(less than)K: then: (code for D)
:If N(greater than or equal to)K and N(less than)L: then: (code for E)
:If N(greater than or equal to)L and N(less than)M: then: (code for F)
:If N(greater than or equal to)G:then:(code for B)

^ mine is poorly written and won't work.
Title: Re: Weighted average in Axe
Post by: Quigibo on September 26, 2010, 10:05:06 pm
I was actually just doing this recently for a Markov Chain in a research project.  You can do this:

1: Sum up all the numbers
2: Choose a random number between 1 and the sum
3: Set an accumulate variable to zero
4: Scan the list from left to right adding each value to the accumulator
5: Once the accumulator is equal to or passes the random number, choose that element.

EDIT: Wow, I was ninja'd
Title: Re: Weighted average in Axe
Post by: jnesselr on September 26, 2010, 10:11:31 pm
Yeah, except happybobjr's doesn't work well, because it uses so many other variables.
As for yours, I don't fully understand, quigibo. I don't really want a list. Can you give some code?
Title: Re: Weighted average in Axe
Post by: meishe91 on September 26, 2010, 10:34:42 pm
Well I don't know how to explain what Quigibo is talking about in Axe, but in TI-BASIC I think it would look something like this:

Code: [Select]
randInt(0,255,7→L1
SortA(L1
sum(Ans
DelVar CrandInt(1,Ans→B
For(D,1,dim(L1
C+L1(D→C
If C≥B
Then
Disp L1(D
Stop
End
End

I could be wrong though, but I think that's what he means.
Title: Re: Weighted average in Axe
Post by: Builderboy on September 27, 2010, 01:07:16 am
This is a very tricky problem, but Quigibo's solution is very elegant ^^ I like it :)
Title: Re: Weighted average in Axe
Post by: AngelFish on September 27, 2010, 02:07:55 pm
I've actually needed weighted random numbers in BASIC a few times and I found the following algorithm useful. I generally used it to change variable domains, but that's just a special case of the method.

1) Divide your input interval [A, B] into a finite number of subintervals [A_0, A_1], [A_2, A_3],...[A_n, B]
2) For each interval, assign a desired output probability and a total output range.
3) For each subinterval, create a linear function f(x) that maps the interval to the total output range multiplied by the probability of occurrence.
4) Offset each successive linear function to the endpoint of the function immediately preceding it.
5) Run the generator.

This method tends to be rather fast at generating weighted numbers and can be implemented fairly easily in most languages. However, it has a few problems, namely that the algorithm often produces numbers that are inconsistently less precise than those put in. You can get around this by truncating the output to a fixed number of decimals. Also, this method is an approximation of a true distribution, so it doesn't perfectly weight the numbers. It also fails to change the probability that any of the mapped intervals will occur. For example, the interval [F(A_0), F(A_1)] will occur precisely as often as [F(A_2),[F(A_3)]. However, normally you don't care about the intervals, only the numbers themselves, so this shouldn't be much of a problem.

This is all assuming you can even get Axe to support decimal arithmetic, of course.
 :)