-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
174 lines (165 loc) · 3.89 KB
/
test.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// const predictDatas = ({
// datas,
// theta0,
// theta1,
// }: {
// datas: number[];
// theta0: number;
// theta1: number;
// }) => {
// return datas.map((data) => estimateData(data, theta0, theta1));
// };
// const computerDeriver = ({
// x,
// y,
// y_pred,
// length,
// }: {
// x: number[];
// y: number[];
// y_pred: number[];
// length: number;
// }) => {
// let deriver = 0;
// for (let i = 0; i < length; i++) {
// deriver += y[i] - y_pred[i];
// }
// const d_theta0 = (-2 / length) * deriver;
// const d_theta1 =
// (-2 / length) * x.reduce((prev, current) => prev + current * deriver);
// return { d_theta0, d_theta1 };
// };
// import { round } from "lodash";
// const ESTIMATE_PRICE = 1;
// const computerDerivation = ({
// kms,
// prices,
// learningRate,
// theta0,
// theta1,
// }: {
// kms: number[];
// prices: number[];
// learningRate: number;
// theta0: number;
// theta1: number;
// }) => {
// let dtheta0 = 0;
// let dtheta1 = 0;
// for (let i = 0; i < kms.length; i++) {
// dtheta0 += theta1 * kms[i] + theta0 - prices[i];
// dtheta1 += (theta1 * kms[i] + theta0 - prices[i]) * kms[i];
// }
// const tmp0 = theta0 - (dtheta0 / kms.length) * learningRate;
// const tmp1 = theta1 - (dtheta1 / kms.length) * learningRate;
// return { tmp0, tmp1 };
// };
// const estimateData = (data: number, theta0: number, theta1: number) => {
// return theta0 + theta1 * data;
// };
// //
// const computerLoss = ({
// theta0,
// theta1,
// kms,
// prices,
// }: {
// theta0: number;
// theta1: number;
// kms: number[];
// prices: number[];
// }) => {
// let loss = 0;
// for (let i = 0; i < kms.length; i++) {
// loss += (prices[i] - (theta1 * kms[i] + theta0)) ** 2;
// }
// return loss / kms.length;
// };
// //
// const boldDriver = ({
// loss,
// lastLoss,
// theta0,
// theta1,
// tmp0,
// tmp1,
// learningRate,
// length,
// }: {
// loss: number;
// lastLoss: number;
// theta0: number;
// theta1: number;
// tmp0: number;
// tmp1: number;
// learningRate: number;
// length: number;
// }) => {
// if (loss >= lastLoss) {
// return {
// theta0: theta0 + (tmp0 / length) * learningRate,
// theta1: theta0 + (tmp1 / length) * learningRate,
// // theta0: round(theta0 + (tmp0 / length) * learningRate, 5),
// // theta1: round(theta0 + (tmp1 / length) * learningRate, 5),
// learningRate: learningRate * 0.5,
// };
// } else
// return {
// theta0,
// theta1,
// learningRate: learningRate * 1.5,
// };
// };
// const computerThetas = ({
// kms,
// prices,
// learningRate,
// iteration = 100,
// }: {
// kms: number[];
// prices: number[];
// learningRate: number;
// iteration?: number;
// }) => {
// let theta0 = 0;
// let theta1 = 0;
// let theta0History: number[] = [];
// let theta1History: number[] = [];
// let lossHistory: number[] = [];
// for (let i = 0; i < iteration; i++) {
// const { tmp0, tmp1 } = computerDerivation({
// kms,
// prices,
// learningRate,
// theta0,
// theta1,
// });
// theta0 = tmp0;
// theta1 = tmp1;
// const loss = computerLoss({ theta0, theta1, kms, prices });
// if (i > 1) {
// const ajust = boldDriver({
// loss,
// lastLoss: lossHistory[i - 1],
// theta0,
// theta1,
// tmp0,
// tmp1,
// learningRate,
// length: kms.length,
// });
// theta0 = ajust.theta0;
// theta1 = ajust.theta1;
// learningRate = ajust.learningRate;
// }
// lossHistory.push(loss);
// theta0History.push(theta0);
// theta1History.push(theta1);
// console.log(theta0, theta1);
// }
// return { theta1, theta0, theta0History, theta1History, lossHistory };
// };
// export default {
// computerThetas,
// estimateData,
// };