Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Inspecting the -logdet function for convexity along lines.
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