Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Created March 9, 2018 14:32
Show Gist options
  • Save YujiSODE/fd0b6164461cbd5df862c11cb38f2fcf to your computer and use it in GitHub Desktop.
Save YujiSODE/fd0b6164461cbd5df862c11cb38f2fcf to your computer and use it in GitHub Desktop.
A sample of output script using regLines.tcl: random variable following bimodal distribution with interval of 0.3
#reglinesSample1520602996_regL.tcl
#This script was generated using regLines.tcl
#data range: 0.0000 to 3.2000
#break points: 0.0000,0.3000,0.6000,0.9000,1.2000,1.5000,1.8000,2.1000,2.4000,2.7000,3.0000,3.2000
#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 {0.0000 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3.0000 3.2000};
#nR is data range size
set nR 12;
#l is an array of regression results: {A B R2} for y=Ax+B and coefficient of determination
set l(0.0000) {30.0 1.6666666666666665 0.9642857142857142};
set l(0.3000) {19.999999999999993 4.3333333333333375 0.32432432432432406};
set l(0.6000) {49.999999999999986 -11.66666666666666 0.9868421052631585};
set l(0.9000) {-64.99999999999997 80.99999999999997 0.9825581395348827};
set l(1.2000) {-40.000000000000014 56.00000000000002 1.0};
set l(1.5000) {0 0 0};
set l(1.8000) {0 0 0};
set l(2.1000) {0 0 0};
set l(2.4000) {19.999999999999982 -46.99999999999995 1.0};
set l(2.7000) {-5.000000000000024 22.333333333333403 0.1071428571428579};
set l(3.0000) {-49.99999999999996 163.66666666666654 0.986842105263158};
if {($X<0.0000)||($X>3.2000)} {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)/23.383333333333326}];};
#it returns a random variable that follows PDF
proc ::tcl::mathfunc::linesVar {} {
set Y [expr {double(0.0000)+double(3.2)*rand()}];
set U [expr rand()];
set v [expr {linesPDF($Y)/1.4255167498218106}];
while {$U>$v} {
set Y [expr {double(0.0000)+double(3.2)*rand()}];
set U [expr rand()];
set v [expr {linesPDF($Y)/1.4255167498218106}];
};
return $Y;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment