You are not signed in. Sign in, or Create an account.

BUGS FORUM: fun with math!

http://www.urlashare.com/forum/bugs/

Page 1 of 2 • Jump to page  1 2
#1 January 18, 2010 11:06 PM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
cb123 said:
Need to change formula (avg growth per transaction)
x=((((cash+portfolio)/10000)^(2/transactions))-1)*100


cb123 said:
I've been messing with that calculation for days now... You know what conclusion I've come to? The darn thing will simply never report the right number, no matter how I work it around... That's the bad news. The good news is that it's actually a much simpler problem than what I've been treating it as.

Granted the current calculation actually works, but it's reporting an overall % relative to the starting amount rather than a true average gain per trade, which isn't possible given the data we're trying to use..

No idea why this hadn't come to me earlier, sorry about being less than a stellar performer on this issue.

So the overall idea is this, we just need to add two variables for the value, which would be calculated on the sell transaction.

Essentially the pseudo-code (runs only on sell transaction) is this:
set user_trade_count_var = (count_var + 1)
set user_gain_total = (user_gain_total + this_gain_percent)
display user_avg_percent_gain = (user_gain_total / user_trade_count_var)


That will do it... And it does work, I've verified this to make sure I'm not wasting more of your time. Not too sure how this system is built, but I assume there is some ability to call this on the sell event, if not then it's of no use.

Cheers, and thanks!
-Cory


that data is calculated on the profile load, and not on the sell event.

ill see if i can find a way to fit it in, but wouldn't (total - 10000) / transactions at least give us an accurate average gain per transaction?


cb123 said:
Unfortunately no, it always favors a higher total. Because the calculation can tell you what the gain was over the entire run, but not per individual transactions.. That's what I had to figure out (and it took some thinking, talking, and more than a few sheets of paper SMILE ).. The idea is that you are looking at average gain balanced against the initial 10000, rather than the gain that is averaged from each transaction... Here's the simplified math--

So, if I have two transactions, starting with 10k -

1. gain 100% balance is now 20000

2. gain 100% balance is now 40000

----

Now we can see that the average gain is 100% here, however when we take [(total - 10000) / transactions] we get an average of 15000 gain per transaction, which averages to 150%, or 50% more than the real average, and this is an exponential increase, so it will increase non-geometrically (in other words, the more you make, the further from reality the calculation becomes...)

I tried to deal with this in my second attempt, turned out that even that method (while better) didn't address the overall conceptual issue, so after pounding my head against a wall for a while, I came to the conclusion that we just needed more data points and everything goes smooth..

****************8

Even if the data is calc'd at load (this is just fine I think..) can you (on a sell transaction) write to a persistent variable that will hold the needed data?

If so, then the calculation can just be done @ load, with the data.

Obviously, you are storing this data somewhere, because the ledger is already perfectly capable of showing the data from each transaction, we just need to act on that data rather than the data I was trying to shoehorn into this calculation..

So, like I said, this has spiraled into something more significant that I initially expected, so I can and will understand if this (a.) takes time, or (b.) never happens..


lol i think i smell toast.

im gonna read this over and over until i get it SMILE

lets think about it in a new way tho...

instead of thinking of it as happening 'at the sell', i should mention that i have records of every sell transaction, as well as its corresponding buy.

its all over my head tho, but its probably not impossible to get it working. THUMBSUP!

cb123 said:

Perfect. HAPPY DANCE!--

