#============================================================================= # FILES #============================================================================= file_star = 'star_luminos.txt' #============================================================================= # LOAD DATA #============================================================================= sData = np.genfromtxt(file_star, skip_header=1, usecols=(0, 1)).T s_temp, s_lumi = sData[0], sData[1] print(s_temp, s_lumi) #============================================================================= # FIT DATA #============================================================================= sLS = utils.lin_LS(np.log10(abs(s_temp)), np.log10(s_lumi)) for tag, item in sLS.items(): if isinstance(item, (int, float)): print(tag, item) s_yhat = 10**sLS['a'] * (s_temp**sLS['b']) #============================================================================= # PLOTS #============================================================================= plt.figure(1) ax1 = plt.subplot(111) ax1.loglog(s_temp, s_lumi, 'ko', ms=1, mew=1, mfc='none', label='obs.') ax1.plot(s_temp, s_yhat, 'r--',
L = Luminosity """ a = 10 #alpha random number b = 15 #beta random number #============================= # Data files #============================= file_in = 'star_luminos.txt' T, L = np.genfromtxt(file_in).T #============================= a_x = np.linspace(10, 1000, 9981) a_y = a * T**b #=================================================================================== # data fits #=================================================================================== dLS = utils.lin_LS(a_x, a_y) for tag, item in dLS.items(): if isinstance(item, (int, float)): print(tag, item) print(dLS['R2'], dLS['r_p']**2) ## same result with scipy slope, intercept, r_p, prob, stderr = scipy.stats.linregress(a_x, a_y) print('scipy', slope, intercept, r_p) #=================================================================================== # plots #=================================================================================== plt.figure(1) ax1 = plt.subplot(111) ax1.plot(a_x, a_y, 'ko', ms=5, mew=1, mfc='none') ax1.plot(a_x,
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt import opt_utils as opt_utils #=============== Data/ Data Import ===============# file_eq = 'star_luminos.txt' mData = np.loadtxt(file_eq).T #=============== Power Law Fitting ===============# x = np.log10(mData[0]) y = np.log10(mData[1]) #=============== Power Law Fitting ===============# l_Ls = opt_utils.lin_LS(x, y) T = np.arange(10, 1000) ay_hat = (l_Ls['a']) * T * l_Ls['b'] plt.figure() plt.plot(mData[0], mData[1], 'bo') plt.loglog(mData[0], mData[1], 'g-') #plt.loglog(mData[0], ay_hat, 'ro', mfc = 'none')
@author: maduong """ import numpy as np import matplotlib.pyplot as plt import opt_utils as opt_utils #============================================================================== # files, params #============================================================================== file_in = 'star_luminos.txt' mData = np.genfromtxt(file_in).T Tmin, Tmax = 10, 1000 sel_T = np.logical_and(mData[0] >= Tmin, mData[0] <= Tmax) dLS = opt_utils.lin_LS(mData[0][sel_T], mData[1][sel_T]) print dLS['b'] #============================================================================== # plots #============================================================================== plt.figure(1) plt.title('Temperature versus Luminosity Graph') plt.plot(mData[0], mData[1], 'ko') plt.xlabel('Temperature (Degree C)') plt.ylabel('Luminosity (Solar Units)') plt.figure(2) plt.title('Logarithmic Temperature versus Luminosity Graph') plt.loglog(mData[0][sel_T], mData[1][sel_T], 'bo',
np.random.seed(123456) tmin, tmax = 10, 1000 N = 1000 alpha = 0.5 # i dont know what alpha or whats beta are so im picking random numbers beta = 2 sigma = 2.1 at = np.linspace(tmin, tmax, N) ay = alpha * at**beta + np.random.randn(N) * sigma file_in = 'star_luminos.txt' mData = np.genfromtxt(file_in, usecols=(0, 1), skip_header=1).T F_mdata = ou.lin_LS(at, ay) slope, intercept, r_p, prob, stderr = scipy.stats.linregress(at, ay) print('scipy', slope, intercept, r_p) #print(mData) plt.figure(1) ax1 = plt.subplot(111) ax1.plot(at, ay, 'ko', ms=1, mew=1, mfc='none') plt.xlabel('Temperature') plt.ylabel('Luminosity') ax1.legend(loc='upper right') plt.show()
# import #===================================== import numpy as np import matplotlib.pyplot as plt import opt_utils #===================================== # definitions #===================================== file_star = 'star_luminos.txt' mData = np.loadtxt(file_star).T dDic = opt_utils.lin_LS( mData[0], mData[1]) a_y = dDic['a']*mData[0]**dDic['b'] #=================================== # parameters #=================================== Tmin, Tmax = 10, 1000 #=================================== # plot #=================================== mData[0] = np.linspace( Tmin, Tmax, 9981) plt.figure( 1) plt.plot( mData[0], mData[1], 'ko')
# ============================================================================= file_in = 'star_luminos.txt' Tmin, Tmax = 10, 1000 # ============================================================================= # Load and select data # ============================================================================= Data = np.loadtxt(file_in).T sel = np.logical_and(Data[0] > Tmin, Data[0] < Tmax) # ============================================================================= # Linear fit # ============================================================================= dLS = opt.lin_LS(np.log10(Data[0][sel]), np.log10(Data[0][sel])) # ============================================================================= # Plotting # ============================================================================= plt.rcParams.update({'font.size': 12}) plt.plot(np.log10(Data[0][sel]), np.log10(Data[0][sel]), 'r+') plt.plot(np.log10(Data[0][sel]), dLS['Y_hat'], 'b-') plt.xlabel("log(T)") plt.ylabel("log(L)") plt.legend(('Data', 'Fit:y = %s*x+ %s\nbeta = %s\nalpha = %s' % (dLS['b'], dLS['a'], dLS['b'], 10**dLS['a']))) plt.title("Star Temperature vs Luminocity") plt.savefig('./Problem1fig') plt.show()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # data fit #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #select data within 10 < temp < 10 seltemp = np.logical_and(temp > 10, temp < 1000) #updated data set sel_data = star_data.T[seltemp].T temp = sel_data[0] lumos = sel_data[1] #fitting data using least squares fit fit_data = opt_utils.lin_LS(temp, lumos) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # plots #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #plot of the data plt.title('star temp and luminosity') plt.loglog(temp, lumos, label='lumos vs temp') #plot of the fitted data plt.loglog(temp, fit_data['Y_hat'], label='fitted lumos vs temp') plt.xlabel('temp (degree C)') plt.ylabel('lumos (solar units)') plt.legend()
# -*- coding: utf-8 -*- """ Created on Wed Apr 24 08:24:51 2019 @author: maduong """ import numpy as np import matplotlib.pyplot as plt import opt_utils Plan_Dist = np.genfromtxt('planet_distance.txt', usecols=(1, 2)) x = Plan_Dist[:, 0] y = Plan_Dist[:, 1] #plt.loglog(x,y) #plt.show() dDic = opt_utils.lin_LS(np.log10(x), np.log10(y)) print dDic a_yhat = 10**(dDic['a'] * x**dDic['b']) b = dDic['b'] print b
#================== Data ====================================================== #import data mData = np.genfromtxt(inFile, skip_header=1).T Temp = mData[0] luminosity = mData[1] # Change data so it only includes temperatures between 10K and 1000K sel = np.logical_and(Temp > 10, Temp < 1000) T = Temp[sel] L = luminosity[sel] #============= best fit ======================================================= #Find the line of best fit dLS = opt.lin_LS(T, L) print(dLS) #============================ plot ============================================ plt.figure(1) plt.subplot(211) plt.loglog(T, L, 'ro', markersize=0.5, label='Data') plt.ylabel('Luminosity (Solar Units)') plt.legend(loc='lower left') plt.subplot(212) plt.plot(T, dLS['Y_hat'], 'k-', label='Best fit line') plt.xlabel('Temperature (K)') plt.ylabel('Luminosity (Solar Units)')
Midterm #1 Star Luminosity """ import numpy as np import matplotlib.pyplot as plt import opt_utils as ou #=================================== # Load Data #=================================== data_dir = 'C:\Users\conma\OneDrive\ASTR 119\My programs\Midterm' file_in = 'star_luminos.txt' m_Data = np.genfromtxt(file_in, usecols=(0, 1), skip_header=1).T #Load data file #=================================== # Variables #=================================== Temp = m_Data[0] Lumin = m_Data[1] Tmin, Tmax = 0, 1000 least_sqr = ou.lin_LS(Temp, Lumin) #pl_fit = plt.plot(Temp, Lumin, 'ko', label='Raw Data') plt.plot(Temp, least_sqr(4), 'r-', label='Best Fit') plt.legend() plt.show() print least_sqr
import numpy as np import matplotlib.pyplot as plt import os import opt_utils as utils # Importing data file data_file = 'star_luminos.txt' mData = np.loadtxt(data_file).T aTemp, aLumi = mData[0], mData[1] xmin, xmax = 10, 1000 dLS = utils.lin_LS(aTemp, aLumi) a_yhat = dLS['a'] + (dLS['b'] * aTemp) plt.figure(1) ax1 = plt.subplot(111) ax1.plot(aTemp, aLumi, 'ko', ms=2, mew=1, mfc='none', label='scatterplot') ax1.plot(aTemp, a_yhat, 'r--', label='L = a * T^b ') ax1.set_xlabel('Temperature [Degrees Celsius]') ax1.set_ylabel('Luminosity [Solar Units]') ax1.set_xlim(10, 1000) ax1.set_ylim(0, 1000) ax1.legend() plt.show()
# -*- coding: utf-8 -*- """ Created on Wed May 8 08:23:48 2019 @author: kardalto """ import numpy as np import matplotlib.pyplot as plt import opt_utils data_star_lum = np.genfromtxt('star_luminos.txt').T afterFitting = opt_utils.lin_LS(data_star_lum[0], data_star_lum[1]) a = 515.9896349043293 b = -0.24207087442733877 fitLum = [] for i in range(10, 1001): lumosity = a * (data_star_lum[0][i]**b) fitLum.append(lumosity) plt.figure(1) plt.subplot(211) plt.plot(data_star_lum[0], data_star_lum[1], 'ko', ms=2) plt.title('Star Lumosity Data') plt.xlabel('Temperature(degree C)') plt.ylabel('Lumosity (solar units)') plt.subplot(212) plt.plot(data_star_lum[0][10:1001], fitLum, 'bo', ms=2) plt.xlabel('Temperature(degree C)')
import numpy as np import matplotlib.pyplot as plt import opt_utils #import data file_in = 'data/star_luminos.txt' mData = np.genfromtxt(file_in).T T = mData[0] L = mData[1] #use opt_utils to find power law fit dLS = opt_utils.lin_LS(T,np.log(L)) L_yhat = np.exp( dLS['a']) * (np.exp( dLS['b']*T)) #plot everything plt.figure() plt.title('Power law exponent:' + str(dLS['R2'])) ax1 = plt.subplot(111) ax1.plot(T,L,'ko',ms=5,mew=1.5,mfc='none',label='Observed data') ax1.plot(T,L_yhat, 'r-', label = 'Model fit') ax1.legend(loc=1) plt.xlabel('Temperature') plt.ylabel('Luminosity') plt.show() plt.savefig('Midterm_1_plot.png')
ax.set_xlabel('Time [dec. year]') ax.set_ylabel('events/day') plt.show() #--------------------------3--------------------------------------------- # power-law fitting #------------------------------------------------------------------------ # TODO: subtract time of mainshock (at_days[MS_ID]) from aftershock times (at_days[MS_ID+1??]- replace ??) - new vector has only AS with time relative to MS in days at_AS = at_days[MS_ID + 1::] - at_days[MS_ID] # TODO: compute AS rates at_bin_AS, aN_bin_AS = seis_utils.eqRate(at_AS, dPar['k']) # select events within tmin and tmax for which the data can be described by a powerlaw sel_t = np.logical_and(at_bin_AS >= dPar['tmin'], at_bin_AS <= dPar['tmax']) # TODO: fot the powerlaw to the log-transformed data between tmin and tmax dPL = opt_utils.lin_LS(np.log(at_bin_AS.T[sel_t].T), np.log(aN_bin_AS.T[sel_t].T)) p_omori = dPL['b'] print 'Omori p-value: ', p_omori print 'R2 value: ', dPL['R2'] #TODO: use dN/dt = alpha*t**(-p) to compute the modeled rates aOmori_rate = 10**(np.log10(at_bin_AS) * p_omori + dPL['a']) #--------------------------4--------------------------------------------- # plots #------------------------------------------------------------------------ plt.figure() ax = plt.subplot() # note: careful with the different time vectors during plotting # we have at_AS - aftershock times in days after mainshock # at_bin_AS - binned time vector for rate computation, this vector is smaller than at_AS
import numpy as np import matplotlib.pyplot as plt import opt_utils as opt_utils np.random.seed(12345) #=================================================================================== # params, dirs, files #=================================================================================== xmin, xmax = 0, 5 N = 20 f_a = 5.0 f_b = -2.4 f_sigma = .5 #=================================================================================== # synthetic data #=================================================================================== a_x = np.linspace(xmin, xmax, N) a_y = f_b * a_x + f_a + np.random.randn(N) * f_sigma #=================================================================================== # lin. LS. and plot #=================================================================================== dLS = opt_utils.lin_LS(a_x, a_y) plt.figure() plt.title(str(dLS['R2'])) ax1 = plt.subplot(111) ax1.plot(a_x, a_y, 'ko', ms=5, mew=1.5, mfc='none', label='obs.') ax1.plot(a_x, dLS['Y_hat'], 'r-', label='Model') ax1.legend(loc='upper right') plt.show()
# -*- coding: utf-8 -*- """ Created on Wed May 8 07:58:54 2019 @author: Brady """ import numpy as np import matplotlib.pyplot as plt import opt_utils as opt Star_lums = 'star_luminos.txt' data = np.genfromtxt(Star_lums, dtype=float, delimiter=(' '), skip_header=(1), usecols=(0, 1)).T #print data aX= data[0]>10 aY= data[1]<592 #print aX #print aY pow_law_fit=opt.lin_LS(aX, aY) print pow_law_fit a = pow_law_fit['a'] b = pow_law_fit['b'] T = aX L = a*T**b print L #plt.plot(aY, aX) plt.plot(aX, L, set(label('star_luminosity'))) #('Star_Luminosity') #plt.set_xlabel('Temp') #plt.set_ylabel('Luminosity') plt.show()
@author: williamdean """ import opt_utils as utils import numpy as np import matplotlib.pyplot as plt # load data star = np.genfromtxt('data/star_luminos.txt') T, L = star.T[0], star.T[1] #print ( T, L) Tmin, Tmax = 10, 1000 # fit data dLS = utils.lin_LS((T), (L)) for tag, item in dLS.items(): if isinstance(item, (int, float)): print(tag, item) #L = alpha*T**beta #at_bin_T, aN_bin_L = seis_utils.eqRate( at_AS) #sel_t = np.logical_and( at_bin_T >= dPar['Tmin'], at_bin_L <= dPar['Tmax']) plt.figure(1) ax1 = plt.subplot(111) ax1.plot(T, L, 'ko', ms=5, mew=1, mfc='none') #a_x = np.linspace( Tmin, Tmax, 1000) #plt.plot( star) plt.xlabel('Temperature(degree C)')
#============================================================================== #computations #============================================================================== #we're only interested in temperatures between 10 and 1000 exclusive sel = np.logical_and(temp > 10, temp < 1000) temp = temp[sel] lum = lum[sel] #data can be described by l=alphaT^beta so it can be linearized by plotting logT vs logl #take the log lintemp = np.log10(temp) linlum = np.log10(lum) #fit the data model = opt_utils.lin_LS(lintemp, linlum) #note model is a dictionary #============================================================================== #plotting #============================================================================== plt.figure() #linearized data plt.plot(lintemp, linlum, 'ko') #predicted values using our least squares model plt.plot(lintemp, model['Y_hat'], 'b-') #labelling plt.xlabel('Log of Temperature') plt.ylabel('Log of Lumosity')
#=================================================================================== # files, parameters #=================================================================================== star_file = 'star_luminos.txt' #=================================================================================== # load data #=================================================================================== mData = np.genfromtxt(star_file, skip_header=1, usecols=(0, 1)).T temp, sol = mData[0], mData[1] print(temp, sol) #=================================================================================== # fit data #=================================================================================== dLS = utils.lin_LS(np.log10(sol), np.log10(temp)) for tag, item in dLS.items(): if isinstance(item, (int, float)): print(tag, item) # compute model fit y_hat a_yhat = 10**dLS['a'] * (sol**dLS['b']) #=================================================================================== # plots #=================================================================================== plt.figure(1) ax1 = plt.subplot(111) ax1.loglog(sol, temp, 'ko', ms=5, mew=1, mfc='none') # ax1.hexbin( a_x, a_y, cmap = plt.cm.Blues) ax1.set_xlabel('Luminosity(Solar Units)')