User:Bobbyb30 Swashbuckler/Standard Deviation

From Second Life Wiki
< User:Bobbyb30 Swashbuckler
Revision as of 08:21, 19 March 2010 by Bobbyb30 Swashbuckler (talk | contribs) (standard deviation implementation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

My own implementation of Standard Deviation (the official is LIST_STAT_STD_DEV): <lsl> //Standard Deviation function v1.1 //Created by Bobbyb30 Swashbuckler //Created: June 11, 2009 //Last Modified: March 18, 2010 //Released into Public Domain ///////////////////////////////////////////// //This function will return the standard deviation of an input list. float standarddeviation(list inputlist)//calculates the standard deviation of the times list {

   //http://en.wikipedia.org/wiki/Standard_deviation
   float mean = llListStatistics(LIST_STAT_MEAN,inputlist);//get the mean of the list
   integer inputlistlength = llGetListLength(inputlist);//get the length of the list
   integer counter;

   float sumofdifferencesquared;
   do
   {
       float differencesquared = llList2Float(inputlist,counter) - mean;
       //differencesquared = differencesquared * differencesquared;//*=differencesquared square it
       sumofdifferencesquared += (differencesquared * differencesquared);
       
   }while(++counter < inputlistlength);

   return llSqrt(sumofdifferencesquared/inputlistlength);

} </lsl>

The function takes about .05 seconds for 100 numbers as calculated per the following test: <lsl> //*********************************************************************************************************** // * // --Speed Tester-- * // * //*********************************************************************************************************** // www.lsleditor.org by Alphons van der Heijden (SL: Alphons Jano) //Creator: Bobbyb30 Swashbuckler //Attribution: none //Created: December 1, 2008 //Last Modified: March 18, 2010 //Released: Wed, March 17, 2010 //License: Public Domain //Status: Fully Working/Production Ready //Version: 1.2.0 //LSLWiki: https://wiki.secondlife.com/wiki/Speed_Tester

//Name: Speed Tester.lsl //Purpose: To test the speed of certain LSL snippets. //Technical Overview: Uses 2 loops, one for trials, one for the test counter //Directions: This is meant to be used by scripters, it should be easy to use. Modify the test() with your snippet, and touch to begin the test.

//Compatible: Mono & LSL compatible //Other items required: None //Notes: If it takes too long, simply reset. // Do the test in an empty region, with as few script as possible to prevent contamination. (Make sure you aren't // wearing scripted attatchments). // This test is not 100% accurate, but it should give you a good idea as to whether or not something is faster. // Anymore than 50 trials may not get printed out in the csv due to charactar limit. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//User modifiable variables integer trials = 10;//how many trials to conduct integer testcounter = 100;//how many times to conduct the test string testname = "speed";//what to call your test, optional integer discardfirsttrial = FALSE;//whether or not to discard the first trial...this can be slower as the script is loaded into the VM for the 1st time following a save

//// //declare any of your global variables here //Standard Deviation function v1.1 //Created by Bobbyb30 Swashbuckler //Created: June 11, 2009 //Last Modified: March 18, 2010 //Released into Public Domain ///////////////////////////////////////////// //This function will return the standard deviation of an input list. float standarddeviation(list inputlist)//calculates the standard deviation of the times list {

   //http://en.wikipedia.org/wiki/Standard_deviation
   float mean = llListStatistics(LIST_STAT_MEAN,inputlist);//get the mean of the list
   integer inputlistlength = llGetListLength(inputlist);//get the length of the list
   integer counter;

   float sumofdifferencesquared;
   do
   {
       float differencesquared = llList2Float(inputlist,counter) - mean;
       //differencesquared = differencesquared * differencesquared;//*=differencesquared square it
       sumofdifferencesquared += (differencesquared * differencesquared);
       
   }while(++counter < inputlistlength);

   return llSqrt(sumofdifferencesquared/inputlistlength);

}

//////test //100 numbers generated from http://stattrek.com/Tables/Random.aspx


float standarddeviationtest(list inputlist)//calculates the standard deviation of the times list {

   //http://en.wikipedia.org/wiki/Standard_deviation
   float mean = llListStatistics(LIST_STAT_MEAN,inputlist);//get the mean of the list
   integer inputlistlength = llGetListLength(inputlist);//get the length of the list
   integer counter;

   float sumofdifferencesquared;
   do
   {
       float differencesquared = llList2Float(inputlist,counter) - mean;
       //differencesquared = differencesquared * differencesquared;//*=differencesquared square it
       sumofdifferencesquared += (differencesquared * differencesquared);
       
   }while(++counter < inputlistlength);

   return llSqrt(sumofdifferencesquared/inputlistlength);

}

string randomnumbers = "07997 11606 74221 12933 23355 38973 18678 79562 96911 97979 94775 04388 48990 62067 75953 34296 59267 57795 88770 95843 01184 85162 33228 62472 48181 17610 82766 87298 26819 81698 90502 17206 10538 77426 03320 52195 52454 04792 21219 45382 99452 37241 97316 43650 78089 27887 78494 00115 88366 23614 91311 35105 03724 24423 35768 28291 50722 83430 11865 26560 29360 47518 93043 83025 39636 43245 05861 18015 54995 70612 50318 11201 01993 03061 99856 09470 80630 93707 07592 39377 90907 89434 20410 00924 06265 90243 38309 94111 53263 49250 14406 92379 31900 13338 95584 22287 42177 09065 08401 83834 ";