Let me think really carefully (I think this is now simple, which makes me think I'm missing something...) about this for a while, and I'll get back to you tomorrow with a working formula..

*if you want I can assist with the code, as you might have guessed, I'm pretty comfy with this stuff...


check your the values in the earnings column of your ledger view... they might be a key to all this.

ive got a hunch BIG GRIN

cb123 said:
just for reference:

earnings=total sale value - cash invested
cash=cash invested
(earnings / cash) * 100 [values from ledger on a sell transactions] gives percent per transaction set.

now-- we have two options, one is to store a persistent value that is the result of this calculation, then refer to in on the next calculation, or we can just run this against every transaction made, but that is inefficient and could cause significant demand on the server, and potential delays (depending on your server config and how well the code handles calculations)

So, if you choose to set a persistent value, then let's call it avgpercent
if not, then just run this for each buy/sell set, and think of avgpercent as a display field.

note that I've included a int() function to make this easier on the eye.
I've been overly cautious on the use of () just to make SURE this is sequenced in a functional way..


Formulas:
____________________________

Simple:
avgpercent=int((avgpercent + (((earnings/cash) *100))) / 2)


Loop:
For x=1 to totaltrans
avgpercent=((avgpercent + (((earnings/cash) *100))) /2 )
next x
avgpercent=int(avgpercent)

_______________

Once again, I really think the better option is the simple approach, you would need to set a value in the DB table, or somehow persist that number, but the advantages are simplicity of calculation and much lower overhead...


Hope this clarifies everything well enough, if not let me know. SMILE

-Cory



Phew::BONGHIT!HAPPY DANCE!

 
 
#2 January 19, 2010 6:27 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
Eh gads... That's a lot of screw up SMILE
 
 
#3 January 19, 2010 8:06 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
LOL nah, but it needed to be in one place for easier digestion THUMBSUP!
 
 
#4 January 25, 2010 4:30 PM
cb123
joined: May 2009
OFFLINE
Posts: 117
Bump .... Keeping you honest here crazy Nick SMILE

(add extra guilt here.. ) WINK WINK
 
 
#5 January 25, 2010 10:53 PM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
duly noted. ive gotta admit, this topic terrifies me.

not forgotten tho.
 
 
#6 January 25, 2010 11:01 PM
cb123
joined: May 2009
OFFLINE
Posts: 117
I think too much explanation is a bad thing maybe

avgpercent= avgpercent + ( (((totalvalue-cashinvested)/cashinvested) *100) / 2 )

need to keep the value of avgpercent, it must be stored somewhere...


Make more sense? SMILE Or at least look less threatening?
 
 
#7 January 25, 2010 11:03 PM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
yes, less threatening.

im still trying to find a way around storing the avgpercent value tho.

im convinced that it either exists already, or can be calculated.

i just need to seriously wrap my head around it. BONGHIT!
 
 
#8 January 26, 2010 6:19 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
Loop:
For x=1 to totaltrans
avgpercent=((avgpercent + (((earnings.x/cash.x) *100))) /2 )
next x
avgpercent=int(avgpercent)


No need to store the value here, the only downside is the need to loop the calculation

Make sense still? Especially what's going on in the loop?

x is the number of the buy/sell transaction set, totaltrans is the full number of buy/sell sets for the person. earnings.x and cash.x are the data for that transaction, where earnings are described by: sellamount-investmentamount.

Ok, if you need more let me know.. I'll do what I can.
 
 
#9 January 27, 2010 12:04 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
ok, im sold on a plan.

things i will need to do to make this happen:

a) create a new column in the users table for avgpct
-done. 30 seconds later. WINK WINK

b) write a script to parse the existing transactions for the season, and populate the column.
-i guess this is the 'hard' part, although it isnt.

c) modify the sell modules to perform the calculation, and update table on sell.
-already updating the table because of the transaction, easy.

d) modify the profile pages to use the new data.

e) pause transactions for like 30 seconds while i run it and put the new modules in place.

ok, now that its all layed out in nickspeak, it makes perfect sense. THUMBSUP!

shouldnt be long now SMILE
 
 
#10 January 27, 2010 1:29 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
waiting for review. please check the math: http://urlashare.com/fugly.php (must be logged in) THUMBSUP!

*edit
fugly.php said:

sold 83 of nexus-one.co.uk -bought at: 8 - sold at: 8.00 - profit: 0 GPT = 486.56%
sold 1 of mashable.com -bought at: 4410 - sold at: 4410.00 - profit: 0 GPT = 486.56%
sold 4 of mashable.com -bought at: 4410 - sold at: 4410.00 - profit: 0 GPT = 486.56%
sold 4 of techcrunch.com -bought at: 4870 - sold at: 4870.00 - profit: 0 GPT = 486.56%
sold 1 of mozilla.com -bought at: 11000 - sold at: 11000.00 - profit: 0 GPT = 486.56%
sold 1 of mozilla.com -bought at: 11000 - sold at: 11000.00 - profit: 0 GPT = 486.56%
sold 93 of benmarsh.co.uk -bought at: 30 - sold at: 30.00 - profit: 0 GPT = 486.56%
sold 293 of nexusone.com -bought at: 16 - sold at: 16.00 - profit: 0 GPT = 486.56%
sold 277 of benmarsh.co.uk -bought at: 30 - sold at: 200.00 - profit: 47090 GPT = 769.89%



that looks odd to me. shouldnt a transaction with no profit or loss still affect this?

here is the math im using:
the math said:

