Random Walk Equation Slot Calculator Example

Difference between version 57 and 58 - Previous - Next
***  Random Walk Equation  Slot Calculator Example ***
----
This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER  in your comment with the same courtesy that I will give you. Its very hard to reply intelligibly without some background of the correspondent. Thanks,[gold]
----

<<TOC>>
   
----**Introduction**
[gold] Here is an eTCL  script on 
to estimate the Random Walk equation . 
For the push buttons, the recommended
procedure is push testcase and fill frame,
change  entries , push solve, and
then push report. Report allows 
copy and paste from console.
The calculator for the random walk does
not return a consistent fixed value 
from fixed constants, but
does return a random value within the
limits of its assumptions. Hence, the
random walk calculator can be used to
test expected gains/losses over an
average of years or trials over time.
Essentially, in three years one can lose
one's shirt, in 12 years one should either
break even or gain or lose another shirt.
----  
======
    pseudocode:Random walk equation
    year rate =      good year rate * term chance(1 in n)
                plus normal year rate * term chance(1 in n)
                plus bad year rate * term chance(1 in n)
                plus place holder to keep from all zeroed terms
    pseudocode: set chance terms initially to zero until activated.
    pseudocode: if      rand() <  $side2 , set term1 1  
    pseudocode: if no chance terms fire, year of zero interest will result.
    pseudocode: if  chance<(1 in n) terms fire, year of calc. interest will result.
    pseudocode: not all terms will fire on every period of time.
    pseudocode: good/norm/bad years loaded as $side1,$side2,$side3 from form.
    current = .01*$side1*$term1+.01*$side3*$term2+.01*$side5*$term3+.00000000001 } ]  
    pseudocode: add $current sum of terms for 1 to N years in for loop
    pseudocode:  and divide by N years from $side7 in form  
    pseudocode:  load results into   answer fields 
======
----
In planning any software, there is 
a need to develop testcases.  
 
 Testcase 1. 
   
%|quantity|number|units|%
&| interest rate for good year | 10.|percent|&
&| 1 in chance:   | .333|none|&
&| interest rate for normal year | 5 |percent|&
&|  1 in chance: |.333|none|&
&|  interest rate for bad year| -10|percent|&
&|  1 in chance  |.333|none|&
&|  1 to N years: | 1 ||&
&| answer is random from -.1 to +.15||percent|&


----
***Screenshots Section***
[http://farm5.static.flickr.com/4120/4951464055_cebe139fdb.jpg] 
***References:***
       
   * Random Walk Down Wall Street,by Burton G. Malkiel
     
---- 
****Appendix Code****
****appendix TCL programs and scripts ****


*** Pretty Print VERSION   *** 


----
======  
    # pretty print from autoindentation and ased editor
    # random walk equation
    # written on Windowws XP on eTCL
    # working under TCL version 8.5.6 and eTCL 1.0.1
    # gold on TCL WIKI , 24aug2010
    package require Tk
    frame .frame -relief flat -bg aquamarine4
    pack .frame -side top -fill y -anchor center
    foreach {i name} { 1 {good year rate:}   2 {1 in chance:}     3 {normal year rate}  
                       4 {1 in chance:}      5 {bad year rate:}   6 {1 in chance}
                       7 {1 to N years:}     8 { answer:} 
                     } {
        label .frame.label$i -text $name -anchor e
        entry .frame.entry$i -width 35 -textvariable side$i
        grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1
        }
    proc about {} {
        set msg "Calculator for Random Walk Equation.
        from TCL WIKI,
        written on eTCL "
        tk_messageBox -title "About" -message $msg
    }   
    proc intelligent5 { xx1   } {
        global side1 side2 side3
        global side4 side5 side6 side7 side8
        set term1 0
        set term2 0
        set term3 0
        set current 0
        set chance1 [expr {1./$side2 }]
        set totyears [expr {int($side7) }]
        for {set i 0;} {$i<$totyears} {incr i} {
            if { [ expr { rand() } ] <= $side2 } {set term1 1 }
            if { [ expr { rand() } ] <= $side4 } {set term2 1 }
            if { [ expr { rand() } ] <= $side6 } {set term3 1 }
            set current  [ expr { ($current*1.) +.01*$side1*$term1+.01*$side3*$term2+.01*$side5*$term3+.00000000001 } ]
        }
        set side7 $totyears
        set side8  [expr {($current*1.)/$totyears }]
        if { [ expr { abs($side8) } ] <= .0001 } {set side8 0 }
        return $side8
    }
    proc calculate {     } {
        global colorwarning
        global colorback
        global answer2   answer3
        global side1 side2 side3 side4 side5 side6 side7 side8
        set answer2 5
        set answer2  [ intelligent5  $side8     ]
        set side8 $answer2
    }
    proc fillup {aa bb cc dd ee ff gg hh} {
        .frame.entry1 insert 0 "$aa"
        .frame.entry2 insert 0 "$bb"
        .frame.entry3 insert 0 "$cc"
        .frame.entry4 insert 0 "$dd"
        .frame.entry5 insert 0 "$ee"
        .frame.entry6 insert 0 "$ff "
        .frame.entry7 insert 0 "$gg "
        .frame.entry8 insert 0 "$hh "
    }
    proc clearx {} {
        foreach i {1 2 3 4 5 6 7 8} {
            .frame.entry$i delete 0 end
        }
    }
    proc reportx {} {
        global side1 side2 side3 side4 side5 side6 side7 side8
        console show;
        puts "
        The interpolation function takes
        two know points on a line and
        solves for an intermediate point.
        The points are xx1,yy1   xx2,yy2 and xx3,?yy3?
        The input order of the five items
        is xx1 yy1 xx2 yy2 xx3
        and solving for ?yy3?.
        The interpolation function loaded as
        proc pol. User should be able to write
        pol 50. 1000. 200. 1200.  150.
        and save answer (1133.3)  on console."
        puts " $side1     "
        puts " $side2    "
        puts " $side3   "
        puts " $side4     "
        puts " $side5     "
        puts " $side6     "
        puts " $side7    "
        puts " $side1     "
        puts " $side2    "
        puts " $side3    "
        puts " $side4    "
        puts " $side5    "
        puts " $side6    "
        puts " $side7    "
        puts " $side8    "
        puts "answer   $side8 "
    }
    frame .buttons -bg aquamarine4
    ::ttk::button .calculator -text "Solve" -command { calculate   }
    ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 10. .333 5. .333  -10. .333  1. 1.}
    ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 10. .333 5. .333  -10. .333  7.7 1.  }
    ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup  10. .333 5. .333  -10. .333  8.5 1.  }
    ::ttk::button .clearallx -text clear -command {clearx  }
    ::ttk::button .about -text about -command about
    ::ttk::button .cons -text report -command { reportx }
    ::ttk::button .exit -text exit -command {exit}
    pack .calculator  -in .buttons -side top -padx 10 -pady 5
    pack  .clearallx .cons .about .exit .test4 .test3  .test2   -side bottom -in .buttons
    grid .frame .buttons -sticky ns -pady {0 10}
    . configure -background aquamarine4 -highlightcolor brown -relief raised -border 30
    wm title . "Random Walk Equation Calculator "

======
----
[gold] This page is copyrighted under the TCL/TK license terms, [http://tcl.tk/software/tcltk/license.html%|%this license]. 

**Comments Section**
Please place any comments here, Thanks.
----
Q: What is your purpose in binding the motion event on your main window to
execute the wm title command.  I.e., this line:
======
    bind . <Motion> {wm title . "Random Walk Equation Calculator "}
======
The result of that binding is that every time you move the mouse, the "wm   
title" subcommand is called repeatedly.  To set the window title, you just  
need to call "wm title . title" once, not on every event update upon mouse  
pointer motion.
----
Why do you define the procs ''interlinear'', ''pol'', ''errorx'', and ''height5'' when you do not appear to use them anywhere in the code presented?  In examples, having extra bits defined that are not used adds noise that a reader has to expend effort upon only to later learn that he/she could have ignored that part.  Keeping the example focused upon only that which is needed for just the example, and nothing more, makes for a more informative, and educational, example.
----
Note, the Tcl foreach command will iterate across plural lists in parallel.  As a result, your set names ... foreach i loop could be written this way:
======
    foreach i    { 1 2 3 4 5 6 7 8 } \
            name { {good year rate:} {1 in chance:} {normal year rate} {1 in chance:} 
                   {bad year rate:}  {1 in chance}  {1 to N years:}    { answer:} } {
        label .frame.label$i -text $name -anchor e
        entry .frame.entry$i -width 35 -textvariable side$i
        grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1
    }
======
It will also iterate across a single list taking more than one element at a time.  Your loop could also be written this way (with some whitespace sugar to make the relationship more apparent to a human reader):
======
    foreach {i name} { 1 {good year rate:}   2 {1 in chance:}     3 {normal year rate}  
                       4 {1 in chance:}      5 {bad year rate:}   6 {1 in chance}
                       7 {1 to N years:}     8 { answer:} 
                     } {
        label .frame.label$i -text $name -anchor e
        entry .frame.entry$i -width 35 -textvariable side$i
        grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1
    }
======
----
[gold] changes: deleted procs ''interlinear'', ''pol'', ''errorx'', ''height5'' and "pi" ,
replaced with wm title . title. Replaced foreach statement.
----

<<discussion>>
  
----

<<categories>> Numerical Analysis | Toys | Calculator | Mathematics| Example| Toys and Games | Games | Application | GUI
----
<<categories>> Development | Concept| Algorithm | Statistics