{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "lQbSB87rNAn9lV6poArVWW", "type": "CODE" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%use kmath\n", "%use plotly(0.5.0)\n", "@file:DependsOn(\"org.apache.commons:commons-math3:3.6.1\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "0UP158hfccGgjQtHz0wAi6", "type": "CODE" } }, "outputs": [], "source": [ "//Plotly.jupyter.notebook()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "Zhgz1Ui91PWz0meJiQpHol", "type": "CODE" } }, "outputs": [], "source": [ "class XYValues(val xValues: DoubleArray, val yValues: DoubleArray) {\n", " init {\n", " require(xValues.size == yValues.size)\n", " }\n", "}\n", "\n", "fun interface XYStatistic {\n", " operator fun invoke(values: XYValues): Double\n", "}\n", "\n", "fun generateParabola(xValues: DoubleArray, a: Double, b: Double, c: Double): XYValues {\n", " val yValues = xValues.map { x -> a * x * x + b * x + c }.toDoubleArray()\n", " return XYValues(xValues, yValues)\n", "}\n", "\n", "fun generateHyperbole(xValues: DoubleArray, gamma: Double, x0: Double, y0: Double): XYValues {\n", " val yValues = xValues.map { x -> y0 + gamma / (x - x0) }.toDoubleArray()\n", " return XYValues(xValues, yValues)\n", "}\n", "\n", "class ConvolutionalXYStatistic(val weights: DoubleArray) : XYStatistic {\n", " override fun invoke(values: XYValues): Double {\n", " require(weights.size == values.yValues.size)\n", " val norm = values.yValues.sum()\n", " return values.yValues.zip(weights) { value, weight -> value * weight }.sum()/norm\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "ZE2atNvFzQsCvpAF8KK4ch", "type": "CODE" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", " \n", "
\n", "
\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val xValues = (1.0..10.0).step(1.0).toDoubleArray()\n", "\n", "val xy = generateHyperbole(xValues, 1.0, 0.0, 0.0)\n", "\n", "Plotly.plot {\n", " scatter {\n", " this.x.doubles = xValues\n", " this.y.doubles = xy.yValues\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "EA5HaydTddRKYrtAUwd29h", "type": "CODE" } }, "outputs": [ { "data": { "text/plain": [ "0.05690285869123425" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val statistic = ConvolutionalXYStatistic(DoubleArray(xValues.size){if(it == 5) 1.0 else 0.0})\n", "statistic(xy)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "t5t6IYmD7Q1ykeo9uijFfQ", "type": "CODE" } }, "outputs": [], "source": [ "import kotlin.random.Random\n", "\n", "val random = Random(1288)\n", "\n", "val parabolae = buildList{\n", " repeat(500){\n", " add(\n", " generateParabola(\n", " xValues, \n", " random.nextDouble(), \n", " random.nextDouble(), \n", " random.nextDouble()\n", " )\n", " )\n", " }\n", "}\n", "\n", "val hyperbolae: List = buildList{\n", " repeat(500){\n", " add(\n", " generateHyperbole(\n", " xValues, \n", " random.nextDouble()*10, \n", " random.nextDouble(), \n", " random.nextDouble()\n", " )\n", " )\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "oXB8lmju7YVYjMRXITKnhO", "type": "CODE" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", " \n", "
\n", "
\n" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Plotly.plot { \n", " scatter { \n", " x.doubles = xValues\n", " y.doubles = parabolae[257].yValues\n", " }\n", " scatter { \n", " x.doubles = xValues\n", " y.doubles = hyperbolae[252].yValues\n", " }\n", " }" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "8EIIecUZrt2NNrOkhxG5P0", "type": "CODE" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", " \n", "
\n", "
\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Plotly.plot { \n", " histogram { \n", " name = \"parabolae\"\n", " x.numbers = parabolae.map { statistic(it) }\n", " }\n", " histogram { \n", " name = \"hyperbolae\"\n", " x.numbers = hyperbolae.map { statistic(it) }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "h7UmglJW5zXkAfKHK40oIL", "type": "CODE" } }, "outputs": [], "source": [ "val lossFunction: (XYStatistic) -> Double = { statistic ->\n", " - abs(parabolae.sumOf { statistic(it) } - hyperbolae.sumOf { statistic(it) })\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "VpQCNmHqHGWNEWDU21jh1D", "type": "CODE" } }, "outputs": [], "source": [ "import org.apache.commons.math3.optimization.*\n", "import org.apache.commons.math3.analysis.*\n", "import org.apache.commons.math3.optimization.direct.*\n", "\n", "val cmFunction = object: MultivariateFunction{\n", " override fun value(point: DoubleArray) = lossFunction(ConvolutionalXYStatistic(point))\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "0EG3K4aCUciMlgGQKPvJ57", "type": "CODE" } }, "outputs": [], "source": [ "val optimizer = BOBYQAOptimizer(20)\n", "\n", "val result = optimizer.optimize(1000, cmFunction, GoalType.MINIMIZE, xValues)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "LelUlY0ZSlJEO9yC6SLk5B", "type": "CODE" } }, "outputs": [ { "data": { "text/plain": [ "[-1.357757502856414E8, -2.0157729926862407E7, -1.003627060413469E7, -3516890.1555952034, 2318972.567610743, 8257264.5552848, 1.4601023119437138E7, 2.149227167045051E7, 2.9007620324898317E7, -1.740750223163073E7]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result.point" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "AuFOq5t9KpOIkGrOLsVXNf", "type": "CODE" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", " \n", "
\n", "
\n" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Plotly.plot { \n", " scatter { \n", " y.doubles = result.point\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "zvmq42DRdM5mZ3SpzviHwI", "type": "CODE" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", " \n", "
\n", "
\n" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val resultStatistic = ConvolutionalXYStatistic(result.point)\n", "Plotly.plot { \n", " histogram { \n", " name = \"parabolae\"\n", " x.numbers = parabolae.map { resultStatistic(it) }\n", " }\n", " histogram { \n", " name = \"hyperbolae\"\n", " x.numbers = hyperbolae.map { resultStatistic(it) }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "datalore": { "hide_input_from_viewers": true, "hide_output_from_viewers": true, "node_id": "1lBar7ejuP92idI3yiquIG", "type": "CODE" } }, "outputs": [], "source": [] } ], "metadata": { "datalore": { "base_environment": "default", "computation_mode": "JUPYTER", "package_manager": "pip", "packages": [], "report_row_ids": [], "version": 3 }, "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.9.23" } }, "nbformat": 4, "nbformat_minor": 4 }