-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjelm.ijs
133 lines (108 loc) · 2.02 KB
/
jelm.ijs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require'trig'
require'plot'
require'numeric'
pushup=: ] + 0.1 * |
pushdown=: ] - 0.1 * |
NB. locate the elements with values between {.x and {:x
sel=: (] >: {.@[) *. (] <: {:@[)
mean=: +/ % #
rmse=: [: %: [: mean ([: *: -)
mp=: +/ . * NB. matrix product
diag=: (<0 1)&|: : (([:(>:*i.)[:#])})
addDiag=: ([+diag@]) diag ] NB. add x to the diagonal of y
f=: 3 : '(^y) * cos 2*pi * sin pi * y'
noise=: 4 : 'y + -&x *&(+:x) ? (#y) # 0'
gendat=: 4 : 0
X=: ? y $ 0
Y=: x noise f X
minmaxX=: (<./ , >./) X
minmaxf=: (([: pushdown <./) , ([: pushup >./)) f steps 0 1 100
XT=: ? (>. 0.1 * y) $ 0
YT=: f XT
0
)
plotdatnoshow=: 3 : 0
pd 'reset'
pd 'color green'
pd 'type marker'
pd 'markersize 1'
pd 'markers circle'
pd X;Y
pd 'color red'
pd 'type line'
pd 'pensize 1'
pd (;f) steps 0 1 100
)
plotdat=: 3 : 0
plotdatnoshow 0
pd 'show'
)
plotpoly=: 3 : 0
plotdatnoshow 0
pd 'color blue'
xs=: (] #~ minmaxX"_ sel ]) /:~ X,steps 0 1 100
pval=: c&p. xs
crop=: minmaxf sel pval
pd (crop # xs);(crop # pval)
pd 'show'
)
polyreg=: 3 : 0
c=: Y %. X ^/ i.#X
YThat=: c&p. XT
plotpoly 0
)
gram=: 3 : 0
A=: X ^/ i.y
S=: (mp~ |:) A
)
leastsq=: 3 : 0
gram y
c=: ((|:A) mp Y) %. S
YThat=: c&p. XT
plotpoly 0
)
ridge=: 4 : 0
gram y
c=: ((|:A) mp Y) %. x addDiag S
YThat=: c&p. XT
plotpoly 0
)
plotelm=: 3 : 0
plotdatnoshow 0
pd 'type line'
pd 'color blue'
xs=: (] #~ minmaxX"_ sel ]) steps (<.<./X),(>.>./X),100
pd xs;(mkH ,. xs) mp c
pd 'show'
)
initelm=: 3 : 0
W=: _1 + 2 * ? (y,1) $ 0 NB. input weights
B=: ? y $ 0 NB. bias
H=: mkH ,. X
0 [ S=: (mp~ |:) H
)
mkH=: 3 : '0&>. B +"1 y mp"1/ W'
elm=: 3 : 0
c=: ((|:H) mp Y) %. y addDiag S
YThat=: (mkH ,. XT) mp c
plotelm 0
)
plottest=: 3 : 0
pd 'reset'
pd 'color green'
pd 'type marker'
pd 'markersize 1'
pd 'markers circle'
pd XT;YT
pd 'color magenta'
pd XT;YThat
pd 'color red'
pd 'type line'
pd 'pensize 1'
pd (;f) steps 0 1 100
pd 'show'
)
test=: 3 : 0
plottest 0
YT rmse YThat
)