Author Topic: Weighted average in Axe  (Read 3115 times)

0 Members and 1 Guest are viewing this topic.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Weighted average in Axe
« 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.
« Last Edit: September 26, 2010, 09:55:54 pm by graphmastur »

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Weighted average in Axe
« Reply #1 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.
« Last Edit: September 27, 2010, 03:47:49 pm by happybobjr »
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Weighted average in Axe
« Reply #2 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
« Last Edit: September 26, 2010, 10:07:12 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Weighted average in Axe
« Reply #3 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?

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Weighted average in Axe
« Reply #4 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.
« Last Edit: September 26, 2010, 10:41:02 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Weighted average in Axe
« Reply #5 on: September 27, 2010, 01:07:16 am »
This is a very tricky problem, but Quigibo's solution is very elegant ^^ I like it :)

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Weighted average in Axe
« Reply #6 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.
 :)
« Last Edit: September 27, 2010, 02:10:08 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