python - 如何在 python 中生成市场的分形图

标签 python python-2.7 fractals

我希望在 python 中生成它:

http://classes.yale.edu/fractals/RandFrac/Market/TradingTime/Example1/Example1.html

但我对这个概念非常陌生。有人知道这方面的图书馆或要点吗?

enter image description here

编辑: 据我了解,您每次都需要将分形一分为二。所以你必须从两个中点之间的线计算 y 轴点。那么这两个截面需要根据分形形成吗?

最佳答案

不是 100% 确定您在问什么,但正如我从您的评论中了解到的那样,您想使用链接中描述的递归生成一条看起来很逼真的股票市场曲线。

据我了解链接页面中的描述和some of the parent pages ,它是这样工作的:

  1. 给定起点、终点和多个转折点,格式为(t1, v1)(t2, v2)等,例如start=(0,0), end=(1, 1), turns = [(1/4, 1/2), (3/4, 1/4)],其中 ti 和 < em>vi 是 0 到 1 之间的分数。
  2. 您确定按比例缩放到开始结束 之间的间隔的实际转折点,并计算这些点之间的差异,即距离p< sub>i 达到 pi+1
  3. 您打乱这些片段以引入一些随机性;当放在一起时,它们仍然覆盖完全相同的距离,即它们连接原始的开始结束点。
  4. 通过递归调用新点之间不同段的函数来重复。

这是我刚刚整理的一些 Python 代码:

from __future__ import division
from random import shuffle

def make_graph(depth, graph, start, end, turns):
    # add points to graph
    graph.add(start)
    graph.add(end)

    if depth > 0:   
        # unpack input values
        fromtime, fromvalue = start
        totime, tovalue = end

        # calcualte differences between points
        diffs = []
        last_time, last_val = fromtime, fromvalue
        for t, v in turns:
            new_time = fromtime + (totime - fromtime) * t
            new_val = fromvalue + (tovalue - fromvalue) * v
            diffs.append((new_time - last_time, new_val - last_val))
            last_time, last_val = new_time, new_val

        # add 'brownian motion' by reordering the segments
        shuffle(diffs)

        # calculate actual intermediate points and recurse
        last = start
        for segment in diffs:
            p = last[0] + segment[0], last[1] + segment[1]
            make_graph(depth - 1, graph, last, p, turns)
            last = p
        make_graph(depth - 1, graph, last, end, turns)

from matplotlib import pyplot
depth = 8
graph = set()
make_graph(depth, graph, (0, 0), (1, 1), [(1/9, 2/3), (5/9, 1/3)])
pyplot.plot(*zip(*sorted(graph)))
pyplot.show()

这里是一些示例输出:

enter image description here

关于python - 如何在 python 中生成市场的分形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25067096/

相关文章:

python - 使用 django-kombu 的其他替代方法是什么?

python - 对pandas数据透视表中的月份列进行排序

python - 为什么每行后面都打印 None ?

c++ - 程序生成无缝分形噪声纹理

c - 低分辨率曼德尔布罗分形……分辨率不够高?

python - python是否具有用于交错生成器/序列的内置函数?

python - Appium 在 iOS(真实设备)上访问 URL 会遇到 "(' Connection aborted .', BadStatusLine("''",))"

python - 字典更新序列元素 #0 的长度为 15; 2 是必需的

processing - 如何找到 L-Systems 树上分支的开始/结束顶点? (p5.js)

python - 使用 Swagger 生成的新代码更新 Flask 代码