Example #1
0
#!/usr/bin/env python

import numpy as np
from statsmodels.sandbox.pca import Pca

x=np.random.randn(1000)
y=x*2.3+5+np.random.randn(1000)
z=x*3.1+2.1*y+np.random.randn(1000)/2

#create the Pca object - requires a p x N array as the input
p=Pca((x,y,z))
print('energies:',p.getEnergies())
print('vecs:',p.getEigenvectors())
print('projected data',p.project(vals=np.ones((3,10))))


#p.plot2d() #requires matplotlib
#from matplotlib import pyplot as plt
#plt.show() #necessary for script

#p.plot3d() #requires mayavi
Example #2
0
mthdata["tr_empl_gap"] = px.Series(tr_empl_gap, index=index)
mthdata["hp_ch"] = px.Series(hp_ch, index=index)

mthdata["tr_empl_gap_perc"] = mthdata["tr_empl_gap"] / mthdata["hp_ch"]

# output
output = mthdata.reindex(columns=["unemployment_seas", "indust_prod_seas", "help_wanted_index"]).dropna()
output["empl_gap"] = mthdata["tr_empl_gap_perc"]

# normalize each output value to zero mean and unit variance
for var in output.columns:
    output[var + "_norm"] = (output[var] - output[var].mean()) / output[var].std()
output = output.filter(regex=".*_norm")

# retrieve first PC
output_pca = Pca(data=output.values.T, names=output.columns.tolist())
output_pca1_data = output_pca.project(nPCs=1)
output["output_pca1"] = px.Series(output_pca1_data.T.tolist()[0], index=output.index)

# prices
prices = px.DataFrame(index=index)
prices["CPI_infl"] = mthdata["CPI_seas"].diff(periods=12) / mthdata["CPI_seas"] * 100
prices["PPI_infl"] = mthdata["PPI_seas"].diff(periods=12) / mthdata["CPI_seas"] * 100

# normalize each price value to zero mean and unit variance
for var in prices.columns:
    prices[var + "_norm"] = (prices[var] - prices[var].mean()) / prices[var].std()
prices = prices.filter(regex=".*_norm").dropna()

# retrieve first PC
prices_pca = Pca(data=prices.values.T, names=prices.columns.tolist())
Example #3
0
#!/usr/bin/env python

import numpy as np
from statsmodels.sandbox.pca import Pca

x = np.random.randn(1000)
y = x * 2.3 + 5 + np.random.randn(1000)
z = x * 3.1 + 2.1 * y + np.random.randn(1000) / 2

#create the Pca object - requires a p x N array as the input
p = Pca((x, y, z))
print('energies:', p.getEnergies())
print('vecs:', p.getEigenvectors())
print('projected data', p.project(vals=np.ones((3, 10))))

#p.plot2d() #requires matplotlib
#from matplotlib import pyplot as plt
#plt.show() #necessary for script

#p.plot3d() #requires mayavi
Example #4
0
mthdata['tr_empl_gap_perc'] = mthdata['tr_empl_gap'] / mthdata['hp_ch']

#output
output = mthdata.reindex(
    columns=['unemployment_seas', 'indust_prod_seas', 'help_wanted_index'
             ]).dropna()
output['empl_gap'] = mthdata['tr_empl_gap_perc']

#normalize each output value to zero mean and unit variance
for var in output.columns:
    output[var + "_norm"] = (output[var] - output[var].mean()) / \
                                output[var].std()
output = output.filter(regex=".*_norm")

#retrieve first PC
output_pca = Pca(data=output.values.T, names=output.columns.tolist())
output_pca1_data = output_pca.project(nPCs=1)
output['output_pca1'] = px.Series(output_pca1_data.T.tolist()[0],
                                  index=output.index)

#prices
prices = px.DataFrame(index=index)
prices['CPI_infl'] = mthdata['CPI_seas'].diff(periods=12)/ \
       mthdata['CPI_seas'] * 100
prices['PPI_infl'] = mthdata['PPI_seas'].diff(periods=12)/ \
       mthdata['CPI_seas'] * 100

#normalize each price value to zero mean and unit variance
for var in prices.columns:
    prices[var + "_norm"] = (prices[var] - prices[var].mean()) / \
                                prices[var].std()