rythemton Posted April 2, 2012 Share Posted April 2, 2012 $anarray2 = array(); for( $a = 1; $a <= 97; $a++ ) { for( $b = 1; $a + $b < 100; $b++ ) { for( $c = 1; $a + $b + $c < 100; $c++ ) { $d = 100 - ( $a + $b + $c ); $var_1 = $a / 100; $var_2 = $b / 100; $var_3 = $c / 100; $var_4 = $d / 100; $anarray2[] = array( $var_1, $var_2, $var_3, $var_4 ); } } } print_r( $anarray2 ); $anarray2 = array(); for( $a = 1; $a <= 97; $a++ ) { for( $b = 1; $a + $b <=98 ; $b++ ) { for( $c = 1; $a + $b + $c <= 99; $c++ ) { $d = 100 - ( $a + $b + $c ); $var_1 = $a / 100; $var_2 = $b / 100; $var_3 = $c / 100; $var_4 = $d / 100; $anarray2[] = array( $var_1, $var_2, $var_3, $var_4 ); } } } print_r( $anarray2 ); That's how a 4th variable would look. I would think that a recursive function might be what you need, but I'm not sure how it would be constructed. Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1333700 Share on other sites More sharing options...
unemployment Posted April 2, 2012 Author Share Posted April 2, 2012 Yeah, a recursive function makes the most sense. I'll do my best to try and come up with something. Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1333717 Share on other sites More sharing options...
unemployment Posted April 4, 2012 Author Share Posted April 4, 2012 I can't figure out how to create the recursive function for this. This is as far as I have gotten and it's not very far. Any ideas? <?php function combination($total = 3){ for($i = 1; $i <= $total: $i++){ return combination($total - 1); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1334191 Share on other sites More sharing options...
unemployment Posted April 4, 2012 Author Share Posted April 4, 2012 Does anyone have an answer to make this a dynamic function? The only parameter that should be passed through is the number of stocks in the portfolio to determine the number of for loops to create. Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1334292 Share on other sites More sharing options...
rythemton Posted April 26, 2012 Share Posted April 26, 2012 I made the following, and they work: function create1array( $iterations ) { if( $iterations <= 1 ) return array( array( 1 ) ); else { $maxarray = maxarray( 99, $iterations - 1 ); foreach( $maxarray AS $key1=>$value1 ) { $cursum = 0; foreach( $value1 AS $value2 ) { $cursum += $value2; } $maxarray[$key1][] = 100 - $cursum; } $onearray = array(); foreach( $maxarray AS $value1 ) { $newarray = array(); foreach( $value1 AS $value2 ) { $newarray[] = $value2 / 100; } $onearray[] = $newarray; } return $onearray; } } function maxarray( $maxvalue, $iterations ) { if( $iterations <= 1 ) { $makearray = array(); for( $i = 1; $i <= $maxvalue; $i++ ) { $makearray[] = array( $i ); } return $makearray; } else { $makearray = array(); $startarray = maxarray( $maxvalue - 1, $iterations -1 ); foreach( $startarray AS $value1 ) { $cursum = 0; foreach( $value1 AS $value2 ) { $cursum += $value2; } for( $i = 1; $i <= $maxvalue - $cursum; $i++ ) { $nextarray = $value1; $nextarray[] = $i; $makearray[] = $nextarray; } } return $makearray; } } The functions ate memory resources fast - I got out of memory errors when trying more than 3. Good Luck! Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1340765 Share on other sites More sharing options...
DavidAM Posted April 26, 2012 Share Posted April 26, 2012 Maybe you should look at this problem in a different way. Why? Because the number of possible combinations increases exponentially with each added stock. The number of permutations is roughly equal to 100 raised to the power of (the number of stocks minus 1) -- roughly, because you excluded zero and therefore 100 from the possibilities. So: Stocks Formula Result 1 100^0 1 2 100^1 100 3 100^2 10,000 4 100^3 1,000,000 Who is going to sit around and wait for your webpage to calculate one million permutations and then wait for you to multiply all those percentages by their available funds; and then wait for you to loop through the results and determine the "proper asset allocation"? This page will take forever. Not to mention the memory exhaustion problem that rythemton pointed out. If you could define what "proper asset allocation" means in terms of a formula, you could do it with far fewer calculations. For instance; let's say that Current Highest Price gets 2 points, Biggest Price Jump in Past 6 Months gets 4 points, Biggest Price Jump in Past 12 Months gets 2 points, Current Highest PE Ratio gets 3 points. Then you calculate the "points" for each stock (warning the following numbers are coming straight out of my @#$%&!) Highest Price: Yahoo Biggest 6-Month Jump: Google Biggest 12-Month Jump: Apple Highest P/E: Yahoo Points: Yahoo => 2 + 3 = 5 Google => 4 Apple => 2 TOTAL = 11 Allocation: Yahoo => (5 / 11) = 45.45% ==>> $AvailableFunds * .4545 Google => (4 / 11) = 36.36% ==>> $AvailableFunds * .3636 Apple => (2 / 11) = 18.18% ==>> $AvailableFunds * .1818 Of course, you have to handle rounding issues and so forth. But this cuts the number of calculations you have to do down to a linear number, uses far fewer resources, and will return the results in a reasonable amount of time. Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1340826 Share on other sites More sharing options...
michiyan Posted July 19, 2012 Share Posted July 19, 2012 Hey unemployment, I'm working on the same issue...have you already found a solution ? I think that the only solution is to find an algorithm to iterate the result of a standard quadratic program... Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1362652 Share on other sites More sharing options...
Barand Posted July 19, 2012 Share Posted July 19, 2012 Incrementing by 5% keeps it manageable. Here's a recursive function for you <?php // portfolio size (2 or more) $num = 3; // get the combinations $results = array(); combo($results,$num, array()); // output the results $format = str_repeat('%3d ', $num) . '<br />'; echo '<pre>'; foreach($results as $r) { vprintf($format, $r); ; } echo '</pre>'; // the recursive function function combo(&$results, $n, $com) { if ($n < 2) exit("Must have at least 2 stocks"); $sum = array_sum($com); $k = count($com); if ($k == $n-1) { $com[] = 100 - $sum; $results[] = $com; } else { $max = 100 - ($n-$k-1)*5; for ($i=5; $i+$sum<=$max; $i+=5) { combo($results, $n, array_merge($com,array($i))); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1362679 Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 I still don't understand what the goal here is, beyond a brute-force style calculation. What makes any given result better than the other? Quote Link to comment https://forums.phpfreaks.com/topic/260134-loop-through-three-variables-but-make-them-always-1/page/2/#findComment-1362762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.