def readData(fileAddress): with open(fileAddress, newline='') as csvfile: data = csv.reader(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_NONNUMERIC) dataList = list(data) ages = dataList[0] heights = dataList[1] if (len(dataList) >= 3): sampleSizes = dataList[2] stdDeviations = dataList[3] return CurveFit.Data(ages, heights, stdDeviations/np.sqrt(sampleSizes), system_error=0.1/np.sqrt(3)) else: return CurveFit.Data(ages, heights, np.array([1.] * len(ages)))
def createBasinPointsPlane(self,p,v): import matplotlib.pyplot as plt self.planes =CurveFit.Planes() pointCnt = 200 self.xp = np.empty(pointCnt) self.yp = np.empty(pointCnt) self.zp = np.empty(pointCnt) for i in range(pointCnt): self.xp[i] = np.random.random_integers(0,self.xlen) self.yp[i] = np.random.random_integers(0,self.ylen) self.zp[i] = self.planes.depth(p,v,self.xp[i],self.yp[i]) self.cf = CurveFit.curveFit(self.xp, self.yp, self.zp, self.res) self.tck = self.cf.fitSurface() self.cf.createRegGrid()
def addRandomError(data, sigma, sampleSize, minAge, maxAge): new_x_arr = [] new_y_arr = [] new_variance_arr = [] for index in range(len(data.x_array)): age = data.x_array[index] if (age >= minAge and age <= maxAge and not age.is_integer()): new_x_arr.append(age) new_y_arr.append( random.gauss(data.y_array[index], sigma / np.sqrt(sampleSize))) new_variance_arr.append(sigma * generateChiSquare(sampleSize - 1) / sampleSize) return CurveFit.Data(new_x_arr, new_y_arr, new_variance_arr / np.sqrt(sampleSize))
def createBasinManual(self): self.createBasinPoints() # x = [294.0, 141.0, 252.0, 426.0, 178.0, 19.0, 499.0, 63.0, 321.0, 162.0] # y = [422.0, 408.0, 146.0, 264.0, 430.0, 19.0, 431.0, 284.0, 63.0, 56.0] # z = [3.81, 6.43, 6.00, 6.97, 4.4, 2.49, 0.1, 2.31, 4.98, 0] x = [29.40, 14.10, 25.20, 42.60, 17.80, 1.90, 49.90, 6.30, 32.10, 16.20] y = [42.20, 40.80, 14.60, 26.40, 43.00, 1.90, 43.10, 28.40, 6.30, 5.60] z = [3.81, 6.43, 6.00, 6.97, 4.4, 2.49, 0.1, 2.31, 4.98, 0] #fig3 = plt.figure() #ax = fig3.add_subplot(111, projection='3d') #ax.scatter(x[:],y[:],z[:], c='r', marker='.') for i in range(self.xtrapoints): # print i, self.xp[i+self.xlen*2], self.yp[i+self.xlen*2], self.zp[i+self.xlen*2] self.xp[i+self.xlen*2] = x[i] self.yp[i+self.xlen*2] = y[i] self.zp[i+self.xlen*2] = z[i] # print i, self.xp[i+self.xlen*2], self.yp[i+self.xlen*2], self.zp[i+self.xlen*2] self.cf = CurveFit.curveFit(self.xp, self.yp, self.zp, self.res) self.tck = self.cf.fitSurface() self.cf.createRegGrid() self.cf.clamp(self.minz,self.maxz)
import CurveFit as cf import matplotlib.pyplot as plt # QUESTAO 1 # a) plota a dispersao dos dados------------------------------------------------- xi = np.array([1872, 1890, 1900, 1920, 1940, 1950, 1960, 1970, 1980, 1991, 1996]) yi = np.array([9.9, 14.3, 17.4, 30.6, 41.2, 51.9, 70.2, 93.1, 119.0, 146.2, 157.1]) plt.plot(xi, yi, 'ro') # --------------------------------------------------------------------------------- # b) calcula a funcao quadrática e os resultados------------------------------------ resultsp, polynomial = cf.fitpolynomial(xi, yi, 2) # Cria a linha da funcao e plota xx = np.linspace(xi[0], xi[-1]) plt.plot(xx, np.polyval(polynomial,xx),'b-') # ----------------------------------------------------------------------------------- # c) calcula a funcao exponencial(A e b) e os resultados ---------- ----------------- resultse, A, b = cf.fitexponencial(xi, yi) # Cria a linha da funcao e plota usando a Ae^(b*x) xx = np.linspace(xi[0], xi[-1]) plt.plot(xx, A * np.exp(b*xx),'g-') plt.grid();plt.show()
def createBasin(self, flat = False, manPoints = [], edgePoints = 10): self.createBasinPoints(flat, manPoints, edgePoints) self.cf = CurveFit.curveFit(self.xp, self.yp, self.zp, self.res) self.tck = self.cf.fitSurface() self.cf.createRegGrid() self.cf.clamp(self.minz,self.maxz)
def autoDayLoop(broker, connection): """ autoDayLoop: automatically buys and sells for the day broker - broker name to run as connection - database connection object return: none """ # fetch broker info [name, bank, date] and owned stocks brokerInfo = fetchSqlData.fetchBrokerInfo(broker, connection) ownedStocks = fetchSqlData.queryAllOwned(broker, connection) today = brokerInfo[2] quarterDate = brokerInfo[2] - timedelta(days=90) yearDate = brokerInfo[2] - timedelta(days=365) # check to sell if ownedStocks: # get 90 days ago for stocks in ownedStocks: # get 60 day pointset points = fetchSqlData.getPointsDateRange(quarterDate, today, stocks[0], ["Date", "AdjClose"], connection) kmeans = CurveFit.getKmean(points) # if its above high kmean sellall if points[-1][1] > kmeans[0]: sellStock(brokerInfo, stocks[0], stocks[1], connection) print(stocks, "SOLD") # get stock list stockList = fetchSqlData.getStockList(connection) highScores = [] totalNumStocks = len(stockList) onStockNum = 0 oldfraction = 0 loadingBar(0) # score all stocks for stocks in stockList: onStockNum += 1 #print(stocks[0]) # if stock exists on dates if fetchSqlData.stockDateExists( today, stocks[0], connection) and fetchSqlData.stockDateExists( yearDate, stocks[0], connection): # load pointests pointSet = fetchSqlData.getPointsDateRange(yearDate, today, stocks[0], ["Date", "AdjClose"], connection) secondSet = fetchSqlData.getPointsDateRange( quarterDate, today, stocks[0], ["Date", "AdjClose"], connection) # score and append if higher score = CurveFit.compositeScore(pointSet, secondSet) CurveFit.appendHigher( [stocks, score[0], score[1], score[2], pointSet[-1][1]], highScores, 10) fraction = int((onStockNum / totalNumStocks) * 50) if oldfraction != fraction: loadingBar(fraction) oldfraction = fraction stdout.write("\r") stdout.flush() # check for which stock to buy for goodStocks in highScores: #newSet = fetchSqlData.getPointsDateRange(quarterDate, today, goodStocks[0][0], ["Date","AdjClose"], connection) #newPoly = CurveFit.polyFit(newSet,9) #CurveFit.drawPoints(newSet,newPoly) #newKmean = CurveFit.getKmean(newSet) # buy if below kmean if goodStocks[2] > goodStocks[4] and goodStocks[3] > goodStocks[4]: # check bank for how much to buy brokerInfo = fetchSqlData.fetchBrokerInfo(broker, connection) totalMoney = (valueStocks(broker, brokerInfo[2], connection) + brokerInfo[1]) / 10 toBuy = int(totalMoney / goodStocks[4]) if toBuy > 1000: toBuy = 1000 # Buy stock if toBuy > 0: if buyStock(brokerInfo, goodStocks[0][0], toBuy, connection): print(goodStocks, toBuy) # Done if stock was bought break