Skip to content

Instantly share code, notes, and snippets.

View thomasnield's full-sized avatar

Thomas Nield thomasnield

  • Frisco, Texas (United States)
View GitHub Profile
@thomasnield
thomasnield / Refreshable.kt
Created September 14, 2018 15:30
RxJava Refreshable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.subjects.BehaviorSubject
class RefreshTrigger {
private val _refreshEvents = BehaviorSubject.createDefault(Unit).toSerialized()
val refreshEvents: Observable<Unit> = _refreshEvents
fun refresh() = _refreshEvents.onNext(Unit)
@thomasnield
thomasnield / two_columns_data.csv
Created September 20, 2018 02:08
two_columns_data.csv
99.5048419879 159.4644145002
44.6627659498 50.5081790885
66.6313365164 91.6775703113
74.6638781433 121.2768737608
57.5548532699 77.6605107301
179.5856336758 315.783778234
35.6445040985 43.0664477198
86.4089899745 140.1552280161
152.7882048383 266.240528791
1.6307540858 -20.8768252687
@thomasnield
thomasnield / linear_regression_gradient_descent.kts
Last active September 20, 2018 23:48
Kotlin Linear Regression with Gradient Descent
import java.net.URL
fun main(args: Array<String>) {
// Fit points to a line conforming to y = mx + b
class Point(val x: Double, val y: Double)
val points = URL("https://tinyurl.com/yaxgfjzt")
.readText().split(Regex("\\r?\\n"))
@thomasnield
thomasnield / pdf_calculus_fun.kt
Last active November 9, 2018 19:08
pdf_calculus_fun.kt
import java.util.concurrent.ThreadLocalRandom
import kotlin.math.pow
import kotlin.math.sqrt
fun main(args: Array<String>) {
(0..1000).map { randomSample() }
.forEach {
println("$it,${2*it}")
}
@thomasnield
thomasnield / quick_derivative_hack.kt
Created November 9, 2018 19:23
Quick Hacky Derivative
import kotlin.math.pow
fun main(args: Array<String>) {
val xSquare = { x: Double -> x.pow(2) }
val slope = derivativeOf(1.0, xSquare)
println(slope)
}
@thomasnield
thomasnield / relaxed_achievable_maximum.kt
Created November 19, 2018 17:17
Relaxed Achievable Maximum
fun main(args: Array<String>) {
class Item(val weight: Double, val value: Double)
val items = listOf(
Item(1.0, 2.0),
Item(2.0, 4.0),
Item(3.0, 6.0)
)
@thomasnield
thomasnield / ContinuousDistributionSampler.kt
Created December 11, 2018 19:29
Continuous Distribution Sampler
import java.util.concurrent.ThreadLocalRandom
import kotlin.math.pow
fun pdf(x: Double) = 2 * x // y = 2x from 0 to 1 is probability distribution function
fun cdf(x: Double) = x.pow(2) // CDF plots area under PDF curve https://www.wolframalpha.com/input/?i=integral+2x
fun reverseCdf(a: Double) = a.pow(.5) // Reverse CDF to sample the area and the resuling x
fun main() {
(1..1000).asSequence()
@thomasnield
thomasnield / sampling_arbitrary_curve.kt
Last active December 18, 2018 22:45
Sampling an Arbitrary Curve
import kotlin.math.pow
// https://www.desmos.com/calculator/qjxcsxsl6t
// https://www.wolframalpha.com/input/?i=integral+-2(x-1)%5E2+%2B+2
fun main(args: Array<String>) {
println(f(1.0))
println(integratedF(0.0,2.0))
@thomasnield
thomasnield / binomial_ojalgo_example.kt
Last active December 20, 2018 02:31
Binomial Distribution ojAlgo Example
import org.ojalgo.random.Binomial
fun main(args: Array<String>) {
// I have a 6-sided fair dice
// what is the probability I will roll 3 sixes out of 10 trials?
val probabilityOfSide = 1.0 / 6.0
Binomial(10, probabilityOfSide).apply {
@thomasnield
thomasnield / quick_distribution_scatterchart.kt
Created December 29, 2018 05:29
quick_distribution_scatterchart.kt
import javafx.application.Application
import javafx.scene.chart.NumberAxis
import org.ojalgo.random.Normal
import tornadofx.*
fun main(args: Array<String>) {
Application.launch(QuickScatterPlotApp::class.java)
}