Created
January 3, 2018 09:05
-
-
Save Tosainu/8df05092c75a53711efbda52ecec4c64 to your computer and use it in GitHub Desktop.
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
module Main where | |
import Control.Monad (replicateM) | |
import Graphics.Rendering.Chart.Backend.Cairo | |
import Graphics.Rendering.Chart.Easy | |
import Graphics.Rendering.Chart.Gtk | |
import Numeric.LinearAlgebra | |
import System.Random | |
instance (Random x, Random y) => Random (x, y) where | |
randomR ((x1, y1), (x2, y2)) gen1 = | |
let (x, gen2) = randomR (x1, x2) gen1 | |
(y, gen3) = randomR (y1, y2) gen2 | |
in ((x, y), gen3) | |
random = undefined | |
main :: IO () | |
main = do | |
c1 <- replicateM 50 $ | |
getStdRandom $ randomR ((-3.75, 2.25), (-2.25, 3.75)) :: IO [(Double, Double)] | |
c2 <- replicateM 50 $ | |
getStdRandom $ randomR ((-1.75, 1.25), (-0.25, 2.75)) :: IO [(Double, Double)] | |
let xs = c1 ++ c2 | |
n = length xs | |
mx = (n><3) $ concatMap (\(x1, x2) -> [1.0, x1, x2]) xs | |
mt = vector $ replicate (length c1) 1.0 | |
++ replicate (length c2) (-1.0) | |
mw = mx <\> mt -- = pinv mx #> mt | |
x1hat = [-5.0, 5.0] | |
x2hat = map (\xi -> (- (mw ! 0) - (mw ! 1) * xi) / (mw ! 2)) x1hat | |
yhat = zip x1hat x2hat | |
putStrLn $ "w~ = " ++ show mw | |
let fo = def { _fo_size = (480, 480) | |
, _fo_format = SVG | |
} | |
toFile fo "ls1.svg" $ do | |
layout_x_axis . laxis_generate .= scaledAxis def (-4, 4) | |
layout_x_axis . laxis_title .= "x1" | |
layout_y_axis . laxis_generate .= scaledAxis def (-4, 4) | |
layout_y_axis . laxis_title .= "x2" | |
setShapes [PointShapeCircle, PointShapeCircle] | |
plot (points "C1" c1) | |
plot (points "C2" c2) | |
plot (line "y" [yhat]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment