473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how to use mutliple values calculated by a function?

I have a function that calculates 10 pairs of values (x,y co-ordinates of a graph). I want to use these to calculate the area under the curve.Although I can plot the graph, I do not know how I get to use the values in the following expression:
(x10-x9)*(y10-y9)+(x9-x8)*(y9-y8)+...
Can one create an array of x,y pairs ( how?) or is there some other way to identify the output as x,y pairs to be used in the way that I wish to.
The reason why I cannot simply enter the values is that the graph changes dependent upon the distribution of another set of data which responds to a slider
I am new to this, so please be explicit in any explanation that you might give.
Many thanks
Oct 4 '10 #1
7 1648
gits
5,390 Expert Mod 4TB
you might simply use nested arrays like:

Expand|Select|Wrap|Line Numbers
  1. var valPairs = [[1,2], [3,4], [5,6]];
  2.  
  3. // alerts the first pair of values
  4. // which is an array itself
  5. alert(valPairs[0]);
Oct 4 '10 #2
The problem is that I don't know how to make the output of the function an array. I use a looped calculation for (var i=0; i<10; i++) which generates the ten or so pairs: using document.write the output of the function is this
1 1

0.98 1

0.84 1

0.5 0.94

0.16 0.7

0.02 0.31000000000000005

0.02 0.31000000000000005
I don;'t know how to use this output and identify each as an x,y pair to do the further calculation.
Thanks for your suggestions.
Oct 4 '10 #3
gits
5,390 Expert Mod 4TB
please post some code ... basically you might use something like this:
Expand|Select|Wrap|Line Numbers
  1. var valuePairs = [];
  2.  
  3. for (var i = 0; i < 10; i++) {
  4.     valuePairs.push([i, i + 1]);
  5. }
which will produce the following array:
Expand|Select|Wrap|Line Numbers
  1. [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10]]
Oct 4 '10 #4
Gits
Could I post directly to you perhaps. The code is a few hundred lines long and I think one is restricted to 20 lines with this forum.
Peter
Oct 4 '10 #5
I made a boo boo in posting the code in the e-mail. Hope that this is correct. Not done this before. My apologies
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.  <head>
  4.   <title> AUC calculation</title>
  5.  
  6.   <style type="text/css">
  7.   p    {
  8.   font-family:comic sans MS;
  9.   color:blue;
  10.   }
  11.   th    {
  12.   font-family:comic sans MS;
  13.   color:#330033;
  14.   }
  15.   td    {
  16.   font-family:comic saNS ms;
  17.   COLOR:#6600cc;
  18.   }
  19.   </style>
  20.  
  21.  </head>
  22.  
  23.  <body >
  24.   <p> I wonder whether I shall ever get this right? At least everything appears on the same page! And in a table too... Trouble is, I cannot make them into numbered pairs and use them in a formula.</p>
  25.   <table border="1">
  26.   <tr><th>x</th><th>y</th></tr>
  27.   <script type="text/javascript">
  28.  
  29.     var FPF=[];
  30.     var TPF =[];
  31.  
  32.     var x=-1;
  33.     //parseFloat($('#display').val());//1;//slider_form.elements["display"].value;//slider_form.elements.display.value;// slider value
  34.     var LT = parseFloat(-3 + (0.8571 *x));//changes slider value to the left tail value 'with disease'
  35.     var RT= 3;//value for right tail of 'without disease'
  36.     var c= parseFloat(1.739 + (0.6116*x)+(0.01297*x*Math.exp(2))-(0.01142*x*Math.exp(3))-(0.004572*x*Math.exp(4))); //intersect between two curves
  37.  
  38. //from Stephen Whitney application 25yearsofprogramming.com 'probability.js'
  39.  function NormalDensityZx(x, Mean, StdDev)
  40. {
  41.     var a = x - Mean;
  42.     return Math.exp(-(a * a) / (2 * StdDev * StdDev)) / (Math.sqrt(2 * Math.PI) * StdDev); 
  43. }
  44.  
  45. //----------------------------------------------------------------------------------------------
  46. // Calculates Q(x), the right tail area under the Standard Normal Curve. 
  47. function StandardNormalQx(x)
  48. {
  49.     x = i;
  50.     if(x === 0)    // no approximation necessary for 0
  51.         return 0.50;
  52.  
  53.     var t1,t2,t3,t4,t5,qx;
  54.     var negative = false;
  55.     if(x < 0)
  56.     {
  57.         x = -x;
  58.         negative = true;
  59.     }
  60.     t1 = 1/(1 + (0.2316419 * x)); 
  61.     t2 = t1 * t1; 
  62.     t3 = t2 * t1; 
  63.     t4 = t3 * t1; 
  64.     t5 = t4 * t1;
  65.     qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) + 
  66.                                 (1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
  67.     if(negative == true)
  68.         qx = 1 - qx;
  69.     return qx;
  70. }
  71.  
  72.  
  73. //----------------------------------------------------------------------------------------------
  74. // Calculates P(x), the left tail area under the Standard Normal Curve, which is 1 - Q(x). 
  75. function StandardNormalPx(x)
  76. {
  77.  
  78.  
  79.     x = -2.8+(i-LT);
  80.     if(x === 0)    // no approximation necessary for 0
  81.         return 0.50;
  82.  
  83.     var t1,t2,t3,t4,t5,qx;
  84.     var negative = false;
  85.     if(x < 0)
  86.     {
  87.         x = -x;
  88.         negative = true;
  89.     }
  90.     t1 = 1/(1 + (0.2316419 * x)); 
  91.     t2 = t1 * t1; 
  92.     t3 = t2 * t1; 
  93.     t4 = t3 * t1; 
  94.     t5 = t4 * t1;
  95.     qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) + 
  96.                                 (1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
  97.     if(negative == true)
  98.         qx = 1 - qx;
  99.  
  100.  
  101.     return 1 - qx;
  102.  
  103. }
  104. //end Stephen Whitney probability.js
  105.  
  106. //calculate points for ROC plot from -3 to +3 at unit intervals    
  107. var valuePairs=[FPF,TPF];
  108. for (var i =-3; i<3.1; i++ ){
  109.  
  110. if (i<LT){
  111.  
  112.             PQAQX = (StandardNormalQx(x).toFixed(2)); //AUC false positives based on AUC = 1
  113.             FPF =parseFloat(StandardNormalQx(x).toFixed(2));
  114.             TPF = 1;}
  115.  
  116. else if (i>LT && i<RT){
  117.  
  118.  
  119. PQAQX= (StandardNormalQx(x).toFixed(2));
  120. PQAPX= (StandardNormalPx(x).toFixed(2));
  121.  
  122. TPF = (1-StandardNormalPx(x).toFixed(2));
  123. FPF = parseFloat(StandardNormalQx(x).toFixed(2));
  124. }
  125.  
  126.  
  127. else if (i >3.01){
  128.  
  129. PQAPX= (StandardNormalPx(x).toFixed(2));
  130. TPF = (1-StandardNormalPx(x).toFixed(2));
  131. FPF = 1;
  132.         }
  133.          valuePairs.push([i, i + 1]); 
  134.         //var xArray = new Array(FPF.toFixed(2));
  135.         //var yArray = new Array(TPF.toFixed(2));
  136.  
  137.         //document.writeln('<tr><td>'+(xArray[0])+'</td><td>'+(yArray[0])+'</td></tr>');
  138.  
  139.  
  140.         //calculate area under the curve
  141.         //take x and y values from function and do AUC calculation
  142.         //var G1= 1-Math.sum((x-x)*(y-y)); this function does not exist! description only
  143.         //var AUC= (G1 + 1)/2;
  144.         //var d=AUC-0.5;
  145.         }
  146.         </script>
  147.         </table>
  148.  </body>
  149. </html>
  150.  
  151.  
