#OK to post homework
#Quentin Dubroff, 2-24-19, Assignment 9
with(linalg):
#ArDataMV(A,v,N) A procedure that outputs a list of random data from the n-dimensinal cube [-A,A]^n
ArDataMV:=proc(A,v,N) local n,i,x,t,L,ra:
n:=nops(v):
ra:=rand(-A..A):
L:=[]:
for i from 1 to N do
t:=1:
x:=[seq(ra(),j=1..n)]:
if dotprod(x,v)<0 then
t:=0:
fi:
L:=[op(L),[x,t]]:
od:
L:
end:
#PAmv(L,n) inputs a list containing data of the form [VectorOfLengthn,ZeroOrOne] where VectorOfLengthn is a list of length n,
#and n is the dimension and implements the perceptron algorithm
PAmv:=proc(L,n) local i,j,c,m,v:
v:=[0$n]:
m:=15*nops(L):
for j from 1 to m do
i:=1+(j mod nops(L)):
if L[i][2]=1 and dotprod(L[i][1],v)<0 then
#False negative then
v:=v+L[i][1]:
elif L[i][2]=0 and dotprod(L[i][1],v)>=0 then
#False positive then
v:=v-L[i][1]:
fi:
od:
v:
end:
#TestPMV(L,v) generalizes our perceptron test to multiple dimensions. It inputs a list of classified data L and a vector v with n components
#It outputs the tripe false negatives, false positives, length of L
TestPMV:=proc(L,v) local fn,fp,i:
fn:=0:
fp:=0:
for i from 1 to nops(L) do
if dotprod(L[i][1],v)>=0 and L[i][2]=0 then
fp:=fp+1:
elif dotprod(L[i][1],v)<0 and L[i][2]=1 then
fn:=fn+1:
fi:
od:
[fn,fp,nops(L)]:
end:
#PA1corrected(L) First transfoms the list L to the format [[data,1],ZeroOrOne], and then uses procedure PAmv(L,n) with n=2 to output
#two numbes a and b such that L[i][2]=1 iff ax+b>=0
PA1corrected:=proc(L) local J,i:
J:=[]:
for i from 1 to nops(L) do
J:=[op(J),[[L[i][1],1],L[i][2]]]:
od:
[J,PAmv(J,2)]:
end:
#Summary of tests: It seems that the new perceptron algorithm PA1corrected has a lot more variance. Sometimes, it found a perfect separator
#but sometimes it produced a large fraction of either false negatives exclusive or false positives. The old PA1 worked nicely for large n.
#raising the number of times we iterate through the list seems to alleviate this problem. It could be related to the fact that our corrections
#are very sensitive in the first variable but not so much for the second variable.
### THINGS FROM C9.txt
#1D perceptron algorithm,
#PA1(L): inputs a list of pairs [heightIncm,{0,1}] where 0 if she or he did not make it 1 if he or she did
#outputs a numnber c a "predictor" that if x>c then they get to be admitted
PA1:=proc(L) local i,c,n:
c:=0:
n:=nops(L):
for i from 1 to n do
if L[i][2]=1 and L[i][1]c then
#False negative then
c:=c+L[i][1]:
fi:
od:
c:
end:
#inputs L1 and H1 (low and high heights in basketball players and the number N of applicants
#and a cut-off c that >=c gets 1 (admitted) and 0 (rejected)
ArData:=proc(L1,H1,N,c) local L,ra,i,t:
ra:=rand(L1..H1):
L:=[]:
for i from 1 to N do
t:=ra():
if t>=c then
L:=[op(L),[t,1]]:
else
L:=[op(L),[t,0]]:
fi:
od:
L:
end:
#Inputs a data set of pairs [height,1 or 0] and a cut-off c, outputs the
#triple # of false negatives and # of false positive, total number of data entries
TestP:=proc(L,c) local fn,fp,i:
fn:=0:
fp:=0:
for i from 1 to nops(L) do
if L[i][1]>=c and L[i][2]=0 then
fp:=fp+1:
elif L[i][1]