bsolomon1124 / pyfinance Goto Github PK
View Code? Open in Web Editor NEWPython package designed for general financial and security returns analysis.
Home Page: https://pypi.python.org/pypi/pyfinance
License: MIT License
Python package designed for general financial and security returns analysis.
Home Page: https://pypi.python.org/pypi/pyfinance
License: MIT License
description:
with pandas version: 1.3.4,there is a error when calling get_anlz_factor,which result in calling error when invoke function anlzd_stdev and sharpe_ratio
problem reason:
at function get_anlz_factor,the map PERIODS_PER_YEAR doesn't has correct key type int,it's FreqGroup.FR_ANN at pandas 1.3.4
possible solution:
use FreqGroup.value to get correction key.
I'm working with large datasets, which and I'm running different RollingOLS classes multiple times on it. However I've noticed that after running the class I would soon start running out of memory, as each pvalue I calculated would get stored on the LRUcache. I believe this is unnecessary, or at least an option should be implemented to remove the LRUCache entirely.
Thanks!
As you correctly said in comments formula is
Formula: `sqrt( sum([min(self - thresh, 0] **2 ) / (n - ddof) )`
Also known as: downside deviation.
so in the function --> line 867 of file returns.py:
ss = ((nansum(np.minimum(self - threshold, 0.) ** 2 )/ n) ** 0.5)
instead of:
ss = (nansum(np.minimum(self - threshold, 0.) ** 2) ** 0.5) / n
FutureWarning: Index.is_all_dates is deprecated, will be removed in a future version. check index.inferred_type instead
if self.index.is_all_dates:
Any thoughts on fixing this here?
Thanks in advance,
GN
The sample usage of PandasRollingOLS posted as an answer at https://stackoverflow.com/questions/37317727/deprecated-rolling-window-option-in-ols-from-pandas-to-statsmodels no longer works
I confirmed it wasn't a problem with my personal machine by running it on this online compiler
https://repl.it/@SamArthur/InfatuatedRubberyResources
Here's the error which was given on my personal machine as well as the online compiler
Traceback (most recent call last):
File "main.py", line 29, in <module>
model = PandasRollingOLS(y=y, x=x, window=window)
File "/home/runner/.site-packages/pyfinance/ols.py", line 738, in __init__
use_const=use_const)
File "/home/runner/.site-packages/pyfinance/ols.py", line 377, in __init__
self.solution = _rolling_lstsq(self.xwins, self.ywins)
File "/home/runner/.site-packages/pyfinance/ols.py", line 36, in _rolling_lstsq
return np.squeeze(np.matmul(np.linalg.inv(np.matmul(x.swapaxes(1, 2), x)),
File "/usr/local/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 532, in inv
ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
File "/usr/local/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 89, in _raise_linalgerror_singular
raise LinAlgError("Singular matrix")
numpy.linalg.linalg.LinAlgError: Singular matrix
I use git to pull the code, then I use the command python -m pip install -e .
then there is a error
(quant) E:\Python\quantaxis\pyfinance>python -m pip install -e .
Looking in indexes: http://pypi.douban.com/simple/
Obtaining file:///E:/Python/XXX/pyfinance
ERROR: Complete output from command python setup.py egg_info:
ERROR: Traceback (most recent call last):
File "<string>", line 1, in <module>
File "E:\Python\XXX\pyfinance\setup.py", line 8, in <module>
with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
File "E:\ProgramData\Anaconda3\envs\quant\lib\codecs.py", line 898, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\Python\\XXX\\pyfinance\\README.rst'
So I change the 'README.rst' to 'README.md', then is OK.
Even if you pass in use_const=False, the regression still appends and uses a constant. Tried tinkering to fix this but ran into dimensionality issues - some help would be appreciated.
Version: 1.2.4
Example:
>>> from pyfinance import ols
>>> data = {"A": [2, 3, 4, 5, 6], "B": [10, 11, 12, 13, 14]}
>>> import pandas as pd
>>> df = pd.DataFrame(data)
>>> rolling = ols.RollingOLS(y=df["B"], x=df["A"], window=3, has_const=False, use_const=False)
ValueError Traceback (most recent call last)
<ipython-input-6-b8d3bfa13f07> in <module>
----> 1 rolling = ols.RollingOLS(y=df["B"], x=df["A"], window=3, has_const=False, use_const=False)
.../pyfinance/ols.py in __init__(self, y, x, window, has_const, use_const)
373 use_const=use_const)
374 self.window = self.n = window
--> 375 self.xwins = utils.rolling_windows(self.x, window=window)
376 self.ywins = utils.rolling_windows(self.y, window=window)
377 self.solution = _rolling_lstsq(self.xwins, self.ywins)
.../pyfinance/utils.py in rolling_windows(a, window)
703 if window > a.shape[0]:
704 raise ValueError('Specified `window` length of {0} exceeds length of'
--> 705 ' `a`, {1}.'.format(window, a.shape[0]))
706 if isinstance(a, (Series, DataFrame)):
707 a = a.values
ValueError: Specified `window` length of 3 exceeds length of `a`, 1.
the following code cannot work and report ValueError. hope to see a better version soon, thx.
from pyfinance import ols
data = {'A':[2,3,4,5,6],'B':[10,11,12,13,14]}
df = pd.DataFrame(data)
rolling = ols.RollingOLS(y=df['B'], x=df['A'], window=3,has_const=False,use_const=False)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\utils.py", line 705, in rolling_windows
' a
, {1}.'.format(window, a.shape[0]))
ValueError: Specified window
length of 3 exceeds length of a
, 1.
Hi,
i was testing pyfinance, i'm using 1.2.1
i wish this will be helpful:
returns.py
line 617
diff = self.excess_ret(benchmark).anlzd_ret()
instead
diff = self.excess_ret(benchmark).anlzd_return()
in utils.py i added the function convertfreq
def convertfreq(freq):
freq = freq.upper()
if freq=='M':
periods = 12
elif freq=='A':
periods = 1
elif freq== 'Q':
periods = 4
else:
raise ValueError('freq must be equal to M,A,Q')
return periods
keyerror for function ts.anlzd_stdev() and ts.sharpe_ratio(ddof=1)
i would like to have min_window in the rollingOLS function, because if we have a window of 90 it does not perform OLS on first 90 values. i would like to perform a OLS expanding until 90 observations starting when there is at least 12 observation (min_window), then rolling of 90 (window)
Is it possible to implement that or someone has a quick workaround for this?
Thanks
First, thanks for fixing the problem and updating the package to 1.2.5.
When I use the newest version, I run into the following result. it seems to make no sense.
data = {'A':[2,3,4,5,6],'B':[10,11,12,13,14]}
df = pd.DataFrame(data)
rolling = ols.RollingOLS(y=df['B'], x=df['A'], window=3,has_const=False,use_const=False)
rolling.rsq
rolling.rsq_adj
the result is array([938.58741603, 868.32596507, 801.06451411])
array([-624.05827735, -577.21731005, -532.37634274])
it should in the interval [0,1]
hi Brad, just looked through your options pricing module.
It might be helpful in general to add tests for your various
functions. This assures the user that e.g. the condor
is written correctly for a specific inputs, and also serves
as demonstrated snippets of code.
Check out https://doc.pytest.org -- good starting point.
As you write the tests, the skeleton for documentation will
emerge. When you modify code, the tests will assure you
that everything works as intended. Take care!
I'm trying to use PandasRollingOLS but the class FreqGroup is not found, neither the get_freq_code.
I have looked at Panda and seems that the lib is in:
from pandas._libs.tslibs.dtypes import FreqGroup
instead of from pandas._libs.tslibs.frequencies import FreqGroup, get_freq_code
at utils.py line 80
If I do this change the lib works, just telling in case you want to change it, i'm not doing a PR because I don't know if that can affect something else, did really quick.
Cheers
First - thanks for this package, it sounds amazing.
The issue I'm facing is actually in installation - keep getting the following error after (installing via pip in anaconda):
!pip3 install pyfinance
Collecting pyfinance
Using cached https://files.pythonhosted.org/packages/74/20/7dd8767ec785cfcdce714e170a0a7b1c79feb7085e6217ec8dc43b4a74d3/pyfinance-1.1.1.tar.gz
Requirement already satisfied: beautifulsoup4>=4.6.0 in c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages (from pyfinance) (4.6.0)
Requirement already satisfied: matplotlib>=1.1 in c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages (from pyfinance) (2.1.0)
Requirement already satisfied: numpy>=0.7.0 in c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages (from pyfinance) (1.13.3)
Exception:
Traceback (most recent call last):
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages\pip_internal\basecommand.py", line 141, in main
status = self.run(options, args)
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages\pip_internal\commands\install.py", line 299, in run
resolver.resolve(requirement_set)
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages\pip_internal\resolve.py", line 102, in resolve
self._resolve_one(requirement_set, req)
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages\pip_internal\resolve.py", line 261, in _resolve_one
check_dist_requires_python(dist)
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\site-packages\pip_internal\utils\packaging.py", line 46, in check_dist_requires_python
feed_parser.feed(metadata)
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\email\feedparser.py", line 175, in feed
self._input.push(data)
File "c:\users\mi113\appdata\local\continuum\anaconda3\lib\email\feedparser.py", line 103, in push
self._partial.write(data)
TypeError: string argument expected, got 'NoneType'
You are using pip version 18.0, however version 18.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Right now, when I use the window = n, it requires me to have at least n entries without empty values. I believe it would be better if we use the amount of available data if the length is smaller than n. Right now it is returning NAN
I periodically attempt to keep up with bugfixes, but I unfortunately do not have the time to properly maintain this project beyond that.
If anyone is interested in adopting this project, I would be glad to transfer it if that means giving it a more caring home.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.