Oct 4 '10 #6
gits
5,390 Expert Mod 4TB
so what is your desired output and how do you want to process the values later on ...
Oct 7 '10 #7
I want to take the values from line 137 ( the function that finishes line 132 and then use these results in the calculation described on line 142. So I need to be able to extract x1, subract x2 from it, multiply the difference between y1 and y2 and then add this number to the difference between x2 and x3 multiplied by the difference between y2 and y3 and so on to the end of the array. My problem is that I cannot extract particular numbers from the array. I have tried x[0], x[0][0], but this just gives 'undefined'
THe generic description of the calulation would be:
the sum of (xn-xn-1)*(yn-yn-1)from the first x,y pair to the last x,y pair
Oct 7 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David Isaac | last post by:
Alan Isaac wrote: > Default parameter values are > evaluated once when the function definition is > executed. Where are they stored? ... Where is this documented? Forgive any poor phrasing: I'm...
0
by: Mike Copeland | last post by:
In the following code I am calculating 2 values when the "Compute Distance" button is pressed. I would like to find a way to "return" both value to the form so I can show both when the calculation...
5
by: Abby Lee | last post by:
I want to check the value for a dropdown menu for "other" to make it so they can add a location that is not on the list. If the value = "other" I will bring up a dilog box they can enter their...
1
by: Marco | last post by:
Hi all I am using .NET reflection to examine a stack trace, using the StackTrace, StackFrame, MethodInfo, ParameterInfo and FieldInfo classes. I can easily discover all methods in the call...
9
by: JD | last post by:
Hi guys What if I need to return two values from a function. What's the best way to do it? Function is to load a text file into memory like so: -----8<----- char *LoadFile(char *infile)
5
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
0
by: Karlo Lozovina | last post by:
Hi all! I have a runTest() function inside my module, which sets up and initializes lots of objects and performs some basic tests on them. Usually (from IPython) I just write `run my_module.py`,...
6
by: chazzy69 | last post by:
Hi, im just wondering if some how possible to return 2 values from the same function that arn't conditional, that is it always returns 2 values. for e.g. (this is not what my function actually...
1
by: Murdz | last post by:
Hi all, A constructor for System.Text.RegularExpressions.Regex includes a 2nd parameter for the RegexOptions enum. With this you can pass multiple RegexOptions as below: ...
3
by: bubblediana | last post by:
<?php $county = array (´Kent´,´West Susses´,´East Sussex´,´Sommerset´,´Hampshire´,´London´,´Essex´); $hear = array (´Google´,´Other Search Engine´,´Magazine´,´Recommendation´); function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.