list listrandomnumbers;//the list of random numbers

test()//in here goes the test {

   standarddeviationtest(listrandomnumbers);

}

//insert the variables you want to clear after each trial clearvariables() {

}

//which variables to use(but not reset) for the duration of all trials. initializevariables() {

   listrandomnumbers = llParseString2List(randomnumbers,[" "],[]);

}


///////////////////////////////////////////////////////////////////////////////////////

//do not modify below ////////////////////////// //global variables...do not modify, except to change whether or not to round list times;//the list of times, do not modify this one


default {

   state_entry()
   {
       llOwnerSay("Speed Tester.lsl' released into Public Domain by Bobbyb30 Swashbuckler (C) 2009");
       initializevariables();
       if(llStringTrim(testname,STRING_TRIM) != "")//if testname isn't empty, print out test name
           llOwnerSay("Touch to begin the '" + testname + "' test.");
   }

   touch_start(integer total_number)
   {
       //print out the headers
       llOwnerSay("\n \n=================================================================================");//break
       if(llStringTrim(testname,STRING_TRIM) != "")//if testname isn't empty, print out test name
           llOwnerSay("Running the '" + testname + "' test in Region: " + (string)llGetRegionName());
       else
           llOwnerSay("Running the test.");

       llOwnerSay("Start Time:" + llGetTimestamp());

       times = [];//reset the times


       ///////
       //the actual test loop
       integer counter;
       integer trialscounter;

       do//the trial loop
       {

           clearvariables();//clear user variables before each test
           counter = 0;//reset counter for testcounter before each trial
           llResetTime();//Reset time before each trial
           do//the test counter loop
           {
               test();//the test to perform
           }while(++counter < testcounter);//the test counter loop
           times += (llGetTime()/testcounter);//add the trial time to the list
           //above you can determine whether to add llRound, (integer) before llGetTime(), or leave as float for more accuracy                   


       }while(++trialscounter < trials);//the trial loop
       /////////////

       if(discardfirsttrial)//whether or not to delete the first trial
       {
           times = llDeleteSubList(times,0,0);//delete from list
           --trials;//one less trial
       }

       llOwnerSay("---------");
       llOwnerSay("Test: " + testname + " Complete.");
       llOwnerSay("Number of trials: " + (string)trials);
       if(discardfirsttrial)//whether or not to delete the first trial
           llOwnerSay("...First trial was deleted.");
       llOwnerSay("Testcounter: " + (string)testcounter);
       llOwnerSay("Trial times: " + llList2CSV(times));
       llOwnerSay("----------");

       //print out results
       llOwnerSay("End Time:" + llGetTimestamp());
       llOwnerSay("Total test time: " + (string)(llListStatistics(LIST_STAT_SUM,times) * testcounter) + " seconds.");
       llOwnerSay("Min: " + (string)llListStatistics(LIST_STAT_MIN,times));//minimum time
       llOwnerSay("Avg: " + (string)llListStatistics(LIST_STAT_MEAN,times));//average time
       llOwnerSay("Median: " + (string)llListStatistics(LIST_STAT_MEDIAN,times));//median time
       llOwnerSay("Max: " + (string)llListStatistics(LIST_STAT_MAX,times));//maximum time
       llOwnerSay("Range: " + (string)llListStatistics(LIST_STAT_RANGE,times));//average time
       llOwnerSay("Standard Deviation: " + (string)standarddeviation(times));//standard deviation
       if(llStringTrim(testname,STRING_TRIM) != "")//if testname isn't empty, print out test name
           llOwnerSay("END OF '"+ testname + "' TEST");
       else
           llOwnerSay("END OF TEST");
       llOwnerSay("=========================================================================");
   }

} </lsl>

which gave

=================================================================================
[8:59]  standard deviation: Running the 'speed' test in Region: Sandbox Admicile
[8:59]  standard deviation: Start Time:2010-03-19T15:59:52.559269Z
[9:00]  standard deviation: ---------
[9:00]  standard deviation: Test: speed Complete.
[9:00]  standard deviation: Number of trials: 10
[9:00]  standard deviation: Testcounter: 100
[9:00]  standard deviation: Trial times: 0.049280, 0.039322, 0.030349, 0.027295, 0.028399, 0.026759, 0.027066, 0.027543, 0.026988, 0.026021
[9:00]  standard deviation: ----------
[9:00]  standard deviation: End Time:2010-03-19T16:00:23.487198Z
[9:00]  standard deviation: Total test time: 30.902230 seconds.
[9:00]  standard deviation: Min: 0.026021
[9:00]  standard deviation: Avg: 0.030902
[9:00]  standard deviation: Median: 0.027419
[9:00]  standard deviation: Max: 0.049280
[9:00]  standard deviation: Range: 0.023259
[9:00]  standard deviation: Standard Deviation: 0.007145
[9:00]  standard deviation: END OF 'speed' TEST
[9:00]  standard deviation: =========================================================================