semvec/pypi-packages huggingface dataset:
PyFinanceLab 0.0.0
Financial applications focusing on portfolio management for Python
Requires Python: >=3.8, <4
Keywords: python,finance,portfolio-optimization,quantitative-finance,portfolio-management

# PyFinanceLab

PyFinanceLab is in alpha development. Please open an issue if you find any bugs. 

## Features

**PyFinanceLab** is a library of financial applications focusing on portfolio management for Python. 

* **Data Api Wrapper**
    
    The data api wrapper makes it easy to switch between [yfinance](https://github.com/ranaroussi/yfinance) (free) and [tia](https://github.com/bpsmith/tia) (Bloomberg terminal     subscription required) libraries for pulling financial data. 
    

* **Covariance Matrix Tester**
    
    The covariance matrix tester tests various risk models from Hudson and Thames's [PortfolioLab](https://hudsonthames.org/portfoliolab/) Python library (requires subscription)     for computing covariance matrices out-of-sample on daily price data. Each risk model is ranked according to how close the covariance matrix generated with that risk             model is to out-of-sample empirical covariance matrix. 
    

## Roadmap

Future development will include:

* **Classification Schema**

    Classify an investment universe of tickers into specified categories such as sector, size, or value. 


* **Constraints Modeling**

    Automatically generate weight constraints for a universe of tickers. 


* **Risk Modeling**

    Sample, test, and select the best risk model for generating covariance matrices for input into portfolio optimizers such as mean-variance optimization (MVO). Examples           include empirical covariance, ledoit-wolf shrinkage, minimum covariance determinant, and more. 


* **Return Modeling**

    Sample, test, and select the best return model for generating return vectors for input into portfolio optimizers such as mean-variance optimization (MVO). Examples include       mean historical return, exponentially-weighted return, and the Capital Asset Pricing Model (CAPM). 


* **Portfolio Optimization**

    Utilize the classification schema, constraints modeling, risk modeling, and return modeling to optimize a portfolio of assets. 
    

* **Portfolio Backtesting**

    Backtest portfolios and generate performance graphical plots and statistics. 


* **Report Generation**

    Report results in a nicely formatted and easily readable Excel file. 
    

* **Documentation**

    Documentation will be published as this Python library is further developed.
surveyweights 0.6
Apply Census weighting to survey data

## Surveyweights

Apply Census weighting to survey data.

### Example Usage

```Python
from surveyweights import run_weighting_scheme, run_weighting_iteration

# Define what to weigh on
weigh_on = ['age', 'education', 'gender', 'income', 'race', 'urban_rural', 'vote2016']

# Run weighting
output = run_weighting_scheme(survey_data, iters=25, weigh_on=weigh_on)

# Get data back with weight column
survey_data = output['final_df']

# See balance of weights 
run_weighting_iteration(survey_data, weigh_on=weigh_on)

# Look at unweighted outcome data
print(survey_data['outcome'].value_counts(normalize=True) * 100)

# Look at weighted outcome data
print(survey_data['outcome'].value_counts(normalize=True) * survey_data.groupby('outcome')['weight'].mean() * 100)
```


### Debugging

**Help! The percentages don't sum to 100%!**

If you subset the dataset, you subset the weights too and they will no longer work for the subsetted dataset. To fix this, use `nomalize_weights`:

```Python
# Subset df
subset_df = df[df[var] == subset]

# Look at weighted data (will be wrong and will not sum to 100%!)
print(subset_df[var].value_counts(normalize=True) * subset_df.groupby(var)['weight'].mean() * 100)

# Normalize weights
df['weight'] = nomalize_weights(df['weight'])

# Look at weighted data (it is now fixed and still representative!)
print(subset_df[var].value_counts(normalize=True) * subset_df.groupby(var)['weight'].mean() * 100)
```

~

**Help! The percentages still don't sum to 100% and I used `normalize_weights`!**

Another issue might be missing values. Try removing those.

```Python
df = df.dropna() # Remove NAs
df['weight'] = nomalize_weights(df['weight']) # Normalize weights

# Look at weighted data (it is now fixed and still representative!)
print(subset_df[var].value_counts(normalize=True) * subset_df.groupby(var)['weight'].mean() * 100)
```

Note that you may prefer to drop NAs just for particular columns of interest, or you may prefer to impute NAs with a particular value.


~

**Help! Re-running changes my results!**

The results should be deterministic, so re-running should not affect results. However, the weights still might be unstable and running the same weights in a different order could affect results. To fix this, try increasing the number of iterations and turning off early termination. Also, keep in mind that fluctuations of ~0.1 percentage point could be very normal - potentially a larger fluctuation for very small sample sizes.


### Installation

`pip3 install surveyweights`