-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSM_Interpolation.ts
242 lines (210 loc) · 7.62 KB
/
PSM_Interpolation.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as fs from 'fs';
import csv from 'csv-parser';
import yargs from 'yargs';
interface PSMData {
tooExpensive: Map<number, number>;
expensive: Map<number, number>;
cheap: Map<number, number>;
tooCheap: Map<number, number>;
}
interface CumulativePercentages {
tooExpensive: Map<number, number>;
expensive: Map<number, number>;
cheap: Map<number, number>;
tooCheap: Map<number, number>;
}
interface PSMPrices {
highestPrice: number;
compromisePrice: number;
idealPrice: number;
guaranteedQualityMinPrice: number;
}
// Configure yargs to get the CSV file path from command-line arguments
const argv = yargs
.option('csvfile', {
alias: 'f',
description: 'Path to the CSV file',
type: 'string',
demandOption: true // Make it a required flag
})
.help()
.alias('help', 'h')
.parseSync();
const results: any[] = [];
fs.createReadStream(argv.csvfile)
.pipe(csv())
.on('data', (data: any) => {
results.push(data);
})
.on('end', () => {
performPSMAnalysis(results);
});
function performPSMAnalysis(data: any[]) {
const tabulated = tabulateResponses(data);
const prices = calculatePSMPrices(tabulated);
console.log(`Highest Price: ${prices.highestPrice} yen`);
console.log(`Compromise Price: ${prices.compromisePrice} yen`);
console.log(`Ideal Price: ${prices.idealPrice} yen`);
console.log(`Guaranteed Quality Minimum Price: ${prices.guaranteedQualityMinPrice} yen`);
}
function tabulateResponses(data: any[]): PSMData {
const tabulated: PSMData = {
tooExpensive: new Map<number, number>(),
expensive: new Map<number, number>(),
cheap: new Map<number, number>(),
tooCheap: new Map<number, number>()
};
data.forEach((row) => {
incrementMap(tabulated.tooExpensive, parseInt(row['高すぎる']));
incrementMap(tabulated.expensive, parseInt(row['高い']));
incrementMap(tabulated.cheap, parseInt(row['安い']));
incrementMap(tabulated.tooCheap, parseInt(row['安すぎる']));
});
return tabulated;
}
function incrementMap(map: Map<number, number>, value: number) {
if (isNaN(value)) {
console.warn('Warning: NaN value encountered');
return;
}
if (map.has(value)) {
map.set(value, map.get(value)! + 1);
} else {
map.set(value, 1);
}
}
function calculatePSMPrices(tabulated: PSMData): PSMPrices {
const cumulativePercentages: CumulativePercentages = {
tooExpensive: getCumulativePercentages(tabulated.tooExpensive, false),
expensive: getCumulativePercentages(tabulated.expensive, false),
cheap: getCumulativePercentages(tabulated.cheap, true),
tooCheap: getCumulativePercentages(tabulated.tooCheap, true)
};
console.log('Cumulative Percentages:');
console.log('Too Expensive:', cumulativePercentages.tooExpensive);
console.log('Expensive:', cumulativePercentages.expensive);
console.log('Cheap:', cumulativePercentages.cheap);
console.log('Too Cheap:', cumulativePercentages.tooCheap);
const highestPrice = calculateIntersection(cumulativePercentages.tooExpensive, cumulativePercentages.cheap);
const idealPrice = calculateIntersection(cumulativePercentages.cheap, cumulativePercentages.expensive);
const compromisePrice = calculateIntersection(cumulativePercentages.tooExpensive, cumulativePercentages.tooCheap);
const guaranteedQualityMinPrice = calculateIntersection(cumulativePercentages.tooCheap, cumulativePercentages.expensive);
console.log(`Calculated Prices:`);
console.log(`Highest Price: ${highestPrice}`);
console.log(`Ideal Price: ${idealPrice}`);
console.log(`Compromise Price: ${compromisePrice}`);
console.log(`Guaranteed Quality Minimum Price: ${guaranteedQualityMinPrice}`);
// Generate and save the chart
const chartHtml = generateChartHtml(cumulativePercentages);
fs.writeFileSync('psm_chart_interpolated.html', chartHtml);
console.log('Chart saved as psm_chart_interpolated.html');
return {
highestPrice,
idealPrice,
compromisePrice,
guaranteedQualityMinPrice
};
}
function getCumulativePercentages(map: Map<number, number>, invert: boolean): Map<number, number> {
const cumulative = new Map<number, number>();
let cumulativeCount = 0;
const totalCount = Array.from(map.values()).reduce((a, b) => a + b, 0);
const sortedKeys = Array.from(map.keys()).sort((a, b) => a - b);
sortedKeys.forEach((key) => {
cumulativeCount += map.get(key)!;
const percentage = (cumulativeCount / totalCount) * 100;
cumulative.set(key, invert ? 100 - percentage : percentage);
});
return interpolateMissingData(cumulative, sortedKeys);
}
function interpolateMissingData(map: Map<number, number>, sortedKeys: number[]): Map<number, number> {
const interpolatedMap = new Map<number, number>();
const xs = sortedKeys;
const ys = sortedKeys.map(key => map.get(key)!);
for (let i = xs[0]; i <= xs[xs.length - 1]; i++) {
interpolatedMap.set(i, linearInterpolation(i, xs, ys));
}
return interpolatedMap;
}
function linearInterpolation(x: number, xs: number[], ys: number[]): number {
for (let i = 1; i < xs.length; i++) {
if (x < xs[i]) {
const x0 = xs[i - 1], x1 = xs[i];
const y0 = ys[i - 1], y1 = ys[i];
return y0 + ((y1 - y0) * (x - x0)) / (x1 - x0);
}
}
return ys[ys.length - 1];
}
function calculateIntersection(map1: Map<number, number>, map2: Map<number, number>): number {
let intersection = 0;
let minDiff = Number.MAX_VALUE;
map1.forEach((value1, key) => {
if (map2.has(key)) {
const value2 = map2.get(key)!;
const diff = Math.abs(value1 - value2);
if (diff < minDiff) {
minDiff = diff;
intersection = key;
}
}
});
return intersection;
}
function generateChartHtml(cumulativePercentages: CumulativePercentages): string {
const prices = Array.from(new Set<number>([
...Array.from(cumulativePercentages.tooExpensive.keys()),
...Array.from(cumulativePercentages.expensive.keys()),
...Array.from(cumulativePercentages.cheap.keys()),
...Array.from(cumulativePercentages.tooCheap.keys())
])).sort((a, b) => a - b);
const datasets = [
{ label: 'Too Expensive', data: prices.map(price => cumulativePercentages.tooExpensive.get(price) || 0), borderColor: 'blue', fill: false },
{ label: 'Expensive', data: prices.map(price => cumulativePercentages.expensive.get(price) || 0), borderColor: 'red', fill: false },
{ label: 'Cheap', data: prices.map(price => cumulativePercentages.cheap.get(price) || 0), borderColor: 'orange', fill: false },
{ label: 'Too Cheap', data: prices.map(price => cumulativePercentages.tooCheap.get(price) || 0), borderColor: 'yellow', fill: false }
];
const chartData = JSON.stringify({ labels: prices, datasets });
return `
<!DOCTYPE html>
<html>
<head>
<title>Price Sensitivity Meter</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="psmChart" width="800" height="400"></canvas>
<script>
const ctx = document.getElementById('psmChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: ${chartData},
options: {
responsive: true,
title: {
display: true,
text: 'Price Sensitivity Meter'
},
scales: {
x: {
title: {
display: true,
text: 'Price'
}
},
y: {
title: {
display: true,
text: 'Cumulative Percentage'
},
min: 0,
max: 100
}
}
}
});
</script>
</body>
</html>
`;
}