generated from bennycode/ts-node-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {SMA} from '../SMA/SMA'; | ||
import {getFasterStandardDeviation, getStandardDeviation} from './getStandardDeviation'; | ||
|
||
describe('getStandardDeviation', () => { | ||
it('returns the standard deviation', () => { | ||
const prices = [9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4]; | ||
const std = getStandardDeviation(prices); | ||
expect(std.toFixed(2)).toBe('2.98'); | ||
}); | ||
|
||
it('can be used to calculate a "Window Rolling Standard Deviation / Standard Deviation Over Period"', () => { | ||
// Test data taken from: | ||
// https://github.com/TulipCharts/tulipindicators/blob/v0.8.0/tests/untest.txt#L367-L369 | ||
const prices = [81.59, 81.06, 82.87, 83.0, 83.61]; | ||
const average = SMA.getResultFromBatch(prices); | ||
const stdDev = getStandardDeviation(prices, average); | ||
expect(stdDev.toFixed(2)).toBe('0.95'); | ||
}); | ||
}); | ||
|
||
describe('getFasterStandardDeviation', () => { | ||
it('only works with the primitive data type number', () => { | ||
const prices = [9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4]; | ||
const std = getFasterStandardDeviation(prices); | ||
expect(std.toFixed(2)).toBe('2.98'); | ||
const fivePrices = [81.59, 81.06, 82.87, 83.0, 83.61]; | ||
const stdDev = getStandardDeviation(fivePrices, SMA.getResultFromBatch(fivePrices)); | ||
expect(stdDev.toFixed(2)).toBe('0.95'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {fasterGetAverage, getAverage} from './getAverage'; | ||
import Big, {BigSource} from 'big.js'; | ||
|
||
/** | ||
* Standard deviation calculates how prices for a collection of prices are spread out from the average price of these | ||
* prices. | ||
* | ||
* @see https://www.mathsisfun.com/data/standard-deviation-formulas.html | ||
* @see https://www.youtube.com/watch?v=9-8E8L_77-8 | ||
*/ | ||
export function getStandardDeviation(values: BigSource[], average?: BigSource): Big { | ||
const middle = average || getAverage(values); | ||
const squaredDifferences = values.map((value: BigSource) => new Big(value).sub(middle).pow(2)); | ||
return getAverage(squaredDifferences).sqrt(); | ||
} | ||
|
||
export function getFasterStandardDeviation(values: number[], average?: number): number { | ||
const middle = average || fasterGetAverage(values); | ||
const squaredDifferences = values.map(value => value - middle).map(value => value * value); | ||
const averageDifference = fasterGetAverage(squaredDifferences); | ||
return Math.sqrt(averageDifference); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters