#C9.txt Feb. 21, 2019 Help:=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: ArDataMV:=proc(A::posint,v::list,N::posint) local i, n, retL, x, c2, rd: with(ListTools): n:=nops(v): rd:=rand(-A+10^(-12)..A-10^(-12)): retL:=[]: for i from 1 to N do x:=[seq(rd(), i=1..n)]: if DotProduct(v, x)<0 then c2:=0: else c2:=1: fi: retL:=[op(retL),[x,c2]]: od: retL; end: PAmv:=proc(L::list,n::posint) local i,c,cc,j: with(ListTools): c:=[seq(0, i=1..n)]: for i from 1 to n do cc:=DotProduct(L[i][1],c): if (cc < 0) and (L[i][2] = 1) then for j from 1 to n do c[j]:=c[j]+L[i][1][j]: od: elif (cc >= 0) and (L[i][2] = 0) then for j from 1 to n do c[j]:=c[j]-L[i][1][j]: od: fi: od: c: end: TestPMV:=proc(L::list,v::list) local i,j,n,nL,fp,fn: with(ListTools): n:=nops(v): nL:=nops(L): fp:=0: fn:=0: for i from 1 to nL do: if (DotProduct(L[i][1],v)<0) and (L[i][2]=1) then fn:=fn+1: elif (DotProduct(L[i][1],v)>=0) and (L[i][2]=0) then fp:=fp+1: fi: od: [fp,fn,nops(L)]: end: PA1Corrected:=proc(L::list) local i, retL, n: n:=nops(L): retL:=[]: for i from 1 to n do retL:=[op(retL),[[L[i][1],1],L[i][2]]]: od: PAmv(retL,2): end: MethodCompare:=proc(L::list): printf("PA1 time: %f\n", time(PA1(L))): printf("PA1correctedtime: %f\n", time(PA1Corrected(L))): end: