#OK to post homework #Jason Saied, Feb. 23, 2019, Assignment 9 Help:=proc(): print(`ArDataMV(A,v,N), Dot(v,w), PAmv(L,n), TestPMV(L,v),ListMod(a,b) PA1corrected(L)`): end: #ArDataMV(A,v,N): #A is a positive number #v is a list (a vector) of numbers #N is the number of data points you want #output a list of N pairs [vector, 0 or 1] where we have 0 if the vector dots nonnegatively with v and 1 if it dots negatively with v ArDataMV:=proc(A,v,N) local n, i, j, p, w, vw, L, ra: n:=nops(v): #check types if not (A>0 and N>0 and type(N,integer) and type(v,list) and {seq(type(v[i],numeric),i=1..n)}={true}) then return FAIL: fi: L:=[]: ra:=rand(-convert(A,float)..convert(A,float)): for p from 1 to N do: w:=[]: #create random vector in [-A,A]^n for j from 1 to n do w:=[op(w), ra()]: od: #check dot product of v and w vw:=Dot(v,w): if vw>=0 then L:=[op(L),[w,1]]: else L:=[op(L),[w,0]]: fi: od: end: #PAmv(L,n) #input a list L containing data of the form [vector of length n, 0 or 1] #returns a vector v defining a hyperplane as given by perceptron PAmv:=proc(L,n) local v, k, p, q, i, j, jj, w, actual, guess, first: #check input #n is a positive integer #L is a list #for each entry of L, it is a list #... its first component is a list #... its first component has length n #... its first component consists of integers #... its second component is 0 or 1 if not (type(n,integer) and n>0 and type(L,list) and {seq(type(L[k], list) and nops(L[k])=2 and type(L[k][1], list) and nops(L[k][1])=n and {L[k][2]} subset {0,1} and {seq(type(L[k][1][p],numeric),p=1..n)}={true}, k=1..nops(L))}={true}) then return FAIL: fi: #start with v=0 v:=[seq(0, i=1..n)]: #for each data point, check and possibly update your hypothesis #we will run through the data many times to get a more accurate guess, as in the text #To do this better, I should calculate M/m^2 as in the text and run that many times. If I have time I will do that. for j from 1 to 100*nops(L) do jj:=ListMod(j,nops(L)): #w is the vector, actual is how the vector was classified w:=L[jj][1]: actual:=L[jj][2]: #calculate guess if Dot(v,w) >=0 then guess:=1: else guess:=0: fi: #interpret actual vs guess and modify v if actual<>guess then if actual = 1 then v:=v+w: else v:=v-w: fi: fi: od: #normalize v for later comparision (since scalars don't matter) first:=v[1]: for q from 1 to n do v:=subsop(q=(v[q]/first), v): od: return v: end: #TestPMV(L,v): #assuming that L is a set of data of the usual form and v is the output of perceptron, #calculates the number of false negatives given by perceptron, false positives given by perceptron, and the number of data points TestPMV:=proc(L,v) local fn, fp, i, dotted, value: fn:=0: fp:=0: for i from 1 to nops(L) do value:=L[i][2]: dotted:=Dot(L[i][1],v): if value=1 and dotted<0 then fn:=fn+1: elif value=0 and dotted>=0 then fp:=fp+1: fi: od: [fn,fp,nops(L)]: end: #PA1corrected(L): #input a list containing lists of the form [number, 0 or 1] #run perceptron on that list #the output already has the desired form PA1corrected:=proc(L) local i, newList: newList:=[]: #go through list #for each entry, replace the first component with [self,1] for i from 1 to nops(L) do #L[i][1]:=[L[i][1],1]: newList:=[op(newList), [[L[i][1],1],L[i][2]]]: od: print(newList): return PAmv(newList,2): end: ###Helper methods #Dot(v,w): #input vectors v and w of the same size and outputs the dot product #assumes good input because I am only using it in methods where the input has been checked Dot:=proc(v,w) local i: return add(v[i]*w[i], i=1..nops(v)): end: #ListMod(a,b): #calculates a mod b #but if the result is 0, return b instead of 0 ListMod:=proc(a,b) local c: c:=modp(a,b): if c>0 then return c: else return b: fi: end: ############from C9.txt #C9.txt Feb. 21, 2019 Help9:=proc() print(`PA1(L) , ArData(L1,H1,N,c), TestP(L,c) , NoisyData(L1,H1,N,c,p) `): end: #L:=[[TadeusBalaban, #Publ , #Citations, #h-index ,1], # [RobertBeals, 2], #.... # [DoronZeilberger, 210,2404, , 2], # [WujunZhang, ]] #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]=c gets 1 (admitted) and 0 (rejected) and a prob. p of mis-classifying #outputs the noisy data set NoisyData:=proc(L1,H1,N,c,p) local L,ra,i,t,ra1: ra:=rand(L1..H1): ra1:=rand(0.0..1.0): L:=[]: for i from 1 to N do t:=ra(): if t>=c then if ra1()<=p then L:=[op(L),[t,0]]: else L:=[op(L),[t,1]]: fi: else if ra1()<=p then L:=[op(L),[t,1]]: else L:=[op(L),[t,0]]: fi: fi: od: L: end: