tag:blogger.com,1999:blog-71258038112137769612020-02-29T08:06:47.389+01:00eXanockexanockhttp://www.blogger.com/profile/07452599334816381012noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-7125803811213776961.post-88431894482664843362008-01-08T14:27:00.000+01:002008-01-09T08:46:25.530+01:00PHP and JavaScript: Drawing a binomial distribution bar graph using the GD library<p class="eXanock-note"><b>Note:</b> This script is licensed under a <a target="_blank" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported license</a>.<br /><br />This means that you are free to use, modify and distribute my work, as long as you:<br />1) Attribute me in an acceptable manner. By this, I mean that you must make it clear to anyone using a copy of this code or code based upon this code that I, eXanock, am the author of the original script. You must also include a link to my website: <a href="http://exanock.blogspot.com/">http://exanock.blogspot.com</a>.<br />2) Release this code and any code based upon it under the same license as I have.</p><br />In <a target="_blank" href="http://exanock.blogspot.com/2007/09/ti-84-plus-program-binomial.html">a previous post</a>, we had a look at how to create two TI-BASIC programs that would reduce the amount of keystrokes required to be performed by the user when calculating binomial distribution and the sum of binomial distributions.<br /><br />In todays post we will return to this subject. Only this time we are going to use JavaScript, PHP and the GD library in order to draw a bar chart showing the binomial distributions. In this example, we are going draw a graph showing the probability for 0 to 10 successes out of 10 trials where the probability for for success, p, is entered by the user.<br /><br />First, we are going to create the PHP script that draws the chart. Then we will create a HTML document which uses JavaScript to pass the probability-variable (entered by the user in a form) via the url to the PHP script, which in turn will calculate the binomial distribution and draw the graph. In the PHP script, we are going to check that the probability lies in the interval 0≤p≤1. This is to ensure that:<br />1) The value entered in the form is correct.<br />2) If someone manually enters an invalid value into the url of the PHP script, the image that is drawn will only display an error message.<br /><br />Tools needed for this tutorial:<br /><li />A server with <a target="_blank" href="http://www.php.net/">PHP</a> and the <a target="_blank" href="http://www.libgd.org/Main_Page">GD library</a> installed. (I recommend <a target="_blank" href="http://www.apachefriends.org/en/xampp.HTML">XAMPP</a>, which recquires no advanced setup. Just download and install.)<br /><li />A php editor. I prefer using <a target="_blank" href="http://notepad-plus.sourceforge.net/uk/site.htm">the free source code editor Notepad++</a>, so that's what I'll be using. However Notepad (or any similar text editor) will work just fine.<br /><br />The image we are going to make will be created on the fly, and therefore no image is stored on the server.<br /><br />The first thing you need to do is to create a new PHP document. I will call mine graph.php<br /><br />Open the file in your prefered text/php editor.<br /><br />As always when writing PHP, we begin our code with the line:<br /><br /><div class="eXanock-code"><code><?php</code></div><br />Then, we must tell the browser that it's recieving an image in the png format:<br /><br /><div class="eXanock-code"><code>header("Content-type: image/png");</code></div><br />The next thing we have to do is to create a variable that set up the image width and height. I'll call this variable im, and I'll be using a document size of 125*115 pixels. As you can see, it will display an error message if it failes to create the image:<br /><br /><div class="eXanock-code"><code>$im = @imagecreate(125, 120) or die("Cannot Initialize new GD image stream");</code></div><br />Before we begin drawing, we must define the variables, including those for the colors. I'll be using white as background color, the graphs axises will be black, the numberson the axises blue and the bars will be red and green. I will also draw some gray grids to make the graph easier to read.<br /><br /><div class="eXanock-code"><code>$background_color = imagecolorallocate($im, 255, 255, 255);<br />$black = imagecolorallocate($im, 0, 0, 0);<br />$red = imagecolorallocate($im, 255, 0, 0);<br />$green = imagecolorallocate($im, 0, 255, 0);<br />$blue = imagecolorallocate($im, 0, 0, 255);<br />$grey = imagecolorallocate($im, 200, 200, 200);</code></div><br /><br />Then, we define the probability variable, which I have chosen to call prob. Remember that you can call this variable whatever you want to, but choosing locigal names for your variables is a great advantage because this makes it easier for others (and yourself at a later point) to understand what happens in the script (you should also make lots of comments in your script about what happens in the code. I have not included these comments in this tutorial because I'm explaining everything - at least most of it - step by step. The code is, however, commented in <a class="eXanock-dl" target="_blank" href="http://www.mediafire.com/?a92gomdjxux">the zip which you can download from here</a>).<br /><br />We use $_GET['variable'] to get the variable from the url. Say for example that you have the url http://www.yourwebsite.com/script.php?a=30&b=45 and you want to use the variables a and b into your script. Then you simply use $new_variable_name = $_GET['a']; and $another_variable_name = $_GET['b']; to define the variables. Since we want to make the url as short as possible, we only use p to define the probability in the url.<br /><br /><div class="eXanock-code"><code>$prob = $_GET['p'];</code></div><br />Before we find the probability for each of the r successes out of 10 trials, we add an if statement, to check wether p lies in the interval where 0 ≤ P ≤ 1. We will calculate this with the formula (n C r)*p^r*(1-p)^(n-r). When calculating (n C r), we use the formula n!/(r!*(n-r)!). We must add a function to find the factorial of a number (the one I'm using here was written by <a target="_blank" href="http://www.php.net/manual/en/ref.math.php#65923">moikboy</a>). We must also define the variables i, n and r which will be need for the calculation.<br /><br /><div class="eXanock-code"><code>if ($probability >= 0 && $probability <= 1) { <br />function fact($int){<br /> if($int<2)return 1;<br /> for($f=2;$int-1>1;$f*=$int--);<br /> return $f;<br />};<br /><br />$i = 0;<br />$n = 10;<br />$r = 0;</code></div><br />To reduce the amount of code that needs to be written, we then make a while-loop. This loop will create 11 variables called bar0, bar1, bar2 and so on up to bar10. And calculate values (which will be the height of the bars) for each of these.<br /><br /><div class="eXanock-code"><code>while ($i <= 10) { <br /> ${bar.$i} = round(((fact($n)/(fact($r)*fact($n-$r)))*pow($prob, $r)*pow(1-$prob, $n-$r))*100);<br /> $i++;<br /> $r++;<br />}</code></div><br />The next thing we are going to do, is to draw the background grids, again we use a loop to reduce the amount of code needed. in order to draw the grids, we use imageline ($name_of_the_image, x1, y1, x2, y2, $color). This will draw a line in the image $name_of_the_image ($im in our case) from the pixel located at x1, y1 to the pixel at location x2, y2. Remember that the location of the upper left pixel is 0, 0.<br /><br />We start with the bottom grid, and for each new grid we reduce y1 and y2 with 10 pixels.<br /><br /><div class="eXanock-code"><code>$i = 0;<br /><br />while ($i <= 10) {<br /> imageline ($im, 7, 98-($i*10), 117, 98-($i*10), $grey);<br /> $i++;<br />}</code></div><br /><br />Now for the actuall bars. Each of the 11 bars will be 10 pixels wide. One pixel in height will represent one percent. We'll be using imagefilledrectangle ($name_of_the_image, x1, y1, x2, y2, $color) to draw the bars. This creates a filled rectangle of color $color in the image $name_of_the_image starting at upper left coordinates x1, y1 and ending at bottom right coordinates x2, y2. y1 is the variable that will determine the height of each bar.<br /><br />Once again, we use a loop. Inside this loop, we add an if-statement which will check wether the number of the bar divided by two gives a whole number. If so, the bar will be colored red. Else, the bar will be colored green. That way, the graph will be easier to read. Each new bar that we draw is placed 10 pixels to the right of the previous bar. We set y2 (the bottom line of the bar) to 108 and define y1 as 108 minus the height of the bar, ${bar.$i}. This way, all bars will have their bottom lines on the same height.<br /><br /><div class="eXanock-code"><code>$i = 0;<br /><br />while ($i <= 10) {<br /><br /> if ($i/2 == round($i/2)) { <br /> $color = $red;<br /> }<br /> else { <br /> $color = $green;<br /> }<br /><br /> imagefilledrectangle ($im, 8+($i*10), 108-${bar.$i}, 17+($i*10), 108, $color);<br /><br /> $i++;<br /> }</code></div><br />Then, we draw the axes and lable the x-axis.<br /><br /><div class="eXanock-code"><code>imageline ($im, 7, 8, 7, 108, $black); // y-axis<br />imageline ($im, 7, 108, 117, 108, $black); // x-axis<br /><br />$i = 0;<br /><br />while ($i <= 10) {<br /> imagestring($im, 3, 10+($i*10), 108, $i, $blue);<br /><br /> $i++;<br /> }</code></div><br />Now, we must close the if statement and write an else statement.<br /><br /><div class="eXanock-code"><code> }<br /><br />else { <br />imagestring($im, 3, 5, 5, "ERROR! P MUST BE", $red);<br />imagestring($im, 3, 5, 17, "IN THE INTERVAL", $red);<br />imagestring($im, 3, 5, 29, "WHERE 0 <= P <= 1", $red);<br /> }</code></div><br />The last piece of code that we need to add in order to complete the PHP script is imagepng($name_of_the_image), which will draw the actual image and imagedestroy($name_of_the_image), which will delete the image from the servers memory after it has been sendt to the user.<br /><br /><div class="eXanock-code"><code>imagepng($im);<br />imagedestroy($im);</code></div><br />And as allways, we close the script by adding this at the last line in the code:<br /><br /><div class="eXanock-code"><code>?></code></div><br />Now for the HTML document with the form field. Since this is a tutorial on JavaScript, PHP and the GD library, I'm not going to explain the HTML. Here is a standard HTML document setup:<br /><br /><div class="eXanock-code"><code><html><br /><head><br /><title>Bar graph: Binomial distribution</title><br /></head><br /><body><br /><br /></body><br /></html></code></div><br />We are going to add a form and a JavaScript into this document. Insert this code between <body> and </body>:<br /><br /><div class="eXanock-code"><code><form name="userdata"><br />Probability for success: <input type='text' name="p" /><br /><input type='button' onClick="mybarchart()" value='Draw bar chart' /><br /></form></code></div><br />All you need to know about the code above, is that onClick="mybarchart()" is going to activate the a function called mybarchart() in a JavaScript when the user clicks on the button that says "Draw bar chart". Also, you should note that the names I have assigned to the form and the input field will be used by the JavaScript to identify our form. Again, you can choose whatever names you like for these onces - just make sure that the name of the form and the name of the field are not the same, as that <b>could</b> cause problems.<br /><br />Let's add the JavaScript. Insert this between the <head> and the </head> tags. Preferably after the line with <title> and </title>. Start out by telling the browser that what comes here is a script. This is done by adding:<br /><br /><div class="eXanock-code"><code><script type='text/javascript'></code></div><br />Next, we add a function which we'll call mybarchart.<br /><br /><div class="eXanock-code"><code>function mybarchart(){</code></div><br />Then, we add a variable, which we call prob<br /><br /><div class="eXanock-code"><code> var prob = document.userdata.p.value;</code></div><br />What the line above does, is that it uses the names that we assigned earlier to locate the correct form (userdata) and then the correct input field (p) end then, it gets the value submited by the user in that field.<br /><br />Then, we tell the script to write an image-tag with the url to our PHP script. After graph.php?p= it writes out the value entered by the user.<br /><br /><div class="eXanock-code"><code> document.write("<img src='http://www.replacethiswithyourwebsite.com/graph.php?p=" +prob +"' width='125' height='115' />");</code></div><br />All that's left now, is closing the function and the script. This is done by adding:<br /><div class="eXanock-code"><code> }<br /></script></code></div><br />That's it. We're finished.<br /><br /><a class="eXanock-demo" target="_blank" href="http://exanock.110mb.com/php_graph/graph.htm">Demo</a> | <a class="eXanock-dl" target="_blank" href="http://www.mediafire.com/?a92gomdjxux">Download the .zip containing all code</a>exanockhttp://www.blogger.com/profile/07452599334816381012noreply@blogger.com2tag:blogger.com,1999:blog-7125803811213776961.post-34965323130820162482007-09-22T22:28:00.000+02:002008-12-09T06:09:39.045+01:00TI-BASIC Programs: Binomial Distribution and Sum of Binomial Distributions<span style="font-weight:bold;">Note: This is NOT a programming tutorial, but rather a walk through intended to show you the development of a program. Therefore, I am not going to explain the code line for line but I will explain the purpose of the program and tell you shortly about how it works.<br /><br />Even though I have written and tested this program on a TI-84 Plus calculator, it should work on any of Texas Instruments' graphing calculators as they all have <a href="http://en.wikipedia.org/wiki/TI-BASIC">the TI-BASIC language</a> built into them.</span><br /><br />If the only reason why you're reading this is because you need one of the programs, <a href="#bindistr">jump to the "Binomial Distribution" program code</a> or <a href="#binomsum-final">jump to the "Sum of Binomial Distributions" program code</a>. If you are here to read the whole thing, just keep on reading.<br /><br />For more information about the TI-84 Plus calculator, <a href="http://education.ti.com/educationportal/appsdelivery/download/download_eula.jsp?applicationId=6125&contentPaneId=17&cid=US">download the TI-84 Plus Guide Books</a>. In these guide books you will find more information about the different commands that I use in this program. For more on TI-BASIC, you should also check out <a href="http://en.wikibooks.org/wiki/Programming:TI-Basic">the TI-BASIC Programming Wikibook</a>, <a href="http://tibasicdev.wikidot.com/">TI-Basic Developer</a>, <a href="http://www.ticalc.org">ticalc.org</a> and <a href="http://education.ti.com/educationportal/appsdelivery/download/download_eula.jsp?cid=us&displayMode=G&applicationid=6142&contentpaneid=28">the TI-83 Plus SDK documentation</a> (works for TI-84 Plus as well).<br /><br />Last month we worked with probability at school. Using our TI-84 Plus calculators and the formula for <a href="http://mathworld.wolfram.com/BinomialDistribution.html">Binomial Distribution</a>, we found the probability for r successes out of n trials to occur.<br /><br />To find this you have to type the following on the calculator:<br /><br />(<u>n</u> nCr <u>r</u>)*<u>p</u>^<u>r</u>*(1-<u>p</u>)^(<u>n</u>-<u>r</u>)<br /><br />Where the letters which are <u>underlined</u> should be replaced by numbers.<br /><br />This can be somewhat time consuming, so I decided to make a simple program (written in <a href="http://en.wikipedia.org/wiki/TI-BASIC">TI-BASIC</a>) that would ask for <span style="font-weight:bold;">n, r and p</span> before it calculates the probability, thereby reducing the amount of keystrokes required to be performed by the user. Also, this program will give you the answer as a fraction (which often is an advantage when working with probability) where possible. Another advantage with my program is that it detects illegal values <span style="font-weight:bold;">before</span> the calculation has started. In other words, the purpose of this program is to save the user from having to perform unnecessarily time consuming operations. As you may understand, if the program was to ask for variables and then perform a calculation (which, depending on how fast the calculator is and how advanced the operation is, may take some time) only to break at the very end of an operation and give an error-message, it would in fact work against its purpose.<br /><br /><div class="eXanock-code"><a name="bindistr">Program code: Binomial Distribution v1.0 aka. BINDISTR</a><br /><code>:Prompt N<br />:Prompt R<br />:While R>N<br />:Disp "NO! R SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE R≤N"<br />:Prompt N<br />:Prompt R<br />:End<br />:Prompt P<br />:While not(0≤P and P≤1)<br />:Disp "NO! P SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE 0≤P≤1"<br />:Prompt P<br />:End<br />:ClrHome<br />:Disp "THE ANSWER IS:"<br />:(N nCr R)*P^R*(1-P)^(N-R)<img src="http://2.bp.blogspot.com/_A1KAE9gJdmY/RvYS1OCpn_I/AAAAAAAAAAM/GFlOHAGcnUM/s320/arrow_right.gif" border="0" alt="" />Frac<br /></code></div><br /><br />The next thing we learned about at school was finding the probability for r OR r+1 OR r+2 OR ... successes out of n trials to occur. In order to find this, you simply sum the all the probabilities for the different scenarios. This of course, is even more time consuming than the previous example, so I decided to modify the program so that it would ask for <span style="font-weight:bold;">n, lowest r, highest r and p</span> and then calculate the probability. I figured this would be a nice way to do that:<br /><br /><div class="eXanock-code">Program code: Sum of Binomial Distributions v1.0 aka. BINOMSUM<br /><code>:0→A<br />:Prompt N<br />:Disp "LOWEST R:"<br />:Prompt L<br />:While L>N<br />:Disp "NO! R SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE R≤N"<br />:Prompt N<br />:Prompt L<br />:End<br />:Disp "HIGHEST R:"<br />:Prompt H<br />:While H>N<br />:Disp "NO! H SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE H≤N"<br />:Prompt H<br />:End<br />:While L>H<br />:Disp "NO! H SHOULD BE"<br />:Disp "SO THAT L≤H"<br />:Prompt H<br />:End<br />:Prompt P<br />:While not(0≤P and P≤1)<br />:Disp "NO! P SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE 0≤P≤1"<br />:Prompt P<br />:End<br />:While L≤H<br />:((N nCr L)*P^L*(1-P)^(N-L)+A)→A)<br />:(L+1)→L<br />:End<br />:ClrHome<br />:Disp "THE ANSWER IS:"<br />:A<img src="http://2.bp.blogspot.com/_A1KAE9gJdmY/RvYS1OCpn_I/AAAAAAAAAAM/GFlOHAGcnUM/s320/arrow_right.gif" border="0" alt="" />Frac<br /></code></div><br /><br />The next day, my teacher told me that the calculator already has a method for summing a sequence of probabilities and the time taken by the calculator to calculate the probability using this algorithm proved to be faster than my method:<br /><br />sum(seq(<u>n</u> nCr X*<u>p</u>^X*(1-<u>p</u>)^(<u>n</u>-X),X,<u>l</u>,<u>h</u>))<br /><br />Where the letters which are <u>underlined</u> should be replaced by numbers.<br /><br />However, once again this involves a lot of typing for the user which means unnecessary usage of time. Therefore I decided to implement the algorithm above in my program. I think that a change in the algorithm should be considered as a major change, therefore I change the version number to 2.0<br /><br /><br /><div class="eXanock-code"><a name="binomsum-final">Program code: Sum of Binomial Distributions v2.0 aka. BINOMSUM</a><br /><code>:Prompt N<br />:Disp "LOWEST R:"<br />:Prompt L<br />:While L>N<br />:Disp "NO! R SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE R≤N"<br />:Prompt N<br />:Prompt L<br />:End<br />:Disp "HIGHEST R:"<br />:Prompt H<br />:While H>N<br />:Disp "NO! H SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE H≤N"<br />:Prompt H<br />:End<br />:While L>H<br />:Disp "NO! H SHOULD BE"<br />:Disp "SO THAT L≤H"<br />:Prompt H<br />:End<br />:Prompt P<br />:While not(0≤P and P≤1)<br />:Disp "NO! P SHOULD BE"<br />:Disp "IN THE INTERVAL"<br />:Disp "WHERE 0≤P≤1"<br />:Prompt P<br />:End<br />:sum(seq((N nCr X)*P^X*(1-P)^(N-X),X,L,H))<br /></code></div><br /><br /><span style="font-weight:bold;">Is there anything you think was unclear, do you think something is wrong in these programs? In that case, write a comment and I'll see what I can do.</span>exanockhttp://www.blogger.com/profile/07452599334816381012noreply@blogger.com1