Skip to content

Instantly share code, notes, and snippets.

@multidis
multidis / julia_short_if.jl
Last active August 29, 2015 14:07
Short if statements in Julia: more elegant way via logical short-circuit evaluation. See "control flow" section of the docs.
# instead of
if <cond> <statement> end
# could use
<cond> && <statement>
# which could be read as: must have <cond> to do <statement>
# similarly
if ! <cond> <statement> end
<cond> || <statement>
# read: either <cond> is met or do <statement> (e.g. throw error)
@multidis
multidis / julia_symcas.jl
Last active August 29, 2015 14:08
Symbolic evaluation in Julia language via SymPy.jl interface to the Python CAS library SumPy. Illustrative tutorials at http://mth229.github.io/symbolic.html
## first verify python-sympy Ubuntu package is installed,
## and Pkg.add("SymPy") in Julia
using Gadfly # before SymPy to have automatic plotting as below
using SymPy
# declare symbols: one of several ways
# see https://github.com/jverzani/SymPy.jl
x, a, b, c = @syms x a b c
typeof(x)
f(x) = a*exp(-(x - b)^2/c)
@multidis
multidis / juliarc.jl
Last active August 29, 2015 14:09
Adding Julia commands to be run on every session's startup. Additions to ENV assoc. array allows defining env variables for Julia use.
# if using custom places for keeping Julia modules
push!(LOAD_PATH, "/home/path/tothatplace")
# defining new env variables
ENV["NEWVAR_NAME"] = "/home/path/newvar"
@multidis
multidis / qcqp_convex.jl
Last active August 29, 2015 14:09
QCQP problem using Convex.jl
## QCQP problem in a convex sub-domain
a = 77.5625
F = [ 351.0 26255.2 -6647.07 1282.81
26255.2 2.23819e6 -780203.0 80689.2
-6647.07 -780203.0 1.25858e6 151221.0
1282.81 80689.2 151221.0 979604.0]
V = [375.77,11564.5,18360.6,3891.82]
# unconstrained min
@multidis
multidis / qcqp_jump.jl
Last active August 29, 2015 14:09
QCQP problem using JuMP.jl
## QCQP problem in a convex sub-domain
a = 77.5625
F = [ 351.0 26255.2 -6647.07 1282.81
26255.2 2.23819e6 -780203.0 80689.2
-6647.07 -780203.0 1.25858e6 151221.0
1282.81 80689.2 151221.0 979604.0]
V = [375.77,11564.5,18360.6,3891.82]
# unconstrained min
@multidis
multidis / julia_to_R_RCall.jl
Last active August 29, 2015 14:14
Converting Julia DataFrame to R object using RCall.jl, one column at a time. String, Bool, Integer (32bit max in R), Real columns work properly. CAUTION: performance not tested for large objects.
## RCall data exchange between Julia and R
using DataFrames
using RCall
# example DataFrame
df = DataFrame(coln1 = 1:4, coln2 = ["M", "F", "F", "M"], coln3 = [false, true, false, true], coln4 = int8([1, 0, 1, 1]))
# R environment in a session started from Julia
g = globalEnv
reval(rparse("dfls <- NULL"))
@multidis
multidis / 00_tuftehand_quantmod.md
Last active August 29, 2015 14:14
Tufte-style handouts with knitr: quantmod chart options.

Tufte handouts

Tufterhandout package. Knit to html in RStudio, initial example.

Problem

Some charts in quantmod draw multiple chunks. Knitr returns error when it can not find all of the corresponding files. This is of course a wider problem than quantmod.

Solution

@multidis
multidis / julia_reactive.jl
Created February 13, 2015 07:57
Reactive.jl works with different types of Input.
## http://julialang.org/Reactive.jl/
using Reactive
x = Input(0)
xsquared = lift(a -> a*a, Int, x)
push!(x,5)
xsquared.value
a = Input(randn(25,3))
@multidis
multidis / reactive_sigdiff.jl
Last active August 29, 2015 14:15
Signal difference with Reactive.jl
## signal difference
using Reactive
s = Input(0.0)
function difference(prev, x)
prev_diff, prev_val = prev
# x becomes prev_val in the next call
return (x-prev_val, x)
end
@multidis
multidis / reactive_concurrency_async.jl
Last active August 29, 2015 14:16
Concurrency in signal updates. NOTE: Reactive.jl is intended for acyclic, directed signal graphs. In this example we essentially deal with a cyclic graph component using @async. May think of it as a full signal graph being split into sub-graphs that update without an enforced order. See concurrent FRP chapter of Evan Czaplicki thesis on ELM lang…
## concurrent signal updates
using Reactive
# trading system case: target and actual position
pact = Input(0)
buy = Input(false)
fptarg(b::Bool) = b ? 10 : 0
ptarg = lift(fptarg, buy)