numpy Python:如何对均值和标准差进行Winsorize?

tf7tbtn2  于 2022-12-26  发布在  Python
关注(0)|答案(3)|浏览(216)

我有一个代码,用于多个不同的指标(例如,风险价值、Omega、Sortino等)。我使用的平均公式是:
均值(平均值):

e = numpy.mean(r)

return numpy.mean(diff) / vol(diff)

标准偏差:

return numpy.std(returns)

我想对计算中使用的均值(和标准差)进行winsorize。有人能告诉我如何做吗?我已经找到了这部分,但不确定如何实现它(如果这是它的话):
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mstats.winsorize.html
谢啦,谢啦

ubby3x7f

ubby3x7f1#

试试这个:

import os
import numpy as np

from scipy.stats.mstats import winsorize

file_location = input("path to file: ")
dirname = os.path.dirname(file_location)
filename = os.path.basename(file_location)

with open(file_location, 'r') as readfile, \
        open(os.path.join(dirname, 'win_' + filename), 'w') as writefile1, \
        open(os.path.join(dirname, 'mod_' + filename), 'w') as writefile2:
    writefile1.write('adj_mean,adj_std\n')
    for idx, line in enumerate(readfile):
        print("Reading line# {}...".format(idx))
        series = np.array([float(x) for x in line.split(',')])
        print("Read {} values...".format(len(series)))
        winsorized_series = winsorize(series, limits=[0.10, 0.10])
        print("Writing modified series to file...")
        writefile2.write(','.join(map(str, winsorized_series)) + '\n')
        adj_mean = np.mean(winsorized_series)
        adj_std = np.std(winsorized_series)
        print("adj mean and std dev...")
        writefile1.write("{},{}\n".format(adj_mean, adj_std))
bksxznpy

bksxznpy2#

要获得准确答案,需要MCVE
假设“e”是一个错误

import numpy as np
import scipy as sp
from scipy.stats.mstats import winsorize

e = np.random.rand(1,100)
print("{}".format(e))
winsorize(e, limits=(0.25,0.25), inplace=True)
print("{}".format(e))
vql8enpb

vql8enpb3#

import numpy as np
from scipy.stats.mstats import winsorize

def winsorized_mean(x, ql, qr):
    return np.mean(winsorize(x, limits=(ql, qr))

def winsorized_std(x, ql, qr):
    return np.std(winsorize(x, limits=(ql, qr))

相关问题