Skip to content

Instantly share code, notes, and snippets.

@endaaman
Created November 3, 2016 09:12
Show Gist options
  • Save endaaman/af34cee4bea67af7a273dc6e09e6dbff to your computer and use it in GitHub Desktop.
Save endaaman/af34cee4bea67af7a273dc6e09e6dbff to your computer and use it in GitHub Desktop.
一次回帰直線の傾き
// 土 10/01 23/13 0 mm 0 cm 19/10
// 日 10/02 25/12 0 mm 0 cm 19/10
// 月 10/03 20/15 6 mm 0 cm 19/10
// 火 10/04 20/12 0 mm 0 cm 19/9
// 水 10/05 17/11 2 mm 0 cm 19/9
// 木 10/06 16/10 9 mm 0 cm 19/9
// 金 10/07 15/9 0 mm 0 cm 18/9
// 土 10/08 17/9 0 mm 0 cm 18/8
// 日 10/09 16/10 0 mm 0 cm 18/8
// 月 10/10 11/7 0 mm 0 cm 18/8
// 火 10/11 15/7 3 mm 0 cm 18/8
// 水 10/12 13/9 0 mm 0 cm 17/8
// 木 10/13 15/8 0 mm 0 cm 17/7
// 金 10/14 14/7 0 mm 0 cm 17/7
// 土 10/15 18/5 0 mm 0 cm 17/7
// 日 10/16 22/9 0 mm 0 cm 17/7
// 月 10/17 19/14 0 mm 0 cm 16/7
// 火 10/18 16/9 0 mm 0 cm 16/6
// 水 10/19 18/6 0 mm 0 cm 16/6
// 木 10/20 14/2 28 mm 0 cm 16/6
// 金 10/21 8/1 16 mm 1.3 cm 16/6
// 土 10/22 10/5 2 mm 0 cm 15/6
// 日 10/23 10/4 3 mm 0 cm 15/5
// 月 10/24 9/4 0 mm 0 cm 15/5
// 火 10/25 13/2 0 mm 0 cm 15/5
// 水 10/26 18/7 8 mm 0 cm 14/5
// 木 10/27 10/5 0 mm 0 cm 14/5
// 金 10/28 11/4 0 mm 0 cm 14/4
// 土 10/29 8/3 5 mm 0 cm 14/4
// 日 10/30 4/2 0 mm 0 cm 13/4
// 月 10/31 8/1 7 mm 0 cm 13/4
const _ = require('lodash')
function average(data) {
const count = data.length
return _.reduce(data, (sum, x)=> sum + x, 0) / count
}
function dispersion(data) {
const count = data.length
const ave = average(data)
return _.reduce(data, (sum, x)=> {
let abmo = x - ave // abmodality
return sum + abmo * abmo
}, 0) / count
}
function standardDeviation(data) {
const disp = dispersion(data)
return Math.sqrt(disp)
}
function averageOfDeviationProduct(a, b) {
if (a.length !== b.length) {
throw new Error('should be same length')
}
const a_average = average(a)
const b_average = average(b)
const deviationProducts = a.map((v, i)=> (v - a_average) * (b[i] - b_average))
return average(deviationProducts)
}
// 気温の配列
const temps = [23, 25, 20, 20, 17, 16, 15, 17, 16, 11, 15, 13, 15, 14, 18, 22, 19, 16, 18, 14, 8, 10, 10, 9, 13, 18, 10, 11, 8, 4, 8]
// 10/1を1、10/31を31とした配列
const x = temps.map((i, v)=> v + 1)
const sd_temp = standardDeviation(temps)
const sd_x = standardDeviation(x)
console.log(sd_temp)
console.log(sd_x)
console.log(averageOfDeviationProduct(temps, x))
console.log(averageOfDeviationProduct(temps, x) / (sd_temp * sd_x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment