-
Notifications
You must be signed in to change notification settings - Fork 2
/
regularisation.R
99 lines (86 loc) · 1.45 KB
/
regularisation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
library(numDeriv)
#adult=read.csv(file.choose(),header=TRUE)
h=function(theta,x){
v=c()
x= as.vector(x)
for(i in 1:length(x)) v[i]=x[i]
x=unlist(c(1,v))
n=length(x)
1/(1+exp(-sum(x*theta)))
}
ll=function(theta,y,x){
n=nrow(x)
a=0
for(i in 1:n){
a=a+y[i]*log(h(theta,x[i,]))+(1-y[i])*log(1-h(theta,x[i,]))
}
a
}
n=nrow(adult)
NFpos=c()
j=1
for(i in 1:n){
if(adult$Income.Binary[i] == 1 && adult$sex[i] == 0){
NFpos[j]=i
j=j+1
}
}
NFneg=c()
for(i in 1:n){
j = 1
if(adult[i,3] == 0 && adult$sex[i]==0){
NFneg[j]=i
j=j+1
}
}
NMpos=c()
for(i in 1:n){
j = 1
if(adult$Income.Binary[i] == 1 && adult$sex[i]==1){
NMpos[j]=i
j=j+1
}
}
NMneg=c()
for(i in 1:n){
j = 1
if(adult$Income.Binary[i] == 0 && adult$sex[i]==1){
NMneg[j]=i
j=j+1
}
}
FNRdif=function(theta,x){
n=length(NFpos)
m=length(NMpos)
# a=0
# b=0
# for(i in 1:n)
a=sum(h(theta,x[i,]))
#for(j in 1:m)
b=sum(h(theta,x[i,]))
-a/n+b/m
}
FPRdif=function(theta,x){
n=length(NFneg)
m=length(NMneg)
# a=0
# b=0
# for(i in 1:n)
a=sum(h(theta,x[i,]))
#for(j in 1:m)
b=sum(h(theta,x[i,]))
-a/n+b/m
}
Obj= function(theta){
c1=1
c2=1
c3=1
-ll(theta,adult$Income.Binary,adult[,-3]) + c1*abs(FPRdif(theta,adult[,-3])) + c2*abs(FNRdif(theta,adult[,-3])) + c3*sum(theta^2)
}
theta=0
e=0.01
for (i in 1:10) {
theta = theta - e*grad(Obj,theta)
print(theta)
}
#female=0, male =1, >=50k = 1, <50k = 0