Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Created March 1, 2018 06:39
Show Gist options
  • Save YujiSODE/f8548df74432bbf10563cc52cee07e48 to your computer and use it in GitHub Desktop.
Save YujiSODE/f8548df74432bbf10563cc52cee07e48 to your computer and use it in GitHub Desktop.
A sample of output script using regLines.tcl: random variable following the standard normal distribution
#reglinesSample1519884175_regL.tcl
#This script was generated using regLines.tcl
#data range: -4.0000 to 4.0000
#break points: -4.0000,-3.0000,-2.0000,-1.0000,0.0000,1.0000,2.0000,3.0000,4.0000
#it returns sum of given list
proc ::tcl::mathfunc::lSum {list} {
#=== lSum.tcl (Yuji SODE, 2018): https://gist.github.com/YujiSODE/1f9a4e2729212691972b196a76ba9bd0 ===
#Reference: Iri, M., and Fujino., Y. 1985. Suchi keisan no joshiki (Japanese). Kyoritsu Shuppan Co., Ltd. ISBN 978-4-320-01343-8
namespace path {::tcl::mathop};set S 0.0;set R 0.0;set T 0.0;foreach e $list {set R [+ $R [expr double($e)]];set T $S;set S [+ $S $R];set T [+ $S [expr {-$T}]];set R [+ $R [expr {-$T}]];};return $S;
};
#it returns estimated sample distribution
proc ::tcl::mathfunc::lines {x} {
set X [expr {double($x)}];
#R is data range
set R {-4.0000 -3.0000 -2.0000 -1.0000 0.0000 1.0000 2.0000 3.0000 4.0000};
#nR is data range size
set nR 9;
#l is an array of regression results: {A B R2} for y=Ax+B and coefficient of determination
set l(-4.0000) {0.0036450281425891183 0.01406816135084428 0.870381196463951};
set l(-3.0000) {0.04613621013133209 0.13669972795497187 0.9376325859081943};
set l(-2.0000) {0.18870581613508441 0.4189800469043151 0.9892857781818333};
set l(-1.0000) {0.16749230769230766 0.4252173076923077 0.9637509174396873};
set l(0.0000) {-0.1612217636022514 0.4218956097560975 0.9576772406761741};
set l(1.0000) {-0.19233433395872418 0.42435982176360226 0.9901665609229251};
set l(2.0000) {-0.04847692307692308 0.14260884615384614 0.9391292287111239};
set l(3.0000) {-0.003821602787456446 0.014717073170731705 0.8668861199090979};
if {($X<-4.0000)||($X>4.0000)} {return 0;} else {
set i 0;while {$i<($nR-1)} {
if {($X<[lindex $R $i+1])&&!($X<[lindex $R $i])} {
set e [lindex $R $i];if {!([lindex $l($e) 1]!=Inf)} {
return [expr {$X!=[string range [lindex $l($e) 0] 2 end]?0:Inf}];
} else {
set v {};
lappend v [expr {[lindex $l($e) 0]*$X}];lappend v [lindex $l($e) 1];
return [expr {lSum($v)}];
};
};
incr i 1;};
set e [lindex $R end-1];if {!([lindex $l($e) 1]!=Inf)} {
return [expr {$X!=[string range [lindex $l($e) 0] 2 end]?0:Inf}];
} else {
set v {};
lappend v [expr {[lindex $l($e) 0]*$X}];lappend v [lindex $l($e) 1];
return [expr {lSum($v)}];
};
};
};
#it returns a value of probability density function estimated from the sample distribution
proc ::tcl::mathfunc::linesPDF {x} {
return [expr {lines($x)/0.9999632926829267}];};
#it returns a random variable that follows PDF
proc ::tcl::mathfunc::linesVar {} {
set Y [expr {double(-4.0000)+double(8.0)*rand()}];
set U [expr rand()];
set v [expr {linesPDF($Y)/0.4252329168518166}];
while {$U>$v} {
set Y [expr {double(-4.0000)+double(8.0)*rand()}];
set U [expr rand()];
set v [expr {linesPDF($Y)/0.4252329168518166}];
};
return $Y;
};
@YujiSODE
Copy link
Author

YujiSODE commented Mar 1, 2018

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