CASHINVESTED = shares * average cost per share;
TOTALVALUE = shares * selling price;
GPT = GPT + ((((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100)/2);



needs to factor in number of transactions, yeah?
 
 
#11 January 27, 2010 1:48 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
you know you're a nerd when you edit a forum post just because you noticed you missed a semicolon in your pseudocode... just sayin.
 
 
#12 January 27, 2010 7:07 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
here is the math im using:
the math said:

CASHINVESTED = shares * average cost per share;
TOTALVALUE = shares * selling price;
GPT = GPT + ((((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100)/2);



.

Cool... This is very close now. We just need to change how we are dividing to fix this. (That is how we are dealing with # of transactions SMILE )


CASHINVESTED = shares * average cost per share;
TOTALVALUE = shares * selling price;
GPT = ((GPT + (((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100)) /2);



That's it. Should work like a champ now.


Killer work Nick, thanks for all the hard work on this. (I so love metrics...)


TWO THUMBS UP!
 
 
#13 January 27, 2010 7:15 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
gotta postpone this til i get home from work (and maybe sleep)

headed out the door now THUMBSDOWN!
 
 
#14 January 27, 2010 10:23 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
Ha! You with your wonderful guilt...

If you get this done in the next week I'm thrilled. THUMBSUP!
 
 
#15 January 27, 2010 5:51 PM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
cb123 said:

GPT = ((GPT + (((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100)) /2);


ok, that math is in place now. please check the fugly.php and see how the calculations look.

if you sign off on it, i will plug that math into the modules, and we should be good to go THUMBSUP!
 
 
#16 January 27, 2010 7:27 PM
cb123
joined: May 2009
OFFLINE
Posts: 117
Ok, almost everything is good, but there is one minor issue still going on, the issue is that the first transaction cannot be divided by 2, because there is no need to do that (since it's the first)..

This effectively gives us a starting value that is 50% less than the correct value.

So, how to solve? Easy I think, just run the calculation the first time as

GPT = ((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100;

then run from #2 onward.. Make sense still? You know I had to have one final small problem here SMILE

But, the good news is that it seems to work flawlessly other than that, all the math seems to be correct.

-Cory

BTW: I've enjoyed debugging this quite a bit, it's been a while since I did much code work. TWO THUMBS UP!
 
 
#17 January 28, 2010 5:14 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
i plugged this in:

fugly.php said:

if(VARIABLE == 0){
GPT = ((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100;
VARIABLE = 1;
}else{
GPT = ((GPT + (((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100)) /2);
}



my first sell was for $0 gain, so my account isnt a good reference LOL
 
 
#18 January 28, 2010 6:23 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
NickSunshine said:
i plugged this in:

fugly.php said:

if(VARIABLE == 0){
GPT = ((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100;
VARIABLE = 1;
}else{
GPT = ((GPT + (((TOTALVALUE - CASHINVESTED) / CASHINVESTED)*100)) /2);
}


my first sell was for $0 gain, so my account isnt a good reference LOL


Hmm, I just tried fugly and it didn't seem to be rolling that first part out, seemed it just went about the business of the regular loop..

All of the code looks good, I know this works, it just seems the conditional isn't being cooperative, as there is no change in my first value.. ?? Odd!

Any ideas? (If you would like, I am willing to change my PW and give you access for a while to have an account who's first trade wasn't a net gain of null)..
 
 
#19 February 1, 2010 9:59 PM
cb123
joined: May 2009
OFFLINE
Posts: 117
Nick,

If you wanted to, you could plug what you have now into the module, it's certainly pretty close, especially after a few trades (and in your case, it's perfectly accurate!)..

That way you could just fix the little bug later, and it's certainly better than what's in place right now SMILE

Cheers!
 
 
#20 February 2, 2010 11:58 PM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
cb123 said:
Nick,

If you wanted to, you could plug what you have now into the module, it's certainly pretty close, especially after a few trades (and in your case, it's perfectly accurate!)..

That way you could just fix the little bug later, and it's certainly better than what's in place right now SMILE

Cheers!


LOL but but but....

we are so close!

just gimme a couple more days... things are crazy busy at work this week, but i havent lost sight of this THUMBSUP!
 
 
#21 February 3, 2010 7:32 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
Your dedication is impressive! TWO THUMBS UP! I'd hire a guy like you any day!
 
 
#22 February 5, 2010 2:52 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
ok, fixed the math on fugly for real this time... turns out i was uploading the file to the wrong folder. kinda embarrassing LOL. not enough sleep.

i will effort to move forward with this after i wake up, i dont work until late afternoon.

if not, probably sunday or monday. THUMBSUP!
 
 
#23 February 5, 2010 6:22 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
Now it looks like it's working well... Except-- Looks like it's not maintaining the total.

Do you have GPT=GPT + (.....)
Or do you have GPT= (......)

Because for the first calculation we do need GPT= (....)
On the second/third/fourth - etc, we need GPT=GPT + (.....)

Just a guess, it could be something else.. Here's the readout from fugly-

cb123 - sold 28 of downloadhelper.net -bought at: 346 - sold at: 1910.00 - profit: 43792 GPT = 452.02%
cb123 - sold 183 of thekingcenter.org -bought at: 8 - sold at: 46.00 - profit: 6954 GPT = 463.51%
cb123 - sold 183 of martinlutherking.org -bought at: 5.5 - sold at: 49.00 - profit: 7960.5 GPT = 627.21%
cb123 - sold 183 of mygeektime.com -bought at: 15 - sold at: 41.00 - profit: 4758 GPT = 400.27%
cb123 - sold 30 of gizmodo.com -bought at: 2140 - sold at: 7060.00 - profit: 147600 GPT = 315.09%
cb123 - sold 322 of moonphases.info -bought at: 12 - sold at: 27.00 - profit: 4830 GPT = 220.04%
cb123 - sold 114 of tonyblairoffice.org -bought at: 1.2 - sold at: 13.00 - profit: 1345.2 GPT = 601.69%
cb123 - sold 66 of timesonline.co.uk -bought at: 2870 - sold at: 3280.00 - profit: 27060 GPT = 307.99%
cb123 - sold 293 of justine-henin.be -bought at: 18 - sold at: 20.00 - profit: 586 GPT = 159.55%
cb123 - sold 461 of showbizgalore.com -bought at: 43 - sold at: 49.00 - profit: 2766 GPT = 86.75%
cb123 - GPT: 86.75%

Notice the last value is the final GPT ... Everything else is working great though.

Cheers!
 
 
#24 February 8, 2010 12:00 AM
NickSunshine
joined: March 2009
OFFLINE
Posts: 852
cb123 said:
Now it looks like it's working well... Except-- Looks like it's not maintaining the total.
Do you have GPT=GPT + (.....)
Or do you have GPT= (......)


yep, its gpt = gpt + after the first transaction.

calc.exe said:

***downloadhelper.net
cashinvested = 346 * 28 = 9688
totalvalue = 1910 * 28 = 53480

gpt = ((totalvalue - cashinvested) / cashinvested) * 100

gpt = ((53480 - 9688) / 9688) * 100

gpt = (43792 / 9688) * 100

gpt = 4.52023 * 100

gpt = 452.02

***thekingcenter.org
cashinvested= 8 * 183 = 1464
totalvalue = 46 * 183 = 8418

gpt = ((gpt + (((totalvalue - cashinvested) / cashinvested) * 100)) / 2)

gpt = ((452.02 + (((8418 - 1464) / 1464) * 100)) / 2)

gpt = ((452.02 + ((6954 / 1464) * 100)) / 2)

gpt = ((452.02 + (4.75 * 100)) / 2)

gpt = ((452.02 + 475.00) / 2)

gpt = (927.02 / 2)

gpt = 463.51

***martinlutherking.org
cashinvested= 5.5 * 183 = 1006.5
totalvalue = 49 * 183 = 8967

gpt = ((gpt + (((totalvalue - cashinvested) / cashinvested) * 100)) / 2)

gpt = ((463.51 + (((8967 - 1006.5) / 1006.5) * 100)) / 2)

gpt = ((463.51 + ((7960.5 / 1006.5) * 100)) / 2)

gpt = ((463.51 + (7.90909 * 100)) / 2)

gpt = ((463.51 + 790.90) / 2)

gpt = (1254.41 / 2)

gpt = 627.205



looks like its working correctly?

BUT-

i keep wondering why we arent dividing the first sell by two... wasnt that division to compensate for the purchase transaction? either way, its close.
 
 
#25 February 8, 2010 7:59 AM
cb123
joined: May 2009
OFFLINE
Posts: 117
Wow, that's working PERFECT!!!

Ok, so here's the long explanation for diving by 2 and why we don't do it on the first transaction...

basically that's what gives us an average- essentially - if we have three items like this ( 5 5 5 ) and we want to know the average, we will take 5+5+5 [=15]and then divide by 3 - this gives us 5, or the average of the three numbers... Now looking at it from a more real world standpoint, if we have the set ( 2 5 7 8 12 1 7 ) then we have seven numbers, so we'll take 2+5+7+8+12+1+7 [=42] and divide by 7, which gives us 6, so we know the average here is 6...

Making sense so far? Ok, so now on to our use here, and why we divide by 2, but not on the first calculation.

So, the idea here is that in general, averages are calculated like this - sum(samples)/num(samples)

Easy enough yeah? Ok, so in every calculation, we've already got the average, so all we do is add another sample to it then divide by 2, thereby getting an average..

Now, see the next post.. I think I can reduce the load on the system here- I'm seeing way too much math going on in that module, and I think I can seriously cut the load down, now that I've seen how you're calc'ing this.. Although that's not a reason to not put this math in place, as it does work reasonably well..
 
 
Page 1 of 2 • Jump to page  1 2
YOU ARE NOT LOGGED IN.
 

POST A NEW MESSAGE

40
USERS ONLINE
Who's Online: Spurtis55, and 39 guests.


Privacy Policy  • Site Statistics  • @urlashare