Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing ranges in R
    primarykey
    data
    text
    <p>I have the following problem and don't know how to code it in R:</p> <p>A dataframe <code>df</code> with two columns: First column an ordered set of numbers <code>df$ind</code>, the second column a highly stochastic set of numbers <code>df$ret</code>.</p> <p>I want to find two ranges in <code>df$ret</code> that don't overlap and optimize the condition that the sum of the numbers in the first range must be positive and the sum of the numbers of second range negative. After that I would like to have the 2x2 <code>df$ind</code>-values of the respective ranges.</p> <p>Two possibilites come to my mind (and I don't know how to program either of these in R):</p> <ol> <li>Monte Carlo where 2x2 positions are being chosen, the sums being calculated and compared to the hitherto best solution.</li> <li>Trying all possible ranges and taking the best solution (concerning the number of values this seems computationally doable).</li> </ol> <p>Could you give me some hints how to realize this in R or is there even a package for these kinds of optimizations (there seems to be a package for everything in R ;-)</p> <p><strong>UPDATE:</strong><br> You will have 4 values: <code>i</code>, <code>k</code>, <code>m</code>, <code>n</code>: <code>df$ret[i:k]</code> and <code>df$ret[m:n]</code> with <code>i</code> &lt; <code>k</code> &lt; <code>m</code> &lt; <code>n</code>.</p> <p>The optimization is (pseudocode): </p> <blockquote> <p>max:abs(sum(range(i:k)))+abs(sum(range(m:n)))</p> </blockquote> <p><br> under the conditions: </p> <blockquote> <p>sum(range(i:k)) > 0 and sum(range(m:n)) &lt; 0</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload