#!/usr/local/bin/perl -w # example usage: # # astro2.p seed=312 b0=-3.0 sigmab=1 sigmadb=0.01 sigmaw=10 sigmadw=10 # astro2.p seed=312 b0=-3.0 sigmab=1 sigmadb=0.3 sigmaw=10 sigmadw=1 # # these two do a good job of spanning the spectrum: # astro2.p T=48 sigmaw=1 sigmadw=0.2 sigmab=0.1 sigmadb=0.1 b0=-1 # astro2.p T=48 sigmaw=1 sigmadw=1 sigmab=1 sigmadb=1 b0=-1 $pause=0; $T=1; # number of loops $Na=50; $x0 = 0.0; $dx = 1.0 ; # $dy = 3 ; $seed = 5334 ; $sigmaw = 1.0 ; # default $sigmab = 0.1 ; $sigmadw = 0.2 ; $sigmadb = 0.1 ; $b0 = -1.0 ; $file1 = "astro1.dat"; $file2 = "astro2.dat"; # $datfile = "astro2.dat" ; $comfile = "astro2.gnu" ; #$NNA = 0 ; # $xmin=-1;$xmax=1; $NN = 20 ; # $N=2*$Na; $K = 3; # program to make a load of mock data with a varying noise level. # output in formats: # # for gnuplot file.gnu # x t # # for bugs file.dat # list(x = c(-0.8,-0.4,0.0,0.4,0.8,-0.8,-0.4,0.0,0.4,0.8), # t = c(1,3,3,3,5, )) $theta = 0.31 ; # angle for random walk # read command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $co = cos($theta) ; $si = sin($theta) ; # $dx = ( $xmax - $xmin ) / 2 ; srand($seed); # initialize the random the generator &makewbdwdb(); #$xs = "list(x = c(" ; # start preparing the bugs-formatted output #$ts = "t = c(" ; $ddx = $dx * 2.0 / ($Na - 1 ) ; # x crawls across instead. open ( GNU1 , ">$file1" ) ; # open ( GNU2 , ">$file2" ) ; # open ( GNU , ">$comfile" ) ; print GNU "set yrange [-10:10]\n" ; for ($time=0;$time<$T;$time++) { $x = $x0 - $dx ; for ( $n = 1 ; $n<= $Na ; $n++ , $x += $ddx ) { # $x = $x0 +($dx)*(rand(1) +rand(1)-1.0) ; $phi[1] = 1.0 ; $phi[2] = ($x-$x0)/$dx ; foreach $h (3..$K) { $phi[$h] = ( ( 2 * $h - 3 ) * $phi[2] * $phi[$h-1] - ( $h - 2 ) * $phi[$h-2] ) / ( $h - 1 ) ; } $y1 = 0.0 ; $tau1 = 0.0 ; $y2 = 0.0 ; $tau2 = 0.0 ; foreach $h (1..$K) { $y1 += $phi[$h] * ($w[$h]-0.5*$dw[$h]) ; $tau1 += $phi[$h] * ($b[$h]-0.5*$db[$h]) ; $y2 += $phi[$h] * ($w[$h]+0.5*$dw[$h]) ; $tau2 += $phi[$h] * ($b[$h]+0.5*$db[$h]) ; } $sigmanu1 = exp(-$tau1/2.0) ; $sigmanu2 = exp(-$tau2/2.0) ; if ( $time==0 ) { $noise1[$n] = &randnormal( ) ; $noise2[$n] = &randnormal( ) ; } $t1 = $y1 + $sigmanu1 * $noise1[$n]; $t2 = $y2 + $sigmanu2 * $noise2[$n]; printf GNU1 "%f\t%f\t%f\t%f\t%f\n" , $x , $t1 , $y1 , $y1 + $sigmanu1 , $y1 - $sigmanu1; # printf GNU2 "%f\t%f\t" , $x , $t2 ; printf GNU2 "%f\t" , $y2; printf GNU2 "%f\t" , $y2 - $sigmanu2 ; printf GNU2 "%f\n" , $y2 + $sigmanu2 ; # $xs .= sprintf("%f",$x); # $ts .= sprintf("%f",$t); # if ( $n+1<=$N ) { $xs .= ",\n"; $ts .= ",\n"; } } $timep = $time + 1; $gnustring = "# gnuplot\n set nokey \n plot '$file1' index $time u 1:2 w p 3 3, '$file2' index $time u 1:2 w p 5 5, '$file1' index $time u 1:3 w l 2, '$file1' index $time u 1:4 w l 1, '$file1' index $time u 1:5 w l 1, '$file2' index $time u 1:3 w l 6, '$file2' index $time u 1:4 w l 4, '$file2' index $time u 1:5 w l 4\n pause $pause '\! astro2.p T=$timep sigmaw=$sigmaw sigmadw=$sigmadw sigmab=$sigmab sigmadb=$sigmadb b0=$b0' \n "; print GNU $gnustring ; # print $gnustring ; &bubblewbdwdb(); printf GNU1 "\n\n" ; printf GNU2 "\n\n" ; } print "# gnuplot \n load '$comfile' \n" ; close ( GNU ) ; close ( GNU1 ) ; close ( GNU2 ) ; #open ( GNU , ">$datfile" ) ; #print GNU $xs,"),\n",$ts,"))\n"; sub randnormal { $sum = 0.0 ; foreach $nn ( 1..$NN ) { $sum += ( rand(1)*3.0 - 1.5 ) ; } $sum /= sqrt($NN); return $sum ; } sub makewbdwdb { # create all the random coefficients w and b for ( $h = 1 ; $h<= $K ; $h++ ) { $w[$h] = &randnormal( ) * $sigmaw ; $b[$h] = &randnormal( ) * $sigmab ; $dw[$h] = &randnormal( ) * $sigmadw ; $db[$h] = &randnormal( ) * $sigmadb ; } $b[1] += $b0 ; } sub bubblewbdwdb { # create all the random coefficients w and b $b[1] -= $b0 ; for ( $h = 1 ; $h<= $K ; $h++ ) { $w[$h] = $co * $w[$h] + $si * &randnormal( ) * $sigmaw ; $b[$h] = $co * $b[$h] + $si *&randnormal( ) * $sigmab ; $dw[$h] = $co * $dw[$h] + $si *&randnormal( ) * $sigmadw ; $db[$h] = $co * $db[$h] + $si *&randnormal( ) * $sigmadb ; } $b[1] += $b0 ; }