Sunday, January 30, 2011

Correlation analysis

# correlation analysis tool
n=7
decimal(3)
font "arial",10,100
graphsize 500,500
dim x(n) : dim y(n)
tx=0 : ty=0
x = {2,3,3,4,4,6,5}
y = {1,2,4,5,4,8,7}
for m = 0 to n-1
tx=tx+x[m] : ty=ty+y[m]
next m
meanx=tx/n : meany=ty/n
sdx=0 : sdy=0: sdxy=0
for m = 0 to n-1
sdx=sdx +(x[m]-meanx)^2
sdy=sdy +(y[m]-meany)^2
sdxy=sdxy +(x[m]-meanx)*(y[m]-meany)
next m
sx=(sdx/n)^.5
sy=(sdy/n)^.5
covxy=sdxy/n
r= covxy/(sx*sy)
m= covxy/sx^2
c= meany-m*meanx
# Standard deviation covariance correlation coefficient and line of best fit
Print "Sx="+sx
Print "Sy="+sy
Print "Sxy="+covxy
print "r="+r
print "y="+m+"x+"+c
# graph parameters
minx=-10 : maxx=10 : miny=-10 : maxy=40
rangex=(maxx-minx)
rangey=(maxy-miny)
for i= 0 to 10
line 0,i*50,500,i*50
line i*50,0,i*50,500
text i*50,480,minx+rangex*i/10
text 0,500-i*50,miny+rangey*i/10
next i
color red
for i= 0 to n-1
circle (x[i]-minx)*(500/rangex),500-(y[i]-miny)*(500/rangey),5
next i
color blue
for i= 0 to 500
circle i,500*(1+(-c+miny)/rangey)-m*(rangex/rangey)*(i+500*minx/rangex) ,2
next i

Tuesday, January 25, 2011

Character frequency

dim a(123)
for n = 97 to 122
a[n]= count("The frequency of letters in text has often been studied for use in cryptography, and frequency analysis in particular. No exact letter frequency distribution underlies a given language, since all writers write slightly differently. Linotype machines sorted the letters' frequencies as etaoin shrdlu cmfwyp vbgkqj xz based on the experience and custom of manual compositors. Likewise, Modern International Morse code encodes the most frequent letters with the shortest symbols; arranging the Morse alphabet into groups of letters that require equal amounts of time to transmit, and then sorting these groups in increasing order, yields e it san hurdm wgvlfbk opjxcz yq. Similar ideas are used in modern data-compression techniques such as Huffman coding.", chr(n),true)
next n
for n = 97 to 122
print chr(n)+" "+a[n]
next n

Pie chart

rem favourite pet pie chart
font "arial",20,100
graphsize 600,300
n=4
total=0
dim freq(n)
dim cumfreq(n+1)
dim category\$(n)
freq = {23,13,62,45}
category\$ = {"Dog","Fish","Cat","Rabbit"}
dim arm(6)
arm = {0,0,1,150,-1,150}
for p = 0 to n-1
total = total + freq[p]
cumfreq[p+1]=total
next p
for i = 0 to 2*pi step .01
for p = 0 to n-1
if cumfreq[p]/total < i/(2*pi) then
color cumfreq[p]*500000
rect 400,p*30+20,20,20
text 440,p*30+15,category\$[p]
end if
next p
stamp 150,150,1,i,arm
next i

Thursday, January 13, 2011

Times table tutor

graphsize 600,600
fastgraphics
font "arial",20,100
For question = 1 to 10
for x = 1 to 10
for y = 0 to 9
color blue
rect x*45,y*45,40,40
color white
text x*45,y*45,x+y*10
next y
next x
a=int (rand*10+1)
b=int (rand*10+1)
print a+"x"+b
refresh
say "Question"+question
say a+" ,times, "+b+","
while clickb = 0
pause .01
endwhile
clickclear
say "Yes, well done"
else
say "No, it is"+a*b
end if
next question

Monday, January 10, 2011

Line pattern

Graphsize 600,600
fastgraphics
clg
for pic = 1 to 30
a=122 : b=rand^2 : c=rand^2: d=rand^2
for x= 1 to 600
for y= 1 to 600
color a+a*sin(b*x),a+a*sin(c*x),a+a*sin(d*x)
plot x,y
next y
refresh
next x
next pic

Saturday, January 1, 2011

Color plots

Graphsize 600,600
fastgraphics
clg
for pic = 1 to 2
for x= 1 to 600
for y= 1 to 600
# try different formulae for color using coordenates x and y
if pic = 1 then color ((x-300)^2+(y-300)^2)*3
if pic = 2 then color 300*abs((x-300)/(y-300))
plot x,y
next y
refresh
next x
next pic