Skip to content

Instantly share code, notes, and snippets.

@caiorss
Forked from crabmusket/negLogDet.hs
Last active August 29, 2015 14:23
Show Gist options
  • Save caiorss/29eaaea26b0c0f7f3416 to your computer and use it in GitHub Desktop.
Save caiorss/29eaaea26b0c0f7f3416 to your computer and use it in GitHub Desktop.
import Numeric.LinearAlgebra
import Control.Monad (replicateM)
import System.Random (randomIO)
import Graphics.Gnuplot.Simple (plotFunc)
-- Test 100 random lines for convexity.
main = replicateM 100 $ do
-- Two 5x5 symmetric positive definite matrices that determine the line.
a <- randomSnPD 5
v <- randomSnPD 5
disp a
disp v
let nld t = (negate . log . det) (a + scale t v)
domain = [0, 0.05 ..1]
opts = []
in plotFunc opts domain nld
randomSnPD n = fmap toSPD (randomSquare n)
-- Add transpose to make it symmetric, then add nI to ensure PD.
where toSPD m = m + trans m + scale (fromIntegral n) (ident n)
randomSquare n = fmap matFromSeed randomIO
where matFromSeed s = reshape n $ randomVector s Uniform (n * n)
disp = putStr . dispf 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment