Numpy错误ValueError:scale< = 0

时间:2015-09-05 00:45:21

标签: python numpy

我正在运行下面的Python代码并从numpy

获取错误

文件“C:\ Users \ Krzysztof \ PycharmProjects \ PSO \ AssetSimulator.py”,第20行,在wiener_process中     return nrand.normal(loc = 0,scale = sqrt_delta_sigma,size = self.time)   在mtrand.RandomState.normal(numpy \ random \ mtrand \ mtrand.c:13649)中输入“mtrand.pyx”,第1557行 ValueError:scale< = 0

任何想法?我有numpy 1.9.2

Krzysztof

import math
import numpy
import pandas
import numpy.random as nrand



class AssetSimulator:
    def __init__(self, delta, sigma, mu, time):
        self.delta = delta
        self.sigma = sigma
        self.time = time
        self.mu = mu

    def wiener_process(self):
        sqrt_delta_sigma = math.sqrt(self.delta) * self.sigma
        return nrand.normal(loc=0, scale=sqrt_delta_sigma, size=self.time)

和这里的跑步者功能,我不认为西格玛是负面的

def runner_all(n, sigma, delta, mu, time, iterations, simulations, path, ce, cb, le, lb):
    print("Experiment", path, "starting")
    asset_simulator = AssetSimulator(delta, sigma, mu, time)

    Portfolio.memoizer = {}
    none, penalty, lagrange, repair, preserve, ss = [], [], [], [], [], 30
    none_ve, penalty_ve, lagrange_ve, repair_ve, preserve_ve = [], [], [], [], []
    none_vb, penalty_vb, lagrange_vb, repair_vb, preserve_vb = [], [], [], [], []

    for i in range(simulations):
        print("Simulation", i, "starting")
        asset_returns = asset_simulator.assets_returns(n)
        corr = pandas.DataFrame(asset_returns).transpose().corr()
        # three_dimensional_landscape(asset_returns, corr, 100)

        none_opt = BarebonesOptimizer(ss, asset_returns, corr)
        result, violation_e, violation_b = none_opt.optimize_none(iterations + 1, ce, cb, le, lb)
        none_ve.append(violation_e)
        none_vb.append(violation_b)
        none.append(result)
        print("\tAlgorithm 1 Done")

        lagrange_opt = BarebonesOptimizer(ss, asset_returns, corr)
        result, violation_e, violation_b = lagrange_opt.optimize_penalty(iterations + 1, ce, cb, le, lb)
        penalty_ve.append(violation_e)
        penalty_vb.append(violation_b)
        penalty.append(result)
        print("\tAlgorithm 2 Done")

        lagrange_opt = BarebonesOptimizer(ss, asset_returns, corr)
        result, violation_e, violation_b = lagrange_opt.optimize_lagrange(iterations + 1, ce, cb, le, lb)
        lagrange_ve.append(violation_e)
        lagrange_vb.append(violation_b)
        lagrange.append(result)
        print("\tAlgorithm 3 Done")

        repair_opt = BarebonesOptimizer(ss, asset_returns, corr)
        result, violation_e, violation_b = repair_opt.optimize_repair(iterations + 1, ce, cb, le, lb)
        repair_ve.append(violation_e)
        repair_vb.append(violation_b)
        repair.append(result)
        print("\tAlgorithm 4 Done")

        preserve_opt = BarebonesOptimizer(ss, asset_returns, corr)
        result, violation_e, violation_b = preserve_opt.optimize_preserving(iterations + 1, ce, cb, le, lb)
        preserve_ve.append(violation_e)
        preserve_vb.append(violation_b)
        preserve.append(result)
        print("\tAlgorithm 5 Done")

    n_r, n_ve, n_vb = pandas.DataFrame(none), pandas.DataFrame(none_ve), pandas.DataFrame(none_vb)
    r_r, r_ve, r_vb = pandas.DataFrame(repair), pandas.DataFrame(repair_ve), pandas.DataFrame(repair_vb)
    p_r, p_ve, p_vb = pandas.DataFrame(preserve), pandas.DataFrame(preserve_ve), pandas.DataFrame(preserve_vb)
    pr_r, pr_ve, pr_vb = pandas.DataFrame(penalty), pandas.DataFrame(penalty_ve), pandas.DataFrame(penalty_vb)
    l_r, l_ve, l_vb = pandas.DataFrame(lagrange), pandas.DataFrame(lagrange_ve), pandas.DataFrame(lagrange_vb)

    n_r.to_csv(path + "/None Fitness.csv")
    n_ve.to_csv(path + "/None Equality.csv")
    n_vb.to_csv(path + "/None Boundary.csv")

    r_r.to_csv(path + "/Repair Fitness.csv")
    r_ve.to_csv(path + "/Repair Equality.csv")
    r_vb.to_csv(path + "/Repair Boundary.csv")

    p_r.to_csv(path + "/Preserve Fitness.csv")
    p_ve.to_csv(path + "/Preserve Equality.csv")
    p_vb.to_csv(path + "/Preserve Boundary.csv")

    pr_r.to_csv(path + "/Penalty Fitness.csv")
    pr_ve.to_csv(path + "/Penalty Equality.csv")
    pr_vb.to_csv(path + "/Penalty Boundary.csv")

    l_r.to_csv(path + "/Lagrangian Fitness.csv")
    l_ve.to_csv(path + "/Lagrangian Equality.csv")
    l_vb.to_csv(path + "/Lagrangian Boundary.csv")

    plot_results([n_r.mean(), r_r.mean(), pr_r.mean(), l_r.mean(), p_r.mean()],
                 ["A1 (No Method)", "A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Fitness f()", path + "/1 Fitness")

    plot_results([r_r.mean(), pr_r.mean(), l_r.mean(), p_r.mean()],
                 ["A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Fitness f()", path + "/1 Fitness Ex None")

    plot_results([n_ve.mean(), r_ve.mean(), pr_ve.mean(), l_ve.mean(), p_ve.mean()],
                 ["A1 (No Method)", "A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Equality Constraint Violation, C_E()", path + "/2 Equality Violations")

    plot_results([r_ve.mean(), pr_ve.mean(), l_ve.mean(), p_ve.mean()],
                 ["A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Equality Constraint Violation, C_E()", path + "/2 Equality Violations Ex None")

    plot_results([n_vb.mean(), r_vb.mean(), pr_vb.mean(), l_vb.mean(), p_vb.mean()],
                 ["A1 (No Method)", "A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Boundary Constraint Violation, C_B()", path + "/3 Boundary Violations")

    plot_results([r_vb.mean(), pr_vb.mean(), l_vb.mean(), p_vb.mean()],
                 ["A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Boundary Constraint Violation, C_B()", path + "/3 Boundary Violations Ex None")

    plot_results([n_r.std(), r_r.std(), pr_r.std(), l_r.std(), p_r.std()],
                 ["A1 (No Method)", "A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Fitness Standard Deviation f()", path + "/4 Fitness Stdev")

    plot_results([r_r.std(), pr_r.std(), l_r.std(), p_r.std()],
                 ["A2 (Particle Repair Method)", "A3 (Penalty Function Method)",
                  "A4 (Augmented Lagrangian Method)", "A5 (Preserving Feasibility Method)"],
                 "Average Global Best Fitness Standard Deviation f()", path + "/4 Fitness Stdev Ex None")


def surface_plotter(n, sigma, delta, mu, time, c_e, c_b, m_e, m_b):
    asset_simulator = AssetSimulator(delta, sigma, mu, time)
    asset_returns = asset_simulator.assets_returns(n)
    corr = pandas.DataFrame(asset_returns).transpose().corr()
    three_dimensional_landscape(asset_returns, corr, 200, c_e, c_b, m_e, m_b)


def run():
    matplotlib.rc('font', family='Arial')
    coeff_e, coeff_b, lagrange_e, lagrange_b = 2.0, 2.0, 0.5, 0.5
    runner_all(4, 0.125, float(1 / 252), 0.08, 500, 80, 60, "Results (A)", coeff_e, coeff_b, lagrange_e, lagrange_b)
    runner_all(8, 0.125, float(1 / 252), 0.08, 500, 80, 60, "Results (B)", coeff_e, coeff_b, lagrange_e, lagrange_b)
    runner_all(16, 0.125, float(1 / 252), 0.08, 500, 80, 60, "Results (C)", coeff_e, coeff_b, lagrange_e, lagrange_b)


if __name__ == '__main__':
    run()

1 个答案:

答案 0 :(得分:2)

我假设您正在使用Python 2,其中1/252将评估为0,除非模块中有.apk。而不是那样,你可以使用1. / 252,你有1/252。最小错误示例:

from __future__ import division

给出

import math
import numpy.random as nrand

class AssetSimulator:
    def __init__(self, delta, sigma, mu, time):
        self.delta = delta
        self.sigma = sigma
        self.time = time
        self.mu = mu

    def wiener_process(self):
        sqrt_delta_sigma = math.sqrt(self.delta) * self.sigma
        print sqrt_delta_sigma
        return nrand.normal(loc=0, scale=sqrt_delta_sigma, size=self.time)

def runner_all(n, sigma, delta, mu, time, iterations, simulations, path, ce, cb, le, lb):
    print("Experiment", path, "starting")
    asset_simulator = AssetSimulator(delta, sigma, mu, time)
    asset_simulator.wiener_process()

def run():
    coeff_e, coeff_b, lagrange_e, lagrange_b = 2.0, 2.0, 0.5, 0.5
    runner_all(4, 0.125, float(1 / 252), 0.08, 500, 80, 60, "Results (A)", coeff_e, coeff_b, lagrange_e, lagrange_b)

if __name__ == '__main__':
    run()

,而

('Experiment', 'Results (A)', 'starting')
0.0
Traceback (most recent call last):
  File "problem2.py", line 26, in <module>
    run()
  File "problem2.py", line 23, in run
    runner_all(4, 0.125, float(1 / 252), 0.08, 500, 80, 60, "Results (A)", coeff_e, coeff_b, lagrange_e, lagrange_b)
  File "problem2.py", line 19, in runner_all
    asset_simulator.wiener_process()
  File "problem2.py", line 14, in wiener_process
    return nrand.normal(loc=0, scale=sqrt_delta_sigma, size=self.time)
  File "mtrand.pyx", line 1487, in mtrand.RandomState.normal (numpy/random/mtrand/mtrand.c:9958)
ValueError: scale <= 0

给出

import math
import numpy.random as nrand

class AssetSimulator:
    def __init__(self, delta, sigma, mu, time):
        self.delta = delta
        self.sigma = sigma
        self.time = time
        self.mu = mu

    def wiener_process(self):
        sqrt_delta_sigma = math.sqrt(self.delta) * self.sigma
        print sqrt_delta_sigma
        return nrand.normal(loc=0, scale=sqrt_delta_sigma, size=self.time)

def runner_all(n, sigma, delta, mu, time, iterations, simulations, path, ce, cb, le, lb):
    print("Experiment", path, "starting")
    asset_simulator = AssetSimulator(delta, sigma, mu, time)
    asset_simulator.wiener_process()

def run():
    coeff_e, coeff_b, lagrange_e, lagrange_b = 2.0, 2.0, 0.5, 0.5
    runner_all(4, 0.125, float(1. / 252), 0.08, 500, 80, 60, "Results (A)", coeff_e, coeff_b, lagrange_e, lagrange_b)

if __name__ == '__main__':
    run()