Hi,
Thanks for article.
Does anyone know how to do this in mySQL? - I have some data in a mySQL DB and would love to view it.
I was able to convert the function but the rest exceeds my limited ability
DELIMITER $$
CREATE FUNCTION `fnGetCorrelation`(TickerA VARCHAR(10),
TickerB VARCHAR(10),
DateStart DATE,
DateEnd DATE
) RETURNS decimal(22,6)
BEGIN
DECLARE MeanA, MeanB, Corr, Return_Corr NUMERIC(22,6);
-- Get Mean Value for Ticker A
SELECT AVG(Price) INTO MeanA FROM Prices
WHERE ticker = TickerA
AND Date >= DateStart
AND Date <= IFNULL(DateEnd, CURDATE())
GROUP BY ticker;
-- Get Mean Value for Ticker B
SELECT AVG(Price) INTO MeanB FROM Prices
WHERE ticker = TickerB
AND Date >= DateStart
AND Date <= IFNULL(DateEnd, CURDATE())
GROUP BY ticker;
-- Calculate Correlation Coefficient
SELECT
(AVG((prcA.Price-MeanA)*(prcB.Price-MeanB)))/(STDDEV_POP(prcA.Price)*STDDEV_POP(prcB.Price)) into CORR
FROM Prices prcA JOIN Prices prcB
ON prcA.Date = prcB.Date
WHERE prcA.ticker = TickerA
AND prcB.TICKER = TickerB
AND prcA.Date >= DateStart
AND prcA.Date <= DateEnd;
-- SET Return_Corr = Corr;
RETURN(Corr);
END
Can anybody help?