542 lines
3.6 MiB
Plaintext
542 lines
3.6 MiB
Plaintext
|
{
|
|||
|
"cells":[
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"@file:Repository(\"https:\/\/repo.kotlin.link\")\n",
|
|||
|
"@file:DependsOn(\"space.kscience:plotlykt-jupyter:0.5.0\")"
|
|||
|
],
|
|||
|
"execution_count":1,
|
|||
|
"outputs":[
|
|||
|
{
|
|||
|
"data":{
|
|||
|
"text\/html":[
|
|||
|
" <div id=\"kotlin_out_0\"\/>\n",
|
|||
|
" <script type=\"text\/javascript\">\n",
|
|||
|
" if(!window.kotlinQueues) {\n",
|
|||
|
" window.kotlinQueues = {};\n",
|
|||
|
" }\n",
|
|||
|
" if(!window.kotlinQueues[\"plotly\"]) {\n",
|
|||
|
" var resQueue = [];\n",
|
|||
|
" window.kotlinQueues[\"plotly\"] = resQueue;\n",
|
|||
|
" window[\"call_plotly\"] = function(f) {\n",
|
|||
|
" resQueue.push(f);\n",
|
|||
|
" }\n",
|
|||
|
" }\n",
|
|||
|
" (function (){\n",
|
|||
|
" var modifiers = [(function(script) {\n",
|
|||
|
" script.src = \"https:\/\/cdn.plot.ly\/plotly-1.54.6.min.js\"\n",
|
|||
|
" script.type = \"text\/javascript\";\n",
|
|||
|
"}),\n",
|
|||
|
"(function(script) {\n",
|
|||
|
" script.textContent = \"\/**\\r\\n* plotly.js v1.54.6\\r\\n* Copyright 2012-2020, Plotly, Inc.\\r\\n* All rights reserved.\\r\\n* Licensed under the MIT license\\r\\n*\/\\r\\n!function(t){if(\\\"object\\\"==typeof exports&&\\\"undefined\\\"!=typeof module)module.exports=t();else if(\\\"function\\\"==typeof define&&define.amd)define([],t);else{(\\\"undefined\\\"!=typeof window?window:\\\"undefined\\\"!=typeof global?global:\\\"undefined\\\"!=typeof self?self:this).Plotly=t()}}((function(){return function t(e,r,n){function a(o,s){if(!r[o]){if(!e[o]){var l=\\\"function\\\"==typeof require&&require;if(!s&&l)return l(o,!0);if(i)return i(o,!0);var c=new Error(\\\"Cannot find module '\\\"+o+\\\"'\\\");throw c.code=\\\"MODULE_NOT_FOUND\\\",c}var u=r[o]={exports:{}};e[o][0].call(u.exports,(function(t){return a(e[o][1][t]||t)}),u,u.exports,t,e,r,n)}return r[o].exports}for(var i=\\\"function\\\"==typeof require&&require,o=0;o<n.length;o++)a(n[o]);return a}({1:[function(t,e,r){\\\"use strict\\\";var n=t(\\\"..\/src\/lib\\\"),a={\\\"X,X div\\\":\\\"direction:ltr;font-family:'Open Sans', verdana, arial, sans-serif;margin:0;padding:0;\\\",\\\"X input,X button\\\":\\\"font-family:'Open Sans', verdana, arial, sans-serif;\\\",\\\"X input:focus,X button:focus\\\":\\\"outline:none;\\\",\\\"X a\\\":\\\"text-decoration:none;\\\",\\\"X a:hover\\\":\\\"text-decoration:none;\\\",\\\"X .crisp\\\":\\\"shape-rendering:crispEdges;\\\",\\\"X .user-select-none\\\":\\\"-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;\\\",\\\"X svg\\\":\\\"overflow:hidden;\\\",\\\"X svg a\\\":\\\"fill:#447adb;\\\",\\\"X svg a:hover\\\":\\\"fill:#3c6dc5;\\\",\\\"X .main-svg\\\":\\\"position:absolute;top:0;left:0;pointer-events:none;\\\",\\\"X .main-svg .draglayer\\\":\\\"pointer-events:all;\\\",\\\"X .cursor-default\\\":\\\"cursor:default;\\\",\\\"X .cursor-pointer\\\":\\\"cursor:pointer;\\\",\\\"X .cursor-crosshair\\\":\\\"cursor:crosshair;\\\",\\\"X .cursor-move\\\":\\\"cursor:move;\\\",\\\"X .cursor-col-resize\\\":\\\"cursor:col-resize;\\\",\\\"X .cursor-row-resize\\\":\\\"cursor:row-resize;\\\",\\\"X .cursor-ns-resize\\\":\\\"cursor:ns-resize;\\\",\\\"X .cursor-ew-resize\\\":\\\"cursor:ew-resize;\\\",\\\"X .cursor-sw-resize\\\":\\\"cursor:sw-resize;\\\",\\\"X .cursor-s-resize\\\":\\\"cursor:s-resize;\\\",\\\"X .cursor-se-resize\\\":\\\"cursor:se-resize;\\\",\\\"X .cursor-w-resize\\\":\\\"cursor:w-resize;\\\",\\\"X .cursor-e-resize\\\":\\\"cursor:e-resize;\\\",\\\"X .cursor-nw-resize\\\":\\\"cursor:nw-resize;\\\",\\\"X .cursor-n-resize\\\":\\\"cursor:n-resize;\\\",\\\"X .cursor-ne-resize\\\":\\\"cursor:ne-resize;\\\",\\\"X .cursor-grab\\\":\\\"cursor:-webkit-grab;cursor:grab;\\\",\\\"X .modebar\\\":\\\"position:absolute;top:2px;right:2px;\\\",\\\"X .ease-bg\\\":\\\"-webkit-transition:background-color 0.3s ease 0s;-moz-transition:background-color 0.3s ease 0s;-ms-transition:background-color 0.3s ease 0s;-o-transition:background-color 0.3s ease 0s;transition:background-color 0.3s ease 0s;\\\",\\\"X .modebar--hover>:not(.watermark)\\\":\\\"opacity:0;-webkit-transition:opacity 0.3s ease 0s;-moz-transition:opacity 0.3s ease 0s;-ms-transition:opacity 0.3s ease 0s;-o-transition:opacity 0.3s ease 0s;transition:opacity 0.3s ease 0s;\\\",\\\"X:hover .modebar--hover .modebar-group\\\":\\\"opacity:1;\\\",\\\"X .modebar-group\\\":\\\"float:left;display:inline-block;box-sizing:border-box;padding-left:8px;position:relative;vertical-align:middle;white-space:nowrap;\\\",\\\"X .modebar-btn\\\":\\\"position:relative;font-size:16px;padding:3px 4px;height:22px;cursor:pointer;line-height:normal;box-sizing:border-box;\\\",\\\"X .modebar-btn svg\\\":\\\"position:relative;top:2px;\\\",\\\"X .modebar.vertical\\\":\\\"display:flex;flex-direction:column;flex-wrap:wrap;align-content:flex-end;max-height:100%;\\\",\\\"X .modebar.vertical svg\\\":\\\"top:-1px;\\\",\\\"X .modebar.vertical .modebar-group\\\":\\\"display:block;float:none;padding-left:0px;padding-bottom:8px;\\\",\\\"X .modebar.vertical .modebar-group .modebar-btn\\\":\
|
|||
|
" script.type = \"text\/javascript\";\n",
|
|||
|
"})];\n",
|
|||
|
" var e = document.getElementById(\"kotlin_out_0\");\n",
|
|||
|
" modifiers.forEach(function (gen) {\n",
|
|||
|
" var script = document.createElement(\"script\");\n",
|
|||
|
" gen(script)\n",
|
|||
|
" script.addEventListener(\"load\", function() {\n",
|
|||
|
" window[\"call_plotly\"] = function(f) {f();};\n",
|
|||
|
" window.kotlinQueues[\"plotly\"].forEach(function(f) {f();});\n",
|
|||
|
" window.kotlinQueues[\"plotly\"] = [];\n",
|
|||
|
" }, false);\n",
|
|||
|
" script.addEventListener(\"error\", function() {\n",
|
|||
|
" window[\"call_plotly\"] = function(f) {};\n",
|
|||
|
" window.kotlinQueues[\"plotly\"] = [];\n",
|
|||
|
" var div = document.createElement(\"div\");\n",
|
|||
|
" div.style.color = 'darkred';\n",
|
|||
|
" div.textContent = 'Error loading resource plotly';\n",
|
|||
|
" document.getElementById(\"kotlin_out_0\").appendChild(div);\n",
|
|||
|
" }, false);\n",
|
|||
|
" \n",
|
|||
|
" e.appendChild(script);\n",
|
|||
|
" });\n",
|
|||
|
" })();\n",
|
|||
|
" <\/script>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
|
|||
|
},
|
|||
|
"output_type":"display_data"
|
|||
|
},
|
|||
|
{
|
|||
|
"data":{
|
|||
|
"text\/html":[
|
|||
|
" <div id=\"kotlin_out_1\"\/>\n",
|
|||
|
" <script type=\"text\/javascript\">\n",
|
|||
|
" if(!window.kotlinQueues) {\n",
|
|||
|
" window.kotlinQueues = {};\n",
|
|||
|
" }\n",
|
|||
|
" if(!window.kotlinQueues[\"plotlyConnect\"]) {\n",
|
|||
|
" var resQueue = [];\n",
|
|||
|
" window.kotlinQueues[\"plotlyConnect\"] = resQueue;\n",
|
|||
|
" window[\"call_plotlyConnect\"] = function(f) {\n",
|
|||
|
" resQueue.push(f);\n",
|
|||
|
" }\n",
|
|||
|
" }\n",
|
|||
|
" (function (){\n",
|
|||
|
" var modifiers = [(function(script) {\n",
|
|||
|
" script.textContent = \"\/**\\r\\n * Use existing plotly or load it from the CDN if it is not available\\r\\n * @param action\\r\\n *\/\\r\\nfunction withPlotly(action) {\\r\\n if (typeof Plotly !== \\\"undefined\\\") {\\r\\n action(Plotly);\\r\\n } else if (typeof window.promiseOfPlotly !== \\\"undefined\\\") {\\r\\n window.promiseOfPlotly.then(plotly => action(plotly));\\r\\n } else {\\r\\n console.warn(\\\"Plotly not defined. Loading the script from CDN\\\")\\r\\n window.promiseOfPlotly = new Promise((accept, reject) => {\\r\\n let plotlyLoaderScript = document.createElement(\\\"script\\\");\\r\\n plotlyLoaderScript.src = \\\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/plotly.js\/1.54.6\/plotly.min.js\\\";\\r\\n plotlyLoaderScript.type = 'text\/javascript';\\r\\n plotlyLoaderScript.onload = () => {\\r\\n accept(Plotly);\\r\\n }\\r\\n plotlyLoaderScript.onerror = (error) => {\\r\\n console.error(error);\\r\\n reject(error)\\r\\n }\\r\\n document.head.appendChild(plotlyLoaderScript);\\r\\n });\\r\\n }\\r\\n}\\r\\n\\r\\n\/**\\r\\n * Request and parse json from given address\\r\\n * @param url {URL}\\r\\n * @param callback\\r\\n * @return Promise<Json>\\r\\n *\/\\r\\nfunction getJSON(url, callback) {\\r\\n\\r\\n function handleErrors(response) {\\r\\n if (!response.ok) {\\r\\n throw Error(response.statusText);\\r\\n }\\r\\n return response;\\r\\n }\\r\\n\\r\\n try {\\r\\n fetch(url, {\\r\\n method: 'GET',\\r\\n headers: {\\r\\n Accept: 'application\/json',\\r\\n }\\r\\n })\\r\\n .then(handleErrors)\\r\\n .then(response => response.json())\\r\\n .then(json => callback(json))\\r\\n .catch(error => console.log(error));\\r\\n } catch (e) {\\r\\n alert(\\\"Fetch of plot data failed with error: \\\" + e)\\r\\n }\\r\\n}\\r\\n\\r\\n\/**\\r\\n * Safe call for Plotly.newPlot\\r\\n * @param id\\r\\n * @param data\\r\\n * @param layout\\r\\n * @param config\\r\\n *\/\\r\\nfunction makePlot(id, data, layout, config) {\\r\\n withPlotly(plotly => plotly.newPlot(id, data, layout, config))\\r\\n}\\r\\n\\r\\n\/**\\r\\n * Create a plot taking data from given url\\r\\n * @param id {string} element id for plot\\r\\n * @param from {URL} json server url\\r\\n * @param config {object} plotly configuration\\r\\n * @return {JSON}\\r\\n *\/\\r\\nfunction createPlotFrom(id, from, config = {}) {\\r\\n getJSON(from, json => withPlotly(plotly => {\\r\\n plotly.newPlot(id, json.data, json.layout, config)\\r\\n }));\\r\\n}\\r\\n\\r\\n\/**\\r\\n * Update a plot taking data from given url\\r\\n * @param id {string} element id for plot\\r\\n * @param from {URL} json server url\\r\\n * @return {JSON}\\r\\n *\/\\r\\nfunction updatePlotFrom(id, from) {\\r\\n getJSON(from, json => withPlotly(plotly => plotly.react(id, json.data, json.layout)));\\r\\n}\\r\\n\\r\\n\/**\\r\\n * Start pull updates with regular requests from client side\\r\\n * @param id {string}\\r\\n * @param from\\r\\n * @param millis\\r\\n *\/\\r\\nfunction startPull(id, from, millis) {\\r\\n let action = function () {\\r\\n updatePlotFrom(id, from)\\r\\n };\\r\\n window.setInterval(action, millis)\\r\\n}\\r\\n\\r\\n\/**\\r\\n * Start push updates via websocket\\r\\n * @param id {string} element id for plot\\r\\n * @param ws {URL} a websocket address\\r\\n *\/\\r\\nfunction startPush(id, ws) {\\r\\n let socket = new WebSocket(ws);\\r\\n\\r\\n socket.onopen = function () {\\r\\n console.log(\\\"[Plotly.kt] A connection for plot with id = \\\" + id + \\\" with server established on \\\" + ws);\\r\\n };\\r\\n\\r\\n socket.onclose = function (event) {\\r\\n if (event.wasClean) {\\r\\n console.log(\\\"The connection with server is closed\\\");\\r\\n } else {\\r\\n co
|
|||
|
" script.type = \"text\/javascript\";\n",
|
|||
|
"})];\n",
|
|||
|
" var e = document.getElementById(\"kotlin_out_1\");\n",
|
|||
|
" modifiers.forEach(function (gen) {\n",
|
|||
|
" var script = document.createElement(\"script\");\n",
|
|||
|
" gen(script)\n",
|
|||
|
" script.addEventListener(\"load\", function() {\n",
|
|||
|
" window[\"call_plotlyConnect\"] = function(f) {f();};\n",
|
|||
|
" window.kotlinQueues[\"plotlyConnect\"].forEach(function(f) {f();});\n",
|
|||
|
" window.kotlinQueues[\"plotlyConnect\"] = [];\n",
|
|||
|
" }, false);\n",
|
|||
|
" script.addEventListener(\"error\", function() {\n",
|
|||
|
" window[\"call_plotlyConnect\"] = function(f) {};\n",
|
|||
|
" window.kotlinQueues[\"plotlyConnect\"] = [];\n",
|
|||
|
" var div = document.createElement(\"div\");\n",
|
|||
|
" div.style.color = 'darkred';\n",
|
|||
|
" div.textContent = 'Error loading resource plotlyConnect';\n",
|
|||
|
" document.getElementById(\"kotlin_out_1\").appendChild(div);\n",
|
|||
|
" }, false);\n",
|
|||
|
" \n",
|
|||
|
" e.appendChild(script);\n",
|
|||
|
" });\n",
|
|||
|
" })();\n",
|
|||
|
" <\/script>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
|
|||
|
},
|
|||
|
"output_type":"display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"oUCrttLL2OmTMU5sqF0WoG",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"Plotly.jupyter.notebook()"
|
|||
|
],
|
|||
|
"execution_count":2,
|
|||
|
"outputs":[
|
|||
|
{
|
|||
|
"data":{
|
|||
|
"text\/html":[
|
|||
|
"<div style=\"color: blue;\">Plotly notebook integration switch into the legacy mode.<\/div>\n"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
|
|||
|
},
|
|||
|
"output_type":"display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"pqfKI18qbEcbcjxf22n1hF",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"markdown",
|
|||
|
"source":[
|
|||
|
"# Центральная предельная теорема\n",
|
|||
|
"\n",
|
|||
|
"[Центральная предельная теорема](https:\/\/ru.wikipedia.org\/wiki\/%D0%A6%D0%B5%D0%BD%D1%82%D1%80%D0%B0%D0%BB%D1%8C%D0%BD%D0%B0%D1%8F_%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D1%8C%D0%BD%D0%B0%D1%8F_%D1%82%D0%B5%D0%BE%D1%80%D0%B5%D0%BC%D0%B0) в вльной формулировке гласит, что сумма случайных величин, имеющих распределение с конечным средним и дисперсией, распределена по нормальному распределению со средним, равным сумме средних компонент и дисперсией, равно сумме дисперсий.\n",
|
|||
|
"\n",
|
|||
|
"На доказательство этой теоремы в общем виде обычно уходит львиная доля семестра в курсе теории вероятности. В данной задаче мы попробуем проверить эту теорему на практике.\n",
|
|||
|
"\n",
|
|||
|
"Для этого мы возьмем некоторое распределение и посчитаем численно распределение суммы.\n",
|
|||
|
"\n",
|
|||
|
"Для решения задачи нам потребуется:\n",
|
|||
|
"\n",
|
|||
|
"* Определить функцию плотности вероятности распределения\n",
|
|||
|
"* Создать функцию для расчета распределения суммы двух случайных величин. Распределение суммы случайных величин вычисляется как свертка распределений (см. ниже).\n",
|
|||
|
"* Последовательно посчитатать распределение суммы нескольких величин `a + a + a + a = (((a + a) + a) +a)`\n",
|
|||
|
"* Сравнить результат с нормальным распределением.\n",
|
|||
|
"\n",
|
|||
|
"Для решения задачи может понадобится представление функции в виде набора точек при помощи процедуры [линейной интерпояции](https:\/\/ru.wikipedia.org\/wiki\/%D0%9B%D0%B8%D0%BD%D0%B5%D0%B9%D0%BD%D0%B0%D1%8F_%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D0%BF%D0%BE%D0%BB%D1%8F%D1%86%D0%B8%D1%8F).\n",
|
|||
|
"\n",
|
|||
|
"## Свертка\n",
|
|||
|
"\n",
|
|||
|
"[Сверткой](https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%B2%D1%91%D1%80%D1%82%D0%BA%D0%B0_(%D0%BC%D0%B0%D1%82%D0%B5%D0%BC%D0%B0%D1%82%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B8%D0%B9_%D0%B0%D0%BD%D0%B0%D0%BB%D0%B8%D0%B7)) \n",
|
|||
|
"двух функций называется следующее интегральное преобразование:\n",
|
|||
|
"$$\n",
|
|||
|
" h(x) = f(x) \\otimes g(x) = \\int {f(x-y) g(y) dy }\n",
|
|||
|
"$$\n",
|
|||
|
"Интегрирование ведется по всей области определения функций. Очевидно, что переменные под интегрированием можно менять местами. Для вычисления интеграла можно использовать [метод прямоугольников](https:\/\/ru.wikipedia.org\/wiki\/%D0%9C%D0%B5%D1%82%D0%BE%D0%B4_%D0%BF%D1%80%D1%8F%D0%BC%D0%BE%D1%83%D0%B3%D0%BE%D0%BB%D1%8C%D0%BD%D0%B8%D0%BA%D0%BE%D0%B2) или [метод трапеций](https:\/\/ru.wikipedia.org\/wiki\/%D0%9C%D0%B5%D1%82%D0%BE%D0%B4_%D1%82%D1%80%D0%B0%D0%BF%D0%B5%D1%86%D0%B8%D0%B9)."
|
|||
|
],
|
|||
|
"attachments":{
|
|||
|
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"Qt6aQ5KMLEic2fkkr4qlVt",
|
|||
|
"type":"MD",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"markdown",
|
|||
|
"source":[
|
|||
|
"\n",
|
|||
|
"## Интерполяция\n",
|
|||
|
"\n",
|
|||
|
"Для помощи в решении задачи можно использовать готовую реализацию линейной интерполяции."
|
|||
|
],
|
|||
|
"attachments":{
|
|||
|
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"4NbaS7idD8SbiGNmBoJkSg",
|
|||
|
"type":"MD",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"typealias Function = (Double) -> Double\n",
|
|||
|
"\n",
|
|||
|
"fun pointsFromRange(from: Double, to: Double, numPoints: Int = 100): List<Double>{\n",
|
|||
|
" require( to > from)\n",
|
|||
|
" require(numPoints > 2)\n",
|
|||
|
" val step = (to - from)\/(numPoints - 1)\n",
|
|||
|
" require(step>0)\n",
|
|||
|
" return List(numPoints){i -> from + step * i}\n",
|
|||
|
"}\n",
|
|||
|
"\n",
|
|||
|
"fun Function.interpolate(from: Double, to: Double, numPoints: Int = 100): Function{\n",
|
|||
|
" \/\/compute xs\n",
|
|||
|
" val xs = pointsFromRange(from, to, numPoints) \n",
|
|||
|
" \/\/compute function values\n",
|
|||
|
" val ys = xs.map{ invoke(it) }\n",
|
|||
|
" \n",
|
|||
|
" \/\/ return an anonimous function\n",
|
|||
|
" return { x ->\n",
|
|||
|
" \/\/ check interpolation region. Zerop outside the region\n",
|
|||
|
" if(x < xs.first() || x > xs.last()) 0.0\n",
|
|||
|
" \/\/ find section number\n",
|
|||
|
" val num: Int = xs.indexOfFirst { it > x }\n",
|
|||
|
" \/\/ num >=1\n",
|
|||
|
" if(num <= 0){\n",
|
|||
|
" 0.0\n",
|
|||
|
" } else{\n",
|
|||
|
" \/\/return the result as last expression\n",
|
|||
|
" ys[num - 1] + (ys[num] - ys[num - 1])\/(xs[num] - xs[num - 1])*(x - xs[num - 1])\n",
|
|||
|
" }\n",
|
|||
|
" }\n",
|
|||
|
"}"
|
|||
|
],
|
|||
|
"execution_count":3,
|
|||
|
"outputs":[
|
|||
|
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"a8NBDUb8ifDAPZYpU3OSzM",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"markdown",
|
|||
|
"source":[
|
|||
|
"# Решение"
|
|||
|
],
|
|||
|
"attachments":{
|
|||
|
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"hqV4unJLx6pKvlyMqRhdnP",
|
|||
|
"type":"MD",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"markdown",
|
|||
|
"source":[
|
|||
|
"### Проверка"
|
|||
|
],
|
|||
|
"attachments":{
|
|||
|
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"6jRPRbjVKunp5d7dhFCKU6",
|
|||
|
"type":"MD",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"fun l2RelativeDiff(res: Function, order: Int, baseDispersion: Double): Double{\n",
|
|||
|
" val numPointForDiff = 100\n",
|
|||
|
" val dispersion = baseDispersion * order\n",
|
|||
|
" val sigma = sqrt(dispersion)\n",
|
|||
|
" val xs = ArrayList<Double>()\n",
|
|||
|
" for(i in 0 until numPointForDiff){\n",
|
|||
|
" xs.add(-3*sigma + i.toDouble()\/(numPointForDiff-1)*6*sigma)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" fun normal(x: Double): Double{\n",
|
|||
|
" return 1.0\/sqrt(2* PI*dispersion)*exp(-x.pow(2)\/2.0\/dispersion)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" return xs.sumOf{ x-> \n",
|
|||
|
" (res(x)\/normal(x) - 1.0).pow(2)\n",
|
|||
|
" }\n",
|
|||
|
"}"
|
|||
|
],
|
|||
|
"execution_count":4,
|
|||
|
"outputs":[
|
|||
|
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"xlUgyZfYueDee0UowZZW1O",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"\n",
|
|||
|
"fun integrate(f: Function, from: Double, to: Double, numPoints: Int = 100): Double{\n",
|
|||
|
" val xs = pointsFromRange(from, to, numPoints)\n",
|
|||
|
" return xs.sumOf { f(it) } * (to - from) \/ numPoints\n",
|
|||
|
"}\n",
|
|||
|
"\n",
|
|||
|
"fun convolve(f: Function, g: Function, from: Double, to: Double, numPoints: Int = 100): Function{\n",
|
|||
|
" val res: Function = { x ->\n",
|
|||
|
" val integrand: Function = { y -> f(x - y) * g(y)}\n",
|
|||
|
" integrate(integrand, from, to, numPoints)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" \/\/We need interpolation here to limit complexity\n",
|
|||
|
" \/\/With interpolation it linear, without it is exponential\n",
|
|||
|
" return res.interpolate(from, to, numPoints)\n",
|
|||
|
"}"
|
|||
|
],
|
|||
|
"execution_count":5,
|
|||
|
"outputs":[
|
|||
|
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"MyDmwsnS5H82wfWbgopf8U",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"val f: Function = {x -> if(x in (-1.0..1.0)) 0.5 else 0.0}\n",
|
|||
|
"\n",
|
|||
|
"val from = -5.0\n",
|
|||
|
"val to = 5.0\n",
|
|||
|
"\n",
|
|||
|
"val num = 300\n",
|
|||
|
"\n",
|
|||
|
"val first: Function = convolve(f, f, from, to, num)\n",
|
|||
|
"val second: Function = convolve(first, f, from, to, num)\n",
|
|||
|
"val third: Function = convolve(second, f, from, to, num)\n",
|
|||
|
"val fourth: Function = convolve(third, f, from, to, num)\n",
|
|||
|
"val fifth: Function = convolve(fourth, f, from, to, num)"
|
|||
|
],
|
|||
|
"execution_count":6,
|
|||
|
"outputs":[
|
|||
|
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"81kNX1pA9p6s28xxrLf9cr",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"val xs = pointsFromRange(from, to, num)\n",
|
|||
|
"\n",
|
|||
|
"Plotly.plot { \n",
|
|||
|
"\n",
|
|||
|
" scatter { \n",
|
|||
|
" name = \"initial\"\n",
|
|||
|
" x.numbers = xs\n",
|
|||
|
" y.numbers = xs.map(f)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" scatter { \n",
|
|||
|
" name = \"first\"\n",
|
|||
|
" x.numbers = xs\n",
|
|||
|
" y.numbers = xs.map(first)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" scatter { \n",
|
|||
|
" name = \"second\"\n",
|
|||
|
" x.numbers = xs\n",
|
|||
|
" y.numbers = xs.map(second)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" scatter { \n",
|
|||
|
" name = \"fifth\"\n",
|
|||
|
" x.numbers = xs\n",
|
|||
|
" y.numbers = xs.map(fifth)\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" val d = 6.0\/3.0\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
" scatter { \n",
|
|||
|
" name = \"normal\"\n",
|
|||
|
" x.numbers = xs\n",
|
|||
|
" y.numbers = xs.map{1.0\/sqrt(2*PI*d) * exp(- it.pow(2)\/d\/2) }\n",
|
|||
|
" }\n",
|
|||
|
" }"
|
|||
|
],
|
|||
|
"execution_count":7,
|
|||
|
"outputs":[
|
|||
|
{
|
|||
|
"data":{
|
|||
|
"text\/html":[
|
|||
|
"<html>\n",
|
|||
|
" <head>\n",
|
|||
|
" <meta charset=\"utf-8\">\n",
|
|||
|
" <title>Plotly.kt<\/title>\n",
|
|||
|
" <script type=\"text\/javascript\" src=\"https:\/\/cdn.plot.ly\/plotly-1.54.6.min.js\"><\/script>\n",
|
|||
|
" <\/head>\n",
|
|||
|
" <body>\n",
|
|||
|
" <div id=\"space.kscience.plotly.Plot@7acaf8c6\">\n",
|
|||
|
" <script>if(typeof Plotly !== \"undefined\"){\n",
|
|||
|
" Plotly.react(\n",
|
|||
|
" 'space.kscience.plotly.Plot@7acaf8c6',\n",
|
|||
|
" [{\"type\":\"scatter\",\"name\":\"initial\",\"x\":[-5.0,-4.966555183946488,-4.933110367892977,-4.8996655518394645,-4.866220735785953,-4.832775919732441,-4.79933110367893,-4.765886287625418,-4.7324414715719065,-4.698996655518394,-4.665551839464883,-4.632107023411371,-4.59866220735786,-4.565217391304348,-4.531772575250836,-4.498327759197324,-4.464882943143813,-4.431438127090301,-4.39799331103679,-4.364548494983278,-4.331103678929766,-4.297658862876254,-4.264214046822742,-4.230769230769231,-4.19732441471572,-4.1638795986622075,-4.130434782608695,-4.096989966555184,-4.063545150501672,-4.030100334448161,-3.9966555183946486,-3.9632107023411374,-3.9297658862876252,-3.896321070234114,-3.862876254180602,-3.8294314381270906,-3.7959866220735785,-3.7625418060200673,-3.729096989966555,-3.6956521739130435,-3.6622073578595318,-3.62876254180602,-3.5953177257525084,-3.5618729096989967,-3.528428093645485,-3.4949832775919734,-3.4615384615384617,-3.42809364548495,-3.3946488294314383,-3.361204013377926,-3.327759197324415,-3.294314381270903,-3.2608695652173916,-3.2274247491638794,-3.193979933110368,-3.160535117056856,-3.127090301003345,-3.0936454849498327,-3.0602006688963215,-3.0267558528428093,-2.9933110367892977,-2.959866220735786,-2.9264214046822743,-2.8929765886287626,-2.859531772575251,-2.8260869565217392,-2.7926421404682276,-2.759197324414716,-2.725752508361204,-2.6923076923076925,-2.658862876254181,-2.625418060200669,-2.5919732441471575,-2.558528428093646,-2.525083612040134,-2.491638795986622,-2.4581939799331103,-2.4247491638795986,-2.391304347826087,-2.3578595317725752,-2.3244147157190636,-2.290969899665552,-2.25752508361204,-2.2240802675585285,-2.190635451505017,-2.157190635451505,-2.1237458193979935,-2.0903010033444818,-2.05685618729097,-2.0234113712374584,-1.9899665551839467,-1.956521739130435,-1.9230769230769234,-1.8896321070234117,-1.8561872909699,-1.8227424749163883,-1.7892976588628766,-1.7558528428093645,-1.7224080267558528,-1.6889632107023411,-1.6555183946488294,-1.6220735785953178,-1.588628762541806,-1.5551839464882944,-1.5217391304347827,-1.488294314381271,-1.4548494983277593,-1.4214046822742477,-1.387959866220736,-1.3545150501672243,-1.3210702341137126,-1.287625418060201,-1.2541806020066892,-1.2207357859531776,-1.1872909698996659,-1.1538461538461542,-1.1204013377926425,-1.0869565217391308,-1.0535117056856187,-1.020066889632107,-0.9866220735785953,-0.9531772575250841,-0.919732441471572,-0.8862876254180607,-0.8528428093645486,-0.8193979933110374,-0.7859531772575252,-0.752508361204014,-0.7190635451505019,-0.6856187290969897,-0.6521739130434785,-0.6187290969899664,-0.5852842809364551,-0.551839464882943,-0.5183946488294318,-0.48494983277591963,-0.4515050167224084,-0.41806020066889626,-0.384615384615385,-0.3511705685618729,-0.31772575250836166,-0.28428093645484953,-0.2508361204013383,-0.21739130434782616,-0.18394648829431492,-0.1505016722408028,-0.11705685618729156,-0.08361204013377943,-0.05016722408026819,-0.016722408026756064,0.016722408026756064,0.0501672240802673,0.08361204013377943,0.11705685618729067,0.1505016722408028,0.18394648829431404,0.21739130434782616,0.2508361204013374,0.28428093645484953,0.31772575250836077,0.3511705685618729,0.38461538461538414,0.41806020066889626,0.4515050167224075,0.48494983277591963,0.5183946488294309,0.551839464882943,0.5852842809364542,0.6187290969899664,0.6521739130434776,0.6856187290969897,0.7190635451505019,0.7525083612040131,0.7859531772575252,0.8193979933110365,0.8528428093645486,0.8862876254180598,0.919732441471572,0.9531772575250832,0.9866220735785953,1.0200668896321066,1.0535117056856187,1.08695652173913,1.120401337792642,1.1538461538461533,1.1872909698996654,1.2207357859531767,1.2541806020066888,1.2876254180602,1.3210702341137122,1.3545150501672234,1.3879598662207355,1.4214046822742468,1.454849498327759,1.488294314381271,1.5217391304347823,1.5551839464882944,1.5886287625418056,1.6220735785953178,1.655518394648829,1.6889632107023411,1.7224080267558524,1.7558528428093645,1.7892976588628757,1.8227424749163879,1.856187290969899,1.8896321070234112,1.9230769230769225,1
|
|||
|
" {},\n",
|
|||
|
" {}\n",
|
|||
|
" ); \n",
|
|||
|
"} else {\n",
|
|||
|
" console.error(\"Plotly not loaded\")\n",
|
|||
|
"}<\/script>\n",
|
|||
|
" <\/div>\n",
|
|||
|
" <\/body>\n",
|
|||
|
"<\/html>\n"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
|
|||
|
},
|
|||
|
"output_type":"display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"23SRPrrfaVvkoX5Ue3awDk",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
"l2RelativeDiff(fifth, 5,1.0)"
|
|||
|
],
|
|||
|
"execution_count":8,
|
|||
|
"outputs":[
|
|||
|
{
|
|||
|
"data":{
|
|||
|
"text\/plain":[
|
|||
|
"53.72510082115637"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata":{
|
|||
|
|
|||
|
},
|
|||
|
"output_type":"display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"zTk7ZWxmVa4qvyR3GbZ2nl",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type":"code",
|
|||
|
"source":[
|
|||
|
|
|||
|
],
|
|||
|
"execution_count":null,
|
|||
|
"outputs":[
|
|||
|
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"datalore":{
|
|||
|
"node_id":"CUEoUJHnmZiaadKblP7Tyk",
|
|||
|
"type":"CODE",
|
|||
|
"hide_input_from_viewers":true,
|
|||
|
"hide_output_from_viewers":true
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata":{
|
|||
|
"kernelspec":{
|
|||
|
"display_name":"Kotlin",
|
|||
|
"language":"kotlin",
|
|||
|
"name":"kotlin"
|
|||
|
},
|
|||
|
"datalore":{
|
|||
|
"computation_mode":"REACTIVE",
|
|||
|
"package_manager":"pip",
|
|||
|
"base_environment":"default",
|
|||
|
"packages":[
|
|||
|
|
|||
|
],
|
|||
|
"report_row_ids":[
|
|||
|
|
|||
|
],
|
|||
|
"version":3
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat":4,
|
|||
|
"nbformat_minor":4
|
|||
|
}
|