-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinRegSingle.hs
132 lines (100 loc) · 3.53 KB
/
LinRegSingle.hs
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
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Main where
import qualified Data.Vector.Unboxed as V
import Data.Random.Source.PureMT
import Data.Random
import Control.Monad.State
import Data.Colour
import Control.Lens hiding ( (#) )
import Graphics.Rendering.Chart hiding ( translate )
import Diagrams.Backend.Cairo.CmdLine
import Diagrams.Backend.CmdLine
import Diagrams.Prelude hiding ( sample, render )
import Data.Default.Class
import Graphics.Rendering.Chart.Backend.Diagrams
import System.IO.Unsafe
nSamples :: Int
nSamples = 100
betaHyperPrior :: Double
betaHyperPrior = 2.0
nuHyperPrior :: Double
nuHyperPrior = 1.0
vHyperPrior :: Double
vHyperPrior = 1.0
vHyperPriorInv :: Double
vHyperPriorInv = recip vHyperPrior
s2HyperPrior :: Double
s2HyperPrior = 1.0
testXs :: Int -> V.Vector Double
testXs m =
V.fromList $
evalState (replicateM m (sample StdUniform))
(pureMT 2)
testXs' :: Int -> V.Vector Double
testXs' m =
V.fromList $
evalState (replicateM m (sample StdUniform))
(pureMT 3)
testEpsilons :: Int -> V.Vector Double
testEpsilons m =
V.fromList $
evalState (replicateM m (sample StdNormal))
(pureMT 2)
testEpsilons' :: Int -> V.Vector Double
testEpsilons' m =
V.fromList $
evalState (replicateM m (sample StdNormal))
(pureMT 3)
testYs :: Int -> V.Vector Double
testYs m = V.zipWith (\x e -> betaHyperPrior * x + e)
(testXs m) (testEpsilons m)
testYs' :: Int -> V.Vector Double
testYs' m = V.zipWith (\x e -> betaHyperPrior * x + e)
(testXs' m) (testEpsilons' m)
posterior :: Int -> (Double, Double)
posterior m = (nuPosterior, vPosteriorInv)
where
xs = testXs m
xsSquare = V.sum $ V.zipWith (*) xs xs
nuPosterior = nuHyperPrior + fromIntegral m
vPosteriorInv = vHyperPriorInv + xsSquare
prices :: [(Double,Double)]
prices = V.toList $ V.zip (testXs nSamples) (testYs nSamples)
prices' :: [(Double,Double)]
prices' = V.toList $ V.zip (testXs' nSamples) (testYs' nSamples)
chart :: Colour Double -> [(Double, Double)] -> Graphics.Rendering.Chart.Renderable ()
chart c prices = toRenderable layout
where
price1 = plot_points_style . point_color .~ opaque c
$ plot_points_values .~ prices
$ plot_points_title .~ "price 1"
$ def
layout = layoutlr_title .~"Price History"
$ layoutlr_left_axis . laxis_override .~ axisGridHide
$ layoutlr_right_axis . laxis_override .~ axisGridHide
$ layoutlr_x_axis . laxis_override .~ axisGridHide
$ layoutlr_plots .~ [Left (toPlot price1),
Right (toPlot price1)]
$ layoutlr_grid_last .~ False
$ def
displayHeader :: FilePath -> Diagram B R2 -> IO ()
displayHeader fn =
mainRender ( DiagramOpts (Just 900) (Just 600) fn
, DiagramLoopOpts False Nothing 0
)
denv :: DEnv
denv = unsafePerformIO $ defaultEnv vectorAlignmentFns 500 500
diag :: Colour Double -> [(Double, Double)] -> QDiagram Cairo R2 Any
diag c prices = fst $ runBackend denv (render (chart c prices) (500, 500))
main :: IO ()
main = do
displayHeader "TestInteractive.png"
((diag red prices # scaleX 0.4 # scaleY 0.4 # translate (r2 (0.1, 0.0)))
<> (diag blue prices' # scaleX 0.6 # scaleY 0.6 # translate (r2 (0.2, 0.0))))
putStrLn "Hello"