Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More timing values #96

Open
gikeymarcia opened this issue Apr 20, 2015 · 4 comments
Open

More timing values #96

gikeymarcia opened this issue Apr 20, 2015 · 4 comments

Comments

@gikeymarcia
Copy link
Owner

Currently timing can be:
User - allow trial to go until the user submits
# - directly set the # of seconds a trial will last. Values are converted to nearest millisecond (e.g., 7 -> 7000 or 1.12453 -> 1245).

We should add:
rand(min,max) - random selection bound by minimum and maximum # of seconds
normRand(mean, sd) - randomly sample a normal distribution of with a given mean and standard deviation (sd).
normRand(mean, sd, Zcap) - same as normRand but you can set a Zcap value that will not let the function return values more extreme than the absolute value of Zcap.

$randVal = dummyFunction();
if (is_numeric($Zcap) {
    $tooExtreme = TRUE;
    while ($tooExtreme == TRUE) {
        $z = abs(($randVal - $mean) / $sd);
        $Zcap = abs($Zcap);
        if ($z < $zcap) {
            $tooExtreme = FALSE;
        }
        $randVal = dummyFunction();
    }
}
@gikeymarcia gikeymarcia added this to the Collector 2.0 milestone Apr 20, 2015
@TysonKerr
Copy link
Collaborator

if (is_numeric($Zcap)) {
    $Zcap = abs($Zcap);
    while (abs(($randVal - $mean) / $sd) > $Zcap) {
        $randVal = dummyFunction();
    }
}

I think in your function, on the same loop iteration that declares $tooExtreme = FALSE, $randVal could then be assigned a too extreme value, since it still calls dummyFunction() again. Or, you could just make it

        if ($z < $zcap) {
            $tooExtreme = FALSE;
        } else {
            $randVal = dummyFunction();
        }

@gikeymarcia
Copy link
Owner Author

Nice catch. Now we just have to write the dummyfunction.

@TysonKerr
Copy link
Collaborator

here is the javascript solution i used in a trial type:

        // user5084 on stackoverflow provided the next two functions to use the Box-Muller transformation to generate random numbers
        // source http://stackoverflow.com/questions/75677/converting-a-uniform-distribution-to-a-normal-distribution
        // I added an extra step to round the result to an integer, which might create some bias? I am no statistician, so I don't know
        /*
         * Returns member of set with a given mean and standard deviation
         * mean: mean
         * standard deviation: std_dev 
         */
        function rnorm(mean,std_dev){
            return Math.round(mean + (gaussRandom()*std_dev));
        }

        /*
         * Returns random number in normal distribution centering on 0.
         * ~95% of numbers returned should fall between -2 and 2
         */
        function gaussRandom() {
            var u = 2*Math.random()-1;
            var v = 2*Math.random()-1;
            var r = u*u + v*v;
            /*if outside interval [0,1] start over*/
            if(r == 0 || r > 1) return gaussRandom();

            var c = Math.sqrt(-2*Math.log(r)/r);
            return u*c;

            /* todo: optimize this algorithm by caching (v*c) 
             * and returning next time gaussRandom() is called.
             * left out for simplicity */
        }

in php, this would be

function rnorm($mean, $stDev) {
    return round($mean + gaussRandom() * $stDev);
}

function gaussRandom() {
    $u = 2 * mt_rand() / mt_getRandMax() - 1;
    $v = 2 * mt_rand() / mt_getRandMax() - 1;
    $r = $u * $u + $v * $v;

    if ($r == 0 || $r >= 1) return gaussRandom();

    $c = sqrt(-2 * log($r) / $r);

    return $u * $c;
}

@adamblake
Copy link
Collaborator

I started working on this on the branch 96-moretiming. All that needs to be done next is to update the timing code in Experiment.php to compute statements like randNorm(5, 2, 1) and update the timing values with the computed values